Handle exceptions when sending mail

This commit is contained in:
Timotej Lazar 2023-07-07 09:04:17 +02:00
parent 5262c64244
commit 4ef3efbc68

View file

@ -21,13 +21,16 @@ import ldap3
from . import db from . import db
def mail(rcpt, subject, body): def mail(rcpt, subject, body):
msg = email.message.EmailMessage() try:
msg['Subject'] = f'friwall: {subject}' msg = email.message.EmailMessage()
msg['From'] = f'{getpass.getuser()}@{socket.getfqdn()}' msg['Subject'] = f'friwall: {subject}'
msg['To'] = rcpt msg['From'] = f'{getpass.getuser()}@{socket.getfqdn()}'
msg.set_content(body) msg['To'] = rcpt
with smtplib.SMTP('localhost') as server: msg.set_content(body)
server.send_message(msg) with smtplib.SMTP('localhost') as server:
server.send_message(msg)
except Exception as e:
syslog.syslog(f'error sending mail: {e}')
def init_app(app): def init_app(app):
app.cli.add_command(generate) app.cli.add_command(generate)