Changeset 53285
- Timestamp:
- 12/23/08 13:05:00 (1 year ago)
- Files:
-
- CPS3/products/CPSUtil/trunk/mail.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
CPS3/products/CPSUtil/trunk/mail.py
r53279 r53285 76 76 return _html_converter.convert(html_data) 77 77 78 def send_mail(context, mto, mfrom, subject, body, mcc=(), attachments=(), 79 encoding='iso-8859-15', plain_text=True): 78 def send_mail(context, mto, mfrom, subject, body, mcc=(), mbcc=(), 79 attachments=(), 80 encoding='iso-8859-15', plain_text=True, additional_headers=()): 80 81 """Send a mail 81 82 82 body is plain text or html according to plain_text kwarg 83 mto is the user-level Mail To. It can be a string, or list/tuple of strings. 84 It can also be None, or empty, provided there are Ccs or Bccs. 85 mcc (Mail Cc) and mbcc (Mail Bcc) can be strings or list/tuples of strings. 83 86 84 Optional attachments are (filename, content-type, data) tuples. 87 body is plain text or html according to the plain_text kwarg 88 89 Optional attachments are triples (filename, content-type, data) tuples. 90 additional_headers are pairs (name, value) 85 91 86 92 This function does not do any error handling besides re-casting and logging … … 90 96 mailhost = getToolByName(context, 'MailHost') 91 97 attachments = list(attachments) 92 93 # building the formatted email message94 if not mto:95 raise ValueError('Empty To field forbidden')96 if not isinstance(mto, str):97 mto = ', '.join(mto)98 98 99 99 # prepare main content … … 115 115 COMMASPACE = ', ' 116 116 117 # Headers 117 118 msg['Subject'] = subject 118 119 msg['From'] = mfrom 119 msg['To'] = isinstance(mto, basestring) and mto or COMMASPACE.join(mto) 120 121 if not mto: 122 mto = [] 123 if isinstance(mto, basestring): 124 mto = [mto] 125 msg['To'] = COMMASPACE.join(mto) 126 120 127 if mcc: 121 128 msg['Cc'] = isinstance(mcc, basestring) and mcc or COMMASPACE.join(mcc) 129 if not mto: 130 # use first Cc as (non header) mail-to 131 mto = [isinstance(mcc, basestring) and mcc or mcc[0]] 132 if mbcc: 133 # Don't put Bcc in headers otherwise they'd get transferred 134 if isinstance(mbcc, basestring): 135 mto.append(mbcc) 136 else: 137 mto.extend(mbcc) 138 for key, value in additional_headers: 139 msg[key] = value 122 140 msg.preamble = subject 123 141 # Guarantees the message ends in a newline … … 154 172 155 173 # sending and error casting 174 if not mto: 175 raise ValueError("Empty final list of recipients address") 156 176 try: 157 177 return mailhost._send(mfrom, mto, msg.as_string())
