Changeset 53285

Show
Ignore:
Timestamp:
12/23/08 13:05:00 (1 year ago)
Author:
gracinet
Message:

Implementation of Bcc and additional headers.
Support for Bcc led to re-implement most of the difference between
_send() and send() in MailHost?. But there was a good reason for OG not to use
send(), so kept it that way.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • CPS3/products/CPSUtil/trunk/mail.py

    r53279 r53285  
    7676    return _html_converter.convert(html_data) 
    7777 
    78 def send_mail(context, mto, mfrom, subject, body, mcc=(), attachments=(), 
    79               encoding='iso-8859-15', plain_text=True): 
     78def send_mail(context, mto, mfrom, subject, body, mcc=(), mbcc=(), 
     79              attachments=(), 
     80              encoding='iso-8859-15', plain_text=True, additional_headers=()): 
    8081    """Send a mail 
    8182 
    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. 
    8386 
    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) 
    8591 
    8692    This function does not do any error handling besides re-casting and logging 
     
    9096    mailhost = getToolByName(context, 'MailHost') 
    9197    attachments = list(attachments) 
    92  
    93     # building the formatted email message 
    94     if not mto: 
    95         raise ValueError('Empty To field forbidden') 
    96     if not isinstance(mto, str): 
    97         mto = ', '.join(mto) 
    9898 
    9999    # prepare main content 
     
    115115    COMMASPACE = ', ' 
    116116 
     117    # Headers 
    117118    msg['Subject'] = subject 
    118119    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 
    120127    if mcc: 
    121128        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 
    122140    msg.preamble = subject 
    123141    # Guarantees the message ends in a newline 
     
    154172 
    155173    # sending and error casting 
     174    if not mto: 
     175        raise ValueError("Empty final list of recipients address") 
    156176    try: 
    157177        return mailhost._send(mfrom, mto, msg.as_string())