Changeset 7350
- Timestamp:
- 03/21/08 16:52:34 (2 months ago)
- Files:
-
- django/trunk/django/core/mail.py (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/mail.py
r7349 r7350 3 3 """ 4 4 5 from django.conf import settings 6 from django.utils.encoding import smart_str, force_unicode 5 import mimetypes 6 import os 7 import smtplib 8 import socket 9 import time 10 import random 7 11 from email import Charset, Encoders 8 12 from email.MIMEText import MIMEText … … 11 15 from email.Header import Header 12 16 from email.Utils import formatdate, parseaddr, formataddr 13 import mimetypes 14 import os 15 import smtplib 16 import socket 17 import time 18 import random 17 18 from django.conf import settings 19 from django.utils.encoding import smart_str, force_unicode 19 20 20 21 # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from … … 70 71 71 72 def forbid_multi_line_headers(name, val): 72 " Forbids multi-line headers, to prevent header injection."73 """Forbids multi-line headers, to prevent header injection.""" 73 74 if '\n' in val or '\r' in val: 74 75 raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name)) … … 103 104 104 105 def __init__(self, host=None, port=None, username=None, password=None, 105 use_tls=None, fail_silently=False):106 use_tls=None, fail_silently=False): 106 107 self.host = host or settings.EMAIL_HOST 107 108 self.port = port or settings.EMAIL_PORT … … 114 115 def open(self): 115 116 """ 116 Ensure we have a connection to the email server. Returns whether or not117 a new connection was required.117 Ensures we have a connection to the email server. Returns whether or 118 not a new connection was required (True or False). 118 119 """ 119 120 if self.connection: … … 137 138 138 139 def close(self): 139 """Close the connection to the email server."""140 """Closes the connection to the email server.""" 140 141 try: 141 142 try: … … 154 155 def send_messages(self, email_messages): 155 156 """ 156 Send one or more EmailMessage objects and returnthe number of email157 Sends one or more EmailMessage objects and returns the number of email 157 158 messages sent. 158 159 """ … … 197 198 connection=None, attachments=None, headers=None): 198 199 """ 199 Initiali se a single email message (which can be sent to multiple200 Initialize a single email message (which can be sent to multiple 200 201 recipients). 201 202 … … 226 227 def message(self): 227 228 encoding = self.encoding or settings.DEFAULT_CHARSET 228 msg = SafeMIMEText(smart_str(self.body, settings.DEFAULT_CHARSET), self.content_subtype, encoding) 229 msg = SafeMIMEText(smart_str(self.body, settings.DEFAULT_CHARSET), 230 self.content_subtype, encoding) 229 231 if self.attachments: 230 232 body_msg = msg … … 254 256 255 257 def send(self, fail_silently=False): 256 """Send the email message."""258 """Sends the email message.""" 257 259 return self.get_connection(fail_silently).send_messages([self]) 258 260 … … 281 283 def _create_attachment(self, filename, content, mimetype=None): 282 284 """ 283 Convert the filename, content, mimetype triple into a MIME attachment285 Converts the filename, content, mimetype triple into a MIME attachment 284 286 object. 285 287 """ … … 298 300 Encoders.encode_base64(attachment) 299 301 if filename: 300 attachment.add_header('Content-Disposition', 'attachment', filename=filename) 302 attachment.add_header('Content-Disposition', 'attachment', 303 filename=filename) 301 304 return attachment 302 305 … … 313 316 self.attach(content=content, mimetype=mimetype) 314 317 315 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None): 318 def send_mail(subject, message, from_email, recipient_list, 319 fail_silently=False, auth_user=None, auth_password=None): 316 320 """ 317 321 Easy wrapper for sending a single message to a recipient list. All members … … 325 329 """ 326 330 connection = SMTPConnection(username=auth_user, password=auth_password, 327 fail_silently=fail_silently) 328 return EmailMessage(subject, message, from_email, recipient_list, connection=connection).send() 329 330 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None): 331 fail_silently=fail_silently) 332 return EmailMessage(subject, message, from_email, recipient_list, 333 connection=connection).send() 334 335 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, 336 auth_password=None): 331 337 """ 332 338 Given a datatuple of (subject, message, from_email, recipient_list), sends … … 342 348 """ 343 349 connection = SMTPConnection(username=auth_user, password=auth_password, 344 fail_silently=fail_silently) 345 messages = [EmailMessage(subject, message, sender, recipient) for subject, message, sender, recipient in datatuple] 350 fail_silently=fail_silently) 351 messages = [EmailMessage(subject, message, sender, recipient) 352 for subject, message, sender, recipient in datatuple] 346 353 return connection.send_messages(messages) 347 354 348 355 def mail_admins(subject, message, fail_silently=False): 349 " Sends a message to the admins, as defined by the ADMINS setting."356 """Sends a message to the admins, as defined by the ADMINS setting.""" 350 357 EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, 351 settings.SERVER_EMAIL, [a[1] for a in352 settings.ADMINS]).send(fail_silently=fail_silently)358 settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS] 359 ).send(fail_silently=fail_silently) 353 360 354 361 def mail_managers(subject, message, fail_silently=False): 355 " Sends a message to the managers, as defined by the MANAGERS setting."362 """Sends a message to the managers, as defined by the MANAGERS setting.""" 356 363 EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, 357 settings.SERVER_EMAIL, [a[1] for a in 358 settings.MANAGERS]).send(fail_silently=fail_silently) 359 364 settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS] 365 ).send(fail_silently=fail_silently)
