Selaa lähdekoodia

Implement email. It almost works

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
Sindre Stephansen 5 vuotta sitten
vanhempi
commit
b0bd63d0a1
3 muutettua tiedostoa jossa 30 lisäystä ja 12 poistoa
  1. +1
    -1
      src/app/requirements.txt
  2. +0
    -11
      src/app/views/app.py
  3. +29
    -0
      src/app/views/utils.py

+ 1
- 1
src/app/requirements.txt Näytä tiedosto

@@ -1,4 +1,4 @@
web.py==0.40
mysql-connector-python==8.0.*
mysql-connector-python==8.0.5
python-dotenv
bcrypt

+ 0
- 11
src/app/views/app.py Näytä tiedosto

@@ -9,17 +9,6 @@ from views.project import Project
from views.index import Index
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
web.config.debug = False



+ 29
- 0
src/app/views/utils.py Näytä tiedosto

@@ -1,6 +1,35 @@
import web
import os
import logging
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
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):
default_globals = {


Loading…
Peruuta
Tallenna