6 from PLC.Faults import *
9 used to send out emails - sets up the headers correctly
13 def __init__(self, config):
15 self.MAIL_ENABLED = config.PLC_MAIL_ENABLED
17 self.SMTP_SERVER= 'localhost'
19 self.X_MAILER= 'PlanetLab API Mailer'
23 open up a connect to our mail server, and send out a message.
25 the email addresses are not verified before sending out the message.
27 to_addr_list, cc_addr_list, and from_addr should be a dictionary, with the keys
28 being the email address and the value being the plain text name of
29 the recipient. only the first key from from_addr will be used, so it should
30 contain only one address.
32 subject is not checked for multiple lines - ensure it is only one.
34 def mail( self, to_addr_list, cc_addr_list, from_addr, subject, content ):
36 if self.MAIL_ENABLED == 0:
39 server= smtplib.SMTP(self.SMTP_SERVER)
41 str_date= time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
43 str_from_addr= self.create_recipient_list(from_addr,1)
44 str_to_addr= self.create_recipient_list(to_addr_list)
47 full_message += "Content-type: text/plain; charset=iso-8859-1\r\n"
48 full_message += "Date: %s\r\n" % str_date
49 full_message += "MIME-Version: 1.0\r\n"
51 full_message += "To: %s\r\n" % str_to_addr
52 full_message += "From: %s\r\n" % str_from_addr
54 if cc_addr_list is not None and len(cc_addr_list) > 0:
55 str_cc_addr= self.create_recipient_list(cc_addr_list)
56 full_message += "Cc: %s\r\n" % str_cc_addr
58 full_message += "Subject: %s\r\n" % subject
59 full_message += "Reply-To: %s\r\n" % str_from_addr
60 full_message += "X-Mailer: %s\r\n\r\n" % self.X_MAILER
61 full_message += content
64 # get just a list of to recipients (needed for envelope). this
65 # needs to include all reciepients, including Cc
66 local_to_list= to_addr_list.copy()
68 if cc_addr_list is not None:
69 local_to_list.update( cc_addr_list )
71 all_to_addr_list= self.create_recipient_list(local_to_list,
75 rc= server.sendmail(str_from_addr, all_to_addr_list, full_message )
79 except smtplib.SMTPRecipientsRefused, err:
80 sys.stderr.write( "SMTPRecipientsRefused: %s" % repr(err) )
83 except smtplib.SMTPHeloError:
84 sys.stderr.write( "SMTPHeloError: %s" % repr(err) )
87 except smtplib.SMTPSenderRefused:
88 sys.stderr.write( "SMTPSenderRefused: %s" % repr(err) )
91 except smtplib.SMTPDataError:
92 sys.stderr.write( "SMTPDataError: %s" % repr(err) )
100 accepts a list of email recipient as a dictionary in the same format
101 as the mail function, and returns it in a format suitable for use in
102 an email message. for example:
104 if limit is specified, only limit number of entries from
105 addr_list is used. which one is used is not defined, so it is really only
106 useful to make sure that the result has a single entry (for from lines)
108 for return_string= 1:
109 input: {'user@domain.com': 'A User','test@planet-lab.org': 'PL User'}
110 ouput: 'A User <user@domain.com>, PL User <test@planet-lab.org>'
112 input: {'user@domain.com': 'A User','test@planet-lab.org': 'PL User'}
113 ouput: ['A User <user@domain.com>', 'PL User <test@planet-lab.org>']
116 def create_recipient_list( self, addr_list, return_string= 1, limit= None ):
117 if not isinstance(addr_list,dict):
119 "Internal error, call to create_recipient_list " \
120 "with non-dict addr_list (%s)." % str(addr_list)
123 limit= len(addr_list.keys())
131 for email in addr_list.keys():
132 recipients = recipients + ['%s <%s>' % (addr_list[email],email)]
137 if return_string == 1:
138 return string.join( recipients, ", " )