Ticket #3307: mail.diff

File mail.diff, 4.0 KB (added by James Bennett, 15 years ago)

Patch which implements this proposal

  • django/core/mail.py

    33from django.conf import settings
    44from email.MIMEText import MIMEText
     5from email.MIMEMultipart import MIMEMultipart
    56from email.Header import Header
    67import smtplib, rfc822
    78import socket
    2930    """
    3031    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
    32 def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
     33def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, suppress_recipients=False):
    3334    """
    3435    Given a datatuple of (subject, message, from_email, recipient_list), sends
    3536    each message to each recipient list. Returns the number of e-mails sent.
     38    If message is a list or tuple, the email will be sent as multipart; the
     39    first element in message will be used as the plain-text version, and the
     40    second will be used as the HTML version.
    3741    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
    3842    If auth_user and auth_password are set, they're used to log in.
     43    If suppress_recipients is True, the first address in the recipients list
     44    will be inserted into the 'To:' field and all other addresses will be
     45    inserted into the 'Bcc:' field.
    3946    """
    4047    try:
    4148        server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
    5057        if not recipient_list:
    5158            continue
    5259        from_email = from_email or settings.DEFAULT_FROM_EMAIL
    53         msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET)
     60        if isinstance(message, basestring):
     61            msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET)
     62        else:
     63            msg = MIMEMultipart('alternative')
     64            msg.attach(MIMEText(message[0]))
     65            msg.attach(MIMEText(message[1], 'html'))
    5466        msg['Subject'] = subject
    5567        msg['From'] = from_email
    56         msg['To'] = ', '.join(recipient_list)
     68        if suppress_recipients:
     69            msg['To'] = recipient_list[0]
     70            if len(recipient_list) > 1:
     71                msg['Bcc'] = ', '.join(recipient_list[1:])
     72        else:
     73            msg['To'] = ', '.join(recipient_list)
    5774        msg['Date'] = rfc822.formatdate()
    5875        try:
    5976            random_bits = str(random.getrandbits(64))
  • docs/email.txt

    6565Here's the definition::
    6767    send_mass_mail(datatuple, fail_silently=False,
    68         auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
     68        auth_user=EMAIL_HOST_USER,
     69        auth_password=EMAIL_HOST_PASSWORD,
     70        suppress_recipients=False):
    7072``datatuple`` is a tuple in which each element is in this format::
    7274    (subject, message, from_email, recipient_list)
     76If the ``message`` in a particular element of ``datatuple`` is a list or tuple,
     77it will result in a multpart MIME message; the first element of ``message`` will
     78be sent as ``text/plain``, and the second will be sent as ``text/html``.
    7480``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
    7581as in ``send_mail()``.
    7783Each separate element of ``datatuple`` results in a separate e-mail message.
    7884As in ``send_mail()``, recipients in the same ``recipient_list`` will all see
    79 the other addresses in the e-mail messages's "To:" field.
     85the other addresses in the e-mail messages's "To:" field. To suppress all but
     86a single address (useful for newsletters or mailing lists), use
     87``suppress_recipients=True``; this will use the first address in the "To:"
     88field, and place all other addresses in the "Bcc:" field.
    8190send_mass_mail() vs. send_mail()
Back to Top