Advertisement
Guest User

Untitled

a guest
Feb 28th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. from django.conf import settings
  2. from django.utils.html import strip_tags, strip_spaces_between_tags
  3. from email.MIMEMultipart import MIMEMultipart
  4. from email.MIMEText import MIMEText
  5. from email.Header import Header
  6. import smtplib, rfc822
  7.  
  8. class BadHeaderError(ValueError):
  9.     pass
  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
  16. newlines (got %r for header %r)" % (val, name)
  17.         if name == "Subject":
  18.             val = Header(val, settings.DEFAULT_CHARSET)
  19.         MIMEText.__setitem__(self, name, val)
  20.  
  21. def send_mass_mail(datatuple, fail_silently=False,
  22. auth_user=settings.EMAIL_HOST_USER,
  23. auth_password=settings.EMAIL_HOST_PASSWORD):
  24.     """
  25.    Given a datatuple of (subject, message, from_email, recipient_list), sends
  26.    each message to each recipient list. Returns the number of e-mails sent.
  27.  
  28.    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
  29.    If auth_user and auth_password are set, they're used to log in.
  30.    """
  31.     # We prepare the server to send messages.
  32.     try:
  33.         server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
  34.         if auth_user and auth_password:
  35.             server.login(auth_user, auth_password)
  36.     except:
  37.         #print 'Server preparation did not work'
  38.         if fail_silently:
  39.             return
  40.         raise
  41.     num_sent = 0
  42.     # We prepare the message to be sent.
  43.     for subject, message, from_email, recipient_list in datatuple:
  44.         if not recipient_list:
  45.             continue
  46.         from_email = from_email or settings.DEFAULT_FROM_EMAIL
  47.         # Create the root message and fill in the basic headers
  48.         msgRoot = MIMEMultipart('related')
  49.         msgRoot['Subject'] = subject
  50.         msgRoot['From'] = from_email
  51.         msgRoot['To'] = ', '.join(recipient_list)
  52.         msgRoot['Date'] = rfc822.formatdate()
  53.         msgRoot.preamble = 'This is a multi-part message in MIME format.'
  54.         # Encapsulate the plain and HTML versions of the message body in an
  55.         # 'alternative' part, so message agents can decide which they
  56. want to display.
  57.         msgAlternative = MIMEMultipart('alternative')
  58.         msgRoot.attach(msgAlternative)
  59.         # This is the html message.
  60.         msgText = MIMEText(message, 'html', settings.DEFAULT_CHARSET)
  61.         msgAlternative.attach(msgText)
  62.         # This is the alternative plain text message.
  63.         msgText =
  64. MIMEText(strip_spaces_between_tags(strip_tags(message)), 'plain',
  65. settings.DEFAULT_CHARSET)
  66.         msgAlternative.attach(msgText)
  67.         # We send the actual message
  68.         try:
  69.             server.sendmail(from_email, recipient_list, msgRoot.as_string())
  70.             num_sent += 1
  71.         except:
  72.             if not fail_silently:
  73.                 raise
  74.     # We quit elegantly
  75.     try:
  76.         server.quit()
  77.     except:
  78.         if fail_silently:
  79.             return
  80.         raise
  81.     return num_sent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement