From 17095ada91abd6298815c626b8d2fce82230f6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Tue, 9 Oct 2018 18:48:41 +0200 Subject: [PATCH] Send mail via smtp server --- aurbot | 42 +++++++++++++++++++++++++++++++++++------- aurbot.conf.example1 | 8 +++++++- aurbot.conf.example2 | 9 +++++++-- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/aurbot b/aurbot index 52b0f64..e415702 100755 --- a/aurbot +++ b/aurbot @@ -31,6 +31,7 @@ from logging import StreamHandler, getLogger, Formatter, DEBUG, INFO from os import chdir, environ, getcwd, mkdir, makedirs, geteuid, stat from os.path import exists, join, abspath from signal import signal, SIGHUP +from smtplib import SMTP, SMTP_SSL, SMTPException from subprocess import Popen, check_call, DEVNULL, PIPE from systemd.daemon import notify from tarfile import open as tar @@ -207,12 +208,39 @@ class Aurbot(object): debug("Unable to parse config file, empty one used: %s" % str(exp)) info("Config file loaded %s" % self.config_path) - def send_message(self, msg): + def send_message(self, pkgconfig, msg): + ''' Send message to an smtp server + ''' + info("Sending message to %s" % pkgconfig["notify"]) + # load smtp info + try: + smtp_host = pkgconfig["smtp_host"] + smtp_port = pkgconfig["smtp_port"] + smtp_login = pkgconfig.get("smtp_login", "") + smtp_pass = pkgconfig.get("smtp_pass", "") + smtp_security = pkgconfig.get("smtp_security", "") + except: + error("Unable to load smtp config") + return + # display message content when debug debug(msg) - proc = Popen(["sendmail", "-i", "-t"], stdin=PIPE, close_fds=True) - proc.stdin.write(msg.as_bytes()) - proc.stdin.close() - proc.wait() + # prepare connection + con = SMTP_SSL() if smtp_security == "ssl" else SMTP() + if getLogger().isEnabledFor(DEBUG): + con.set_debuglevel(True) + con._host = smtp_host + try: + con.connect(smtp_host, smtp_port) + if smtp_security == "starttls": + con.starttls() + if smtp_login != "" and smtp_pass != "": + con.login(smtp_login, smtp_pass) + # send it + con.send_message(msg) + # gentleman quit + con.quit() + except Exception as exp: + error("Unable to send message via smtp: %s" % str(exp)) def send_build_report(self, pkgconfig, localpkg, aurpkg, status, logfile): ''' Send build notification @@ -231,7 +259,7 @@ class Aurbot(object): with open(logfile, "r") as fd: mt = MIMEText(fd.read()) msg.attach(mt) - self.send_message(msg) + self.send_message(pkgconfig, msg) def send_maintainer_report(self, pkgconfig, localpkg, aurpkg): ''' Send email to notify invalid maintainer @@ -250,7 +278,7 @@ class Aurbot(object): msg["From"] = pkgconfig.get("from", "Aurbot") msg["To"] = pkgconfig["notify"] msg["Date"] = formatdate(localtime=True) - self.send_message(msg) + self.send_message(pkgconfig, msg) def update(self, pkgconfig, localpkg, aurpkg): ''' Update (build and commit) a package diff --git a/aurbot.conf.example1 b/aurbot.conf.example1 index ffd6232..75a0dff 100644 --- a/aurbot.conf.example1 +++ b/aurbot.conf.example1 @@ -5,6 +5,12 @@ check_interval = 86400 timeout = 30 from = noreply@example.com notify = seblu@example.com +smtp_host = mail.example.com +smtp_port = 465 +smtp_login = seblu@example.com +smtp_pass = password +smtp_security = ssl + [virtualbox-ext-oracle] maintainer = seblu @@ -41,4 +47,4 @@ force = 2592000 #30days maintainer = dolik.rce [iozone] -maintainer = jsst \ No newline at end of file +maintainer = jsst diff --git a/aurbot.conf.example2 b/aurbot.conf.example2 index f70335d..992af3b 100644 --- a/aurbot.conf.example2 +++ b/aurbot.conf.example2 @@ -1,7 +1,12 @@ [DEFAULT] build_cmd = makepkg -from = noreply@seblu.net -notify = seblu@seblu.net +from = noreply@example.com +notify = seblu@example.com +smtp_host = mail.example.com +smtp_port = 587 +smtp_login = login@example.com +smtp_pass = password +smtp_security = starttls [python-sjrpc] maintainer = seblu -- GitLab