Move send_mail() to own module
This commit is contained in:
parent
19a9015dba
commit
ff51bb9ff9
2 changed files with 36 additions and 31 deletions
28
agenda/mail.py
Normal file
28
agenda/mail.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"""Send e-mail."""
|
||||
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
from email.utils import formatdate, make_msgid
|
||||
|
||||
import flask
|
||||
|
||||
|
||||
def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
|
||||
"""Send an e-mail."""
|
||||
msg = EmailMessage()
|
||||
|
||||
msg["Subject"] = subject
|
||||
msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
|
||||
msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
|
||||
msg["Date"] = formatdate()
|
||||
msg["Message-ID"] = make_msgid()
|
||||
|
||||
# Add extra mail headers
|
||||
for header, value in config["MAIL_HEADERS"]:
|
||||
msg[header] = value
|
||||
|
||||
msg.set_content(body)
|
||||
|
||||
s = smtplib.SMTP(config["SMTP_HOST"])
|
||||
s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
|
||||
s.quit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue