[PATCH v2 1/2] Use smtplib for sending emails
Support mail delivery without a local MTA. Instead, an SMTP server can now be configured using the smtp-server option in the [notifications] section. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- Change the default configuration to use localhost. aurweb/scripts/notify.py | 13 ++++++++----- conf/config.defaults | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/aurweb/scripts/notify.py b/aurweb/scripts/notify.py index b0f218b..35d2701 100755 --- a/aurweb/scripts/notify.py +++ b/aurweb/scripts/notify.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 import email.mime.text -import subprocess +import email.utils +import smtplib import sys import textwrap @@ -63,7 +64,7 @@ class Notification: return body.rstrip() def send(self): - sendmail = aurweb.config.get('notifications', 'sendmail') + server_addr = aurweb.config.get('notifications', 'smtp-server') sender = aurweb.config.get('notifications', 'sender') reply_to = aurweb.config.get('notifications', 'reply-to') reason = self.__class__.__name__ @@ -79,13 +80,15 @@ class Notification: msg['Reply-to'] = reply_to msg['To'] = to msg['X-AUR-Reason'] = reason + msg['Date'] = email.utils.formatdate(localtime=True) for key, value in self.get_headers().items(): msg[key] = value - p = subprocess.Popen([sendmail, '-t', '-oi'], - stdin=subprocess.PIPE) - p.communicate(msg.as_bytes()) + server = smtplib.SMTP(server_addr) + server.set_debuglevel(1) + server.sendmail(sender, recipient, msg.as_bytes()) + server.quit() class ResetKeyNotification(Notification): diff --git a/conf/config.defaults b/conf/config.defaults index c519eae..e77ccf4 100644 --- a/conf/config.defaults +++ b/conf/config.defaults @@ -47,7 +47,7 @@ window_length = 86400 [notifications] notify-cmd = /usr/local/bin/aurweb-notify -sendmail = /usr/bin/sendmail +smtp-server = localhost sender = notify@aur.archlinux.org reply-to = noreply@aur.archlinux.org -- 2.25.0
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- Change the default configuration to use localhost. aurweb/scripts/notify.py | 19 ++++++++++++++++++- conf/config.defaults | 5 +++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/aurweb/scripts/notify.py b/aurweb/scripts/notify.py index 35d2701..ddf4736 100755 --- a/aurweb/scripts/notify.py +++ b/aurweb/scripts/notify.py @@ -65,6 +65,11 @@ class Notification: def send(self): server_addr = aurweb.config.get('notifications', 'smtp-server') + server_port = aurweb.config.getint('notifications', 'smtp-port') + use_ssl = aurweb.config.getboolean('notifications', 'smtp-use-ssl') + use_starttls = aurweb.config.getboolean('notifications', 'smtp-use-starttls') + smtp_user = aurweb.config.get('notifications', 'smtp-user') + smtp_passwd = aurweb.config.get('notifications', 'smtp-password') sender = aurweb.config.get('notifications', 'sender') reply_to = aurweb.config.get('notifications', 'reply-to') reason = self.__class__.__name__ @@ -85,7 +90,19 @@ class Notification: for key, value in self.get_headers().items(): msg[key] = value - server = smtplib.SMTP(server_addr) + if use_ssl: + server = smtplib.SMTP_SSL(server_addr, server_port) + else: + server = smtplib.SMTP(server_addr, server_port) + + if use_starttls: + server.ehlo() + server.starttls() + server.ehlo() + + if user and password: + server.login(user, passwd) + server.set_debuglevel(1) server.sendmail(sender, recipient, msg.as_bytes()) server.quit() diff --git a/conf/config.defaults b/conf/config.defaults index e77ccf4..40cad04 100644 --- a/conf/config.defaults +++ b/conf/config.defaults @@ -48,6 +48,11 @@ window_length = 86400 [notifications] notify-cmd = /usr/local/bin/aurweb-notify smtp-server = localhost +smtp-port = 25 +smtp-use-ssl = 0 +smtp-use-starttls = 0 +smtp-user = +smtp-password = sender = notify@aur.archlinux.org reply-to = noreply@aur.archlinux.org -- 2.25.0
participants (1)
-
Lukas Fleischer