Ticket #3985: mail-additional_headers.3.diff

File mail-additional_headers.3.diff, 4.3 KB (added by SmileyChris, 8 years ago)

backwards compatibility and documentation

  • django/core/mail.py

     
    3434            val = Header(val, settings.DEFAULT_CHARSET)
    3535        MIMEText.__setitem__(self, name, val)
    3636
    37 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None):
     37def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, additional_headers_dict={}):
    3838    """
    3939    Easy wrapper for sending a single message to a recipient list. All members
    4040    of the recipient list will see the other recipients in the 'To' field.
     
    4646        auth_user = settings.EMAIL_HOST_USER
    4747    if auth_password is None:
    4848        auth_password = settings.EMAIL_HOST_PASSWORD
    49     return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
     49    return send_mass_mail([[subject, message, from_email, recipient_list, additional_headers_dict]], fail_silently, auth_user, auth_password)
    5050
    5151def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
    5252    """
    53     Given a datatuple of (subject, message, from_email, recipient_list), sends
    54     each message to each recipient list. Returns the number of e-mails sent.
     53    Given a datatuple of (subject, message, from_email, recipient_list) or
     54    (subject, message, from_email, recipient_list, additional_headers_dict),
     55    sends each message to each recipient list. Returns the number of e-mails
     56    sent.
    5557
    5658    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
    5759    If auth_user and auth_password are set, they're used to log in.
     
    7173            return
    7274        raise
    7375    num_sent = 0
    74     for subject, message, from_email, recipient_list in datatuple:
     76    for data in datatuple:
     77        if len(data) == 5:
     78            subject, message, from_email, recipient_list, additional_headers_dict = data
     79        else:
     80            # Backwards compatibility (no additional_headers_dict passed)
     81            subject, message, from_email, recipient_list = data
     82            additional_headers_dict = {}
    7583        if not recipient_list:
    7684            continue
    7785        from_email = from_email or settings.DEFAULT_FROM_EMAIL
     
    8593        except AttributeError: # Python 2.3 doesn't have random.getrandbits().
    8694            random_bits = ''.join([random.choice('1234567890') for i in range(19)])
    8795        msg['Message-ID'] = "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME)
     96        core_headers = ['Subject', 'From', 'To', 'Date', 'Message-ID']
     97        for header in additional_headers_dict:
     98            if header not in core_headers:
     99                msg[header] = additional_headers_dict[header]
    88100        try:
    89101            server.sendmail(from_email, recipient_list, msg.as_string())
    90102            num_sent += 1
  • docs/email.txt

     
    3535
    3636    send_mail(subject, message, from_email, recipient_list,
    3737        fail_silently=False, auth_user=None,
    38         auth_password=None)
     38        auth_password=None, additional_headers_dict={})
    3939
    4040The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
    4141are required.
     
    5555    * ``auth_password``: The optional password to use to authenticate to the
    5656      SMTP server. If this isn't provided, Django will use the value of the
    5757      ``EMAIL_HOST_PASSWORD`` setting.
     58    * ``additional_headers_dict``: An optional dictionary of additional headers
     59      to use in the outgoing e-mail.
    5860
    5961.. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
    6062
     
    6769    send_mass_mail(datatuple, fail_silently=False,
    6870        auth_user=None, auth_password=None):
    6971
    70 ``datatuple`` is a tuple in which each element is in this format::
     72``datatuple`` is a tuple in which each element is either of the following
     73formats::
    7174
    7275    (subject, message, from_email, recipient_list)
     76    (subject, message, from_email, recipient_list, additional_headers_dict)
    7377
    7478``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
    7579as in ``send_mail()``.
Back to Top