Ticket #1541: multipart_mail.diff
File multipart_mail.diff, 2.1 KB (added by , 19 years ago) |
---|
-
mail.py
2 2 3 3 from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD 4 4 from email.MIMEText import MIMEText 5 from email.MIMEMultipart import MIMEMultipart 5 6 import smtplib 6 7 7 8 class BadHeaderError(ValueError): 8 9 pass 9 10 11 class SafeMIMEMultipart(MIMEMultipart): 12 def __setitem__(self, name, val): 13 "Forbids multi-line headers, to prevent header injection." 14 if '\n' in val or '\r' in val: 15 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 16 MIMEMultipart.__setitem__(self, name, val) 17 10 18 class SafeMIMEText(MIMEText): 11 19 def __setitem__(self, name, val): 12 20 "Forbids multi-line headers, to prevent header injection." … … 18 26 """ 19 27 Easy wrapper for sending a single message to a recipient list. All members 20 28 of the recipient list will see the other recipients in the 'To' field. 29 Note that the message parameter can be either text or one of the SafeMIMExxx methods listed above. 21 30 """ 22 31 return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password) 23 32 … … 28 37 29 38 If from_email is None, the DEFAULT_FROM_EMAIL setting is used. 30 39 If auth_user and auth_password are set, they're used to log in. 40 Note that the message parameter can be either text or one of the SafeMIMExxx methods listed above. 31 41 """ 32 42 try: 33 43 server = smtplib.SMTP(EMAIL_HOST) … … 42 52 if not recipient_list: 43 53 continue 44 54 from_email = from_email or DEFAULT_FROM_EMAIL 45 msg = SafeMIMEText(message) 55 56 if isinstance(message, SafeMIMEText) or isinstance(message, SafeMIMEMultipart): 57 msg = message 58 else: 59 msg = SafeMIMEText(message) 60 46 61 msg['Subject'] = subject 47 62 msg['From'] = from_email 48 63 msg['To'] = ', '.join(recipient_list)