Ticket #1541: mail_attachment_as_str.diff
File mail_attachment_as_str.diff, 5.3 KB (added by , 18 years ago) |
---|
-
django/core/mail.py
1 1 # Use this module for e-mailing. 2 2 3 3 from django.conf import settings 4 from email import Encoders 5 from email.MIMEBase import MIMEBase 6 from email.MIMEMultipart import MIMEMultipart 4 7 from email.MIMEText import MIMEText 5 8 from email.Header import Header 6 9 import smtplib, rfc822 … … 22 25 val = Header(val, settings.DEFAULT_CHARSET) 23 26 MIMEText.__setitem__(self, name, val) 24 27 25 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD): 28 class SafeMIMEMultipart(MIMEMultipart): 29 def __setitem__(self, name, val): 30 "Forbids multi-line headers, to prevent header injection." 31 if '\n' in val or '\r' in val: 32 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 33 if name == "Subject": 34 val = Header(val, settings.DEFAULT_CHARSET) 35 MIMEMultipart.__setitem__(self, name, val) 36 37 def attachFile(self, filename, content, mimetype): 38 maintype, subtype = mimetype.split('/', 1) 39 msg = MIMEBase(maintype, subtype) 40 msg.set_payload(content) 41 Encoders.encode_base64(msg) 42 msg.add_header('Content-Disposition', 'attachment', filename=filename) 43 MIMEMultipart.attach(self, msg) 44 45 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, attachments=None): 26 46 """ 27 47 Easy wrapper for sending a single message to a recipient list. All members 28 48 of the recipient list will see the other recipients in the 'To' field. 29 49 """ 30 return send_mass_mail([[subject, message, from_email, recipient_list ]], fail_silently, auth_user, auth_password)50 return send_mass_mail([[subject, message, from_email, recipient_list, attachments]], fail_silently, auth_user, auth_password) 31 51 32 52 def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD): 33 53 """ … … 46 66 return 47 67 raise 48 68 num_sent = 0 49 for subject, message, from_email, recipient_list in datatuple: 69 for i in datatuple: 70 try: 71 subject, message, from_email, recipient_list, attachments = i 72 except ValueError: 73 subject, message, from_email, recipient_list = i 74 attachments = None 50 75 if not recipient_list: 51 76 continue 52 77 from_email = from_email or settings.DEFAULT_FROM_EMAIL 53 msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET) 78 body = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET) 79 if attachments: 80 # This is a multipart mail 81 msg = SafeMIMEMultipart() 82 # First the body 83 msg.attach(body) 84 # Then the various files to be attached. 85 for (filename, content, mimetype) in attachments: 86 msg.attachFile(filename, content, mimetype) 87 else: 88 msg = body 54 89 msg['Subject'] = subject 55 90 msg['From'] = from_email 56 91 msg['To'] = ', '.join(recipient_list) -
docs/email.txt
35 35 36 36 send_mail(subject, message, from_email, recipient_list, 37 37 fail_silently=False, auth_user=EMAIL_HOST_USER, 38 auth_password=EMAIL_HOST_PASSWORD )38 auth_password=EMAIL_HOST_PASSWORD, attachments=None) 39 39 40 40 The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters 41 41 are required. … … 55 55 * ``auth_password``: The optional password to use to authenticate to the 56 56 SMTP server. If this isn't provided, Django will use the value of the 57 57 ``EMAIL_HOST_PASSWORD`` setting. 58 * ``attachments``: A list of ``(filename, content, mimetype)`` tuples 59 specifying attachements. 58 60 59 61 .. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html 60 62 63 Sending mail with attachments 64 ----------------------------- 65 66 ``attachments`` is a list of ``(filename, content, mimetype)`` tuples. 67 ``filename`` specifes the name of the file to be attached, ``content`` is the 68 contents of a file as a ``str`` object, and finally, ``mimetype`` is the mime 69 type of the attachment. To attach an image and an audio file pass 70 71 attachments = [ 72 ('image.png', open('/home/user/image.png', 'rb').read(), 'image/png'), 73 ('audio.mp3', open('/home/user/audio.mp3', 'rb').read(), 'audio/mpeg'), 74 ] 75 76 to the ``send_mail`` function. 77 61 78 send_mass_mail() 62 79 ================ 63 80 … … 67 84 send_mass_mail(datatuple, fail_silently=False, 68 85 auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD): 69 86 70 ``datatuple`` is a tuple in which each element is in thisformat::87 ``datatuple`` is a tuple in which each element is either in the format:: 71 88 72 89 (subject, message, from_email, recipient_list) 73 90 91 or:: 92 93 (subject, message, from_email, recipient_list, attachments) 94 74 95 ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions 75 96 as in ``send_mail()``. 76 97