[v3,2/2] Make SMTP port and authentication configurable

Message ID 20200210100328.75985-2-lfleischer@archlinux.org
State New
Headers show
Series
  • [v3,1/2] Support smtplib for sending emails
Related show

Commit Message

Lukas Fleischer Feb. 10, 2020, 10:03 a.m. UTC
Add more options to configure the smtplib implementation for sending
notification emails.

The port can be changed using the new smtp-port option.

Encryption can be configured using smtp-use-ssl and smtp-use-starttls.
Keep in mind that you usually also need to change the port when enabling
either of these options.

Authentication can be configured using smtp-user and smtp-password.
Authentication is disabled if either of these values is empty.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
---
 aurweb/scripts/notify.py | 20 +++++++++++++++++++-
 conf/config.defaults     |  5 +++++
 2 files changed, 24 insertions(+), 1 deletion(-)

Patch

diff --git a/aurweb/scripts/notify.py b/aurweb/scripts/notify.py
index 941187e..3417721 100755
--- a/aurweb/scripts/notify.py
+++ b/aurweb/scripts/notify.py
@@ -65,6 +65,7 @@  class Notification:
         return body.rstrip()
 
     def send(self):
+        sendmail = aurweb.config.get('notifications', 'sendmail')
         sender = aurweb.config.get('notifications', 'sender')
         reply_to = aurweb.config.get('notifications', 'reply-to')
         reason = self.__class__.__name__
@@ -95,8 +96,25 @@  class Notification:
             else:
                 # send email using smtplib; no local MTA required
                 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')
+
+                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 = smtplib.SMTP(server_addr)
                 server.set_debuglevel(1)
                 server.sendmail(sender, recipient, msg.as_bytes())
                 server.quit()
diff --git a/conf/config.defaults b/conf/config.defaults
index 23d46b0..b69d031 100644
--- a/conf/config.defaults
+++ b/conf/config.defaults
@@ -49,6 +49,11 @@  window_length = 86400
 notify-cmd = /usr/local/bin/aurweb-notify
 sendmail =
 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