Ticket #1541: 1541_multipart_unified.diff
File 1541_multipart_unified.diff, 3.5 KB (added by , 17 years ago) |
---|
-
django/core/mail.py
4 4 5 5 from django.conf import settings 6 6 from email.MIMEText import MIMEText 7 from email.MIMEMultipart import MIMEMultipart 7 8 from email.Header import Header 8 9 from email.Utils import formatdate 9 10 from email import Charset … … 64 65 val = Header(val, settings.DEFAULT_CHARSET) 65 66 MIMEText.__setitem__(self, name, val) 66 67 68 class SafeMIMEMultipart(MIMEMultipart): 69 def __setitem__(self, name, val): 70 "Forbids multi-line headers, to prevent header injection." 71 if '\n' in val or '\r' in val: 72 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 73 MIMEMultipart.__setitem__(self, name, val) 74 67 75 class SMTPConnection(object): 68 76 """ 69 77 A wrapper that manages the SMTP network connection. … … 154 162 """ 155 163 A container for email information. 156 164 """ 157 def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, connection=None): 165 def __init__(self, subject='', body='', 166 from_email=None, to=None, bcc=None, 167 attachments=None, body_html=None, 168 connection=None): 158 169 self.to = to or [] 159 170 self.bcc = bcc or [] 160 171 self.from_email = from_email or settings.DEFAULT_FROM_EMAIL 161 172 self.subject = subject 162 173 self.body = body 163 174 self.connection = connection 175 self.attachments = attachments or [] 176 if body_html: 177 self.attach(SafeMIMEText(body_html, 'html', settings.DEFAULT_CHARSET)) 164 178 165 179 def get_connection(self, fail_silently=False): 166 180 if not self.connection: 167 181 self.connection = SMTPConnection(fail_silently=fail_silently) 168 182 return self.connection 169 183 184 def attach(self, attachment): 185 self.attachments += [attachment] 186 170 187 def message(self): 171 msg = SafeMIMEText(self.body, 'plain', settings.DEFAULT_CHARSET) 188 if self.attachments is not []: 189 msg = SafeMIMEMultipart('alternative', settings.DEFAULT_CHARSET) 190 for attachment in self.attachments: 191 msg.attach(attachment) 192 else: 193 msg = SafeMIMEText(self.body, 'plain', settings.DEFAULT_CHARSET) 172 194 msg['Subject'] = self.subject 173 195 msg['From'] = self.from_email 174 196 msg['To'] = ', '.join(self.to) … … 189 211 """Send the email message.""" 190 212 return self.get_connection(fail_silently).send_messages([self]) 191 213 192 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None): 214 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, 215 message_html=None, auth_user=None, auth_password=None): 193 216 """ 194 217 Easy wrapper for sending a single message to a recipient list. All members 195 218 of the recipient list will see the other recipients in the 'To' field. … … 202 225 """ 203 226 connection = SMTPConnection(username=auth_user, password=auth_password, 204 227 fail_silently=fail_silently) 205 return EmailMessage(subject, message, from_email, recipient_list, connection=connection).send() 228 return EmailMessage(subject, message, from_email, recipient_list, 229 body_html=message_html, connection=connection).send() 206 230 207 231 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None): 208 232 """