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 """
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 = (),
import base64
import sys
-from common.send_mail import sendmail
+from common.send_mail import sendmail, MailError
def main():
"""The main body"""
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())