Catch error of sendmail in job_mail_sender.py
authorZhuoX Li <zhuox.li@intel.com>
Fri, 4 Apr 2014 09:16:12 +0000 (17:16 +0800)
committerLin A Yang <lin.a.yang@intel.com>
Tue, 6 May 2014 04:02:29 +0000 (07:02 +0300)
Catch the error,when happens '5.5.1 Error: no valid recipients'

Change-Id: I8a0cc8d424014c0a86bfdd919438da894ccbdf6f
Fixes: #1404

common/send_mail.py
job_mail_sender.py

index 5e15690..2e2fbf7 100644 (file)
@@ -13,6 +13,12 @@ from email import encoders as Encoders
 from email.header import Header
 from email.utils import parseaddr, formataddr
 
+class MailError(Exception):
+    """Local Error handler
+    """
+    pass
+
+
 def prepare_mail(env_filename, subject, body, mail_from, mail_to, mail_cc = '',
         mail_bcc = '', attachment = None):
     """ Prepare a parameter inject file for mail sender job """
@@ -48,6 +54,8 @@ def sendmail(from_email, to_who, msg, smtp_server):
         smtp.quit()
         print "Email to %s sent succeeded" % to_who
     except smtplib.SMTPException, smtpe:
+        if 'no valid recipients' in smtpe.recipients.values()[0][1]:
+            raise MailError('Error: unable to send email: %s' % smtpe)
         raise Exception, "Error: unable to send email: %s" % smtpe
 
 def makemail(subject = '', body = '', from_email = None, to_who = (),
index 37eae0c..207c121 100755 (executable)
@@ -6,7 +6,7 @@ import os
 import base64
 import sys
 
-from common.send_mail import sendmail
+from common.send_mail import sendmail, MailError
 
 def main():
     """The main body"""
@@ -24,12 +24,16 @@ def main():
         fromaddr = os.getenv('FROM')
         toaddr = os.getenv('TO')
         smtp_server = os.getenv('SMTP_SERVER')
-        if fromaddr and toaddr and smtp_server:
-            sendmail(fromaddr, toaddr.split(','), msg, smtp_server)
-        else:
+        if not fromaddr or not toaddr or not smtp_server:
             print "Error: configuration parameters 'FROM', 'TO' "\
                   "or 'SMTP_SERVER' are not set"
             return 1
+        try:
+           sendmail(fromaddr, toaddr.split(','), msg, smtp_server)
+        except MailError, err:
+            print 'Email from %s to %s' % (fromaddr, toaddr.split(','))
+            print err
+            return 1
 
 if __name__ == '__main__':
     sys.exit(main())