-
- """
- accepts a list of email recipient as a dictionary in the same format
- as the mail function, and returns it in a format suitable for use in
- an email message. for example:
-
- if limit is specified, only limit number of entries from
- addr_list is used. which one is used is not defined, so it is really only
- useful to make sure that the result has a single entry (for from lines)
-
- for return_string= 1:
- input: {'user@domain.com': 'A User','test@planet-lab.org': 'PL User'}
- ouput: 'A User <user@domain.com>, PL User <test@planet-lab.org>'
- otherwise:
- input: {'user@domain.com': 'A User','test@planet-lab.org': 'PL User'}
- ouput: ['A User <user@domain.com>', 'PL User <test@planet-lab.org>']
-
- """
- def create_recipient_list( self, addr_list, return_string= 1, limit= None ):
- if not isinstance(addr_list,dict):
- raise PLCAPIError, \
- "Internal error, call to create_recipient_list " \
- "with non-dict addr_list (%s)." % str(addr_list)
-
- if limit == None:
- limit= len(addr_list.keys())
-
- if limit <= 0:
- return ''
-
- recipients= []
- total= 0
-
- for email in addr_list.keys():
- recipients = recipients + ['%s <%s>' % (addr_list[email],email)]
- total= total+1
- if total == limit:
- break
-
- if return_string == 1:
- return string.join( recipients, ", " )
- else:
- return recipients
-
-
-
-
+ # Fix up defaults
+ if not isinstance(To, list):
+ To = [To]
+ if Cc is not None and not isinstance(Cc, list):
+ Cc = [Cc]
+ if Bcc is not None and not isinstance(Bcc, list):
+ Bcc = [Bcc]
+ if From is None:
+ From = ("%s Support" % api.config.PLC_NAME,
+ api.config.PLC_MAIL_SUPPORT_ADDRESS)
+
+ # Create a MIME-encoded UTF-8 message
+ msg = MIMEText(Body.encode(api.encoding), _charset = api.encoding)
+
+ # Unicode subject headers are automatically encoded correctly
+ msg['Subject'] = Subject
+
+ def encode_addresses(addresses, header_name = None):
+ """
+ Unicode address headers are automatically encoded by
+ email.Header, but not correctly. The correct way is to put the
+ textual name inside quotes and the address inside brackets:
+
+ To: "=?utf-8?b?encoded" <recipient@domain>
+
+ Each address in addrs may be a tuple of (name, address) or
+ just an address. Returns a tuple of (header, addrlist)
+ representing the encoded header text and the list of plain
+ text addresses.
+ """
+
+ header = []
+ addrs = []
+
+ for addr in addresses:
+ if isinstance(addr, tuple):
+ (name, addr) = addr
+ try:
+ name = name.encode('ascii')
+ header.append('%s <%s>' % (name, addr))
+ except:
+ h = Header(name, charset = api.encoding, header_name = header_name)
+ header.append('"%s" <%s>' % (h.encode(), addr))
+ else:
+ header.append(addr)
+ addrs.append(addr)
+
+ return (", ".join(header), addrs)
+
+ (msg['From'], from_addrs) = encode_addresses([From], 'From')
+ (msg['To'], to_addrs) = encode_addresses(To, 'To')
+
+ if Cc is not None:
+ (msg['Cc'], cc_addrs) = encode_addresses(Cc, 'Cc')
+ to_addrs += cc_addrs
+
+ if Bcc is not None:
+ (unused, bcc_addrs) = encode_addresses(Bcc, 'Bcc')
+ to_addrs += bcc_addrs
+
+ # Needed to pass some spam filters
+ msg['Reply-To'] = msg['From']
+ msg['X-Mailer'] = "Python/" + sys.version.split(" ")[0]
+
+ if not api.config.PLC_MAIL_ENABLED:
+ print >> log, "From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg
+ return
+
+ s = SMTP()
+ s.connect()
+ rejected = s.sendmail(from_addrs[0], to_addrs, msg.as_string(), rcpt_options = ["NOTIFY=NEVER"])
+ s.close()
+
+ if rejected:
+ raise PLCAPIError, "Error sending message to " + ", ".join(rejected.keys())