This is a fix to issue #10, originally opened by Kamil Paral
on the original trac issue tracker about 10 months ago.
With this, we add an option to the method send_email on that API,
so that one can specify a custom mailserver. I've added an smtp_info
dictionary where one can fill it up with smtp info.
Modified the only user of that function in the upstream code,
server/frontend.py. Also, made the code use proper logging,
and also, appropriately use a custom mail server defined on
global_config.ini (this custom server thing already existed
for the scheduler e-mails).
Signed-off-by: Lucas Meneghel Rodrigues <lmr(a)redhat.com>
---
client/common_lib/base_utils.py | 32 +++++++++++++++++++++-----------
server/frontend.py | 30 ++++++++++++++++++++++--------
2 files changed, 43 insertions(+), 19 deletions(-)
diff --git a/client/common_lib/base_utils.py b/client/common_lib/base_utils.py
index ad19e80..b191911 100644
--- a/client/common_lib/base_utils.py
+++ b/client/common_lib/base_utils.py
@@ -154,29 +154,39 @@ def set_ip_local_port_range(lower, upper):
'%d %d\n' % (lower, upper))
-
-def send_email(mail_from, mail_to, subject, body):
- """
- Sends an email via smtp
-
- mail_from: string with email address of sender
- mail_to: string or list with email address(es) of recipients
- subject: string with subject of email
- body: (multi-line) string with body of email
+def send_email(mail_from, mail_to, subject, body,
+ smtp_info={'server':'localhost',
'user':'', 'password':'',
+ 'port':''}):
+ """
+ Sends an email via SMTP.
+
+ @param mail_from: string with email address of sender
+ @param mail_to: string or list with email address(es) of recipients
+ @param subject: string with subject of email
+ @param body: (multi-line) string with body of email
+ @param smtp_info: Dict with smtp server info
+ server: SMTP server
+ user: SMTP user (if any)
+ password: SMTP password (if any)
+ port: SMTP port (if non standard)
"""
if isinstance(mail_to, str):
mail_to = [mail_to]
msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (mail_from,
','.join(mail_to),
subject, body)
try:
- mailer = smtplib.SMTP('localhost')
+ # Here if we pass an empty string as port, the default (25) will be
+ # used
http://docs.python.org/library/smtplib.html
+ mailer = smtplib.SMTP(smtp_info['server'], smtp_info['port'])
+ if smtp_info['user']:
+ mailer.login(smtp_info['user'], smtp_info['password'])
try:
mailer.sendmail(mail_from, mail_to, msg)
finally:
mailer.quit()
except Exception, e:
- # Emails are non-critical, not errors, but don't raise them
- print "Sending email failed. Reason: %s" % repr(e)
+ # Emails are non-critical, log the error, don't raise it
+ logging.error("Sending email failed. Reason: %s", repr(e))
def read_one_line(filename):
diff --git a/server/frontend.py b/server/frontend.py
index eb7b23c..3c32594 100644
--- a/server/frontend.py
+++ b/server/frontend.py
@@ -14,7 +14,7 @@ For docs, see:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api
"""
-import getpass, os, time, traceback, re
+import getpass, os, time, traceback, re, logging
import common
from autotest_lib.frontend.afe import rpc_client_lib
from autotest_lib.client.common_lib import global_config
@@ -393,15 +393,29 @@ class AFE(RpcClient):
url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
text.append(url + '\n')
+ smtp_info = {}
+ smtp_info['server'] = GLOBAL_CONFIG.get_config_value('SERVER',
+ 'smtp_server',
+
default='localhost')
+ smtp_info['port'] = GLOBAL_CONFIG.get_config_value('SERVER',
+ 'smtp_port',
+ default='')
+ smtp_info['user'] = GLOBAL_CONFIG.get_config_value('SERVER',
+ 'smtp_user',
+ default='')
+ smtp_info['password'] = GLOBAL_CONFIG.get_config_value('SERVER',
+ 'smtp_password',
+ default='')
+
body = '\n'.join(text)
- print '---------------------------------------------------'
- print 'Subject: ', subject
- print body
- print '---------------------------------------------------'
+ logging.debug('---------------------------------------------------')
+ logging.debug('Subject: %s', subject)
+ for l in body.splitlines():
+ logging.debug(l)
+ logging.debug('---------------------------------------------------')
if email_from and email_to:
- print 'Sending email ...'
- utils.send_email(email_from, email_to, subject, body)
- print
+ logging.debug('Sending email ...')
+ utils.send_email(email_from, email_to, subject, body, smtp_info)
def print_job_result(self, job):
--
1.7.6