Ticket #3307: mail.diff
File mail.diff, 4.0 KB (added by , 18 years ago) |
---|
-
django/core/mail.py
2 2 3 3 from django.conf import settings 4 4 from email.MIMEText import MIMEText 5 from email.MIMEMultipart import MIMEMultipart 5 6 from email.Header import Header 6 7 import smtplib, rfc822 7 8 import socket … … 29 30 """ 30 31 return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password) 31 32 32 def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD ):33 def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, suppress_recipients=False): 33 34 """ 34 35 Given a datatuple of (subject, message, from_email, recipient_list), sends 35 36 each message to each recipient list. Returns the number of e-mails sent. 36 37 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. 37 41 If from_email is None, the DEFAULT_FROM_EMAIL setting is used. 38 42 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. 39 46 """ 40 47 try: 41 48 server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) … … 50 57 if not recipient_list: 51 58 continue 52 59 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')) 54 66 msg['Subject'] = subject 55 67 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) 57 74 msg['Date'] = rfc822.formatdate() 58 75 try: 59 76 random_bits = str(random.getrandbits(64)) -
docs/email.txt
65 65 Here's the definition:: 66 66 67 67 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): 69 71 70 72 ``datatuple`` is a tuple in which each element is in this format:: 71 73 72 74 (subject, message, from_email, recipient_list) 73 75 76 If the ``message`` in a particular element of ``datatuple`` is a list or tuple, 77 it will result in a multpart MIME message; the first element of ``message`` will 78 be sent as ``text/plain``, and the second will be sent as ``text/html``. 79 74 80 ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions 75 81 as in ``send_mail()``. 76 82 77 83 Each separate element of ``datatuple`` results in a separate e-mail message. 78 84 As in ``send_mail()``, recipients in the same ``recipient_list`` will all see 79 the other addresses in the e-mail messages's "To:" field. 85 the other addresses in the e-mail messages's "To:" field. To suppress all but 86 a single address (useful for newsletters or mailing lists), use 87 ``suppress_recipients=True``; this will use the first address in the "To:" 88 field, and place all other addresses in the "Bcc:" field. 80 89 81 90 send_mass_mail() vs. send_mail() 82 91 --------------------------------