- Introduce exceptions to be raised upon transient and permanent delivery failures, as specified by SMTP RFC. Depending on type of failure, return either 451 or 554 reply code. - When serialising a message, treat ValueError as a serialisation issue (and try again to deliver in cleartext).
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""Lacre notification sender"""
|
|
|
|
import logging
|
|
import lacre
|
|
import lacre.config as conf
|
|
import smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
import markdown
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
def _load_file(name):
|
|
f = open(name)
|
|
data = f.read()
|
|
f.close()
|
|
return data
|
|
|
|
|
|
def _authenticate_maybe(smtp):
|
|
if conf.config_item_equals('smtp', 'enabled', 'true'):
|
|
LOG.debug(f"Connecting to {conf.get_item('smtp', 'host')}:{conf.get_item('smtp', 'port')}")
|
|
smtp.connect(conf.get_item('smtp', 'host'), conf.get_item('smtp', 'port'))
|
|
smtp.ehlo()
|
|
if conf.config_item_equals('smtp', 'starttls', 'true'):
|
|
LOG.debug("StartTLS enabled")
|
|
smtp.starttls()
|
|
smtp.ehlo()
|
|
smtp.login(conf.get_item('smtp', 'username'), conf.get_item('smtp', 'password'))
|
|
|
|
|
|
def notify(mailsubject, messagefile, recipients = None):
|
|
"""Send notification email."""
|
|
|
|
mailbody = _load_file(conf.get_item('cron', 'mail_templates') + "/" + messagefile)
|
|
msg = MIMEMultipart("alternative")
|
|
|
|
msg["From"] = conf.get_item('cron', 'notification_email')
|
|
msg["To"] = recipients
|
|
msg["Subject"] = mailsubject
|
|
|
|
msg.attach(MIMEText(mailbody, 'plain'))
|
|
msg.attach(MIMEText(markdown.markdown(mailbody), 'html'))
|
|
|
|
if conf.config_item_set('relay', 'host') and conf.config_item_set('relay', 'enc_port'):
|
|
host = conf.relay_params()
|
|
smtp = smtplib.SMTP(host.name, host.port)
|
|
_authenticate_maybe(smtp)
|
|
LOG.info('Delivering notification: %s', recipients)
|
|
smtp.sendmail(conf.get_item('cron', 'notification_email'), recipients, msg.as_string())
|
|
else:
|
|
LOG.warning("Could not send mail due to wrong configuration")
|