The email works when sent from app.py, but not from any other file. Also, it requires mysql-connector-python version 8.0.5, for some reason. Right now the email is logged, so even if it couldn't get through the server testing works.pull/40/head
| @@ -1,4 +1,4 @@ | |||||
| web.py==0.40 | web.py==0.40 | ||||
| mysql-connector-python==8.0.* | |||||
| mysql-connector-python==8.0.5 | |||||
| python-dotenv | python-dotenv | ||||
| bcrypt | bcrypt | ||||
| @@ -9,17 +9,6 @@ from views.project import Project | |||||
| from views.index import Index | from views.index import Index | ||||
| from views.apply import Apply | from views.apply import Apply | ||||
| # Connect to smtp server, enables web.sendmail() | |||||
| try: | |||||
| smtp_server = os.getenv("smtp_server") + ":25" | |||||
| web.config.smtp_server = smtp_server | |||||
| except: | |||||
| smtp_server = "molde.idi.ntnu.no:25" | |||||
| web.config.smtp_server = smtp_server | |||||
| # Example use of the smtp server, insert username | |||||
| # web.sendmail("beelance@ntnu.no", "<username>@stud.ntnu.no", "Hello", "Grz, the beelance app is running") | |||||
| # Disable the debug error page | # Disable the debug error page | ||||
| web.config.debug = False | web.config.debug = False | ||||
| @@ -1,6 +1,35 @@ | |||||
| import web | import web | ||||
| import os | |||||
| import logging | |||||
| import smtplib | |||||
| from email.message import EmailMessage | |||||
| from email.headerregistry import Address | |||||
| from uuid import uuid4 | from uuid import uuid4 | ||||
| logger = logging.getLogger(__name__) | |||||
| def sendmail(subject, message, to_name, to_email, from_name="Beelance", from_email="beelance@ntnu.no"): | |||||
| try: | |||||
| msg = EmailMessage() | |||||
| msg['From'] = Address(from_name, from_email) | |||||
| msg['To'] = Address(to_name, to_email) | |||||
| msg['Subject'] = subject | |||||
| msg.set_content(message) | |||||
| logger.info("Sending email: %s", msg) | |||||
| with get_smtp() as smtp: | |||||
| smtp.set_debuglevel(2) | |||||
| smtp.send_message(msg) | |||||
| except Exception: | |||||
| logging.exception("Exception when sending email") | |||||
| def get_smtp(timeout=3000): | |||||
| smtp_server = os.getenv("smtp_server", default="molde.idi.ntnu.no") + ":25" | |||||
| return smtplib.SMTP(smtp_server, timeout=timeout) | |||||
| def get_render(path='templates/', globals={}, **kwargs): | def get_render(path='templates/', globals={}, **kwargs): | ||||
| default_globals = { | default_globals = { | ||||