Guest User

Untitled

a guest
Oct 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #coding: utf-8
  2. from cStringIO import StringIO
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.text import MIMEText
  5. from email.header import Header
  6. from email import Charset
  7. from email.generator import Generator
  8. import smtplib
  9.  
  10. # Example address data
  11. from_address = [u'⌘Tomek Kopczuk⌘', 'tomek@example.com']
  12. recipient = [u'Those #!@', 'contact@example.com']
  13. subject = u'⌘Unicode test⌘'
  14.  
  15. # Example body
  16. html = u'Unicode⏎\nTest⏎'
  17. text = u'Unicode⏎\nTest⏎'
  18.  
  19. # Default encoding mode set to Quoted Printable. Acts globally!
  20. Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
  21.  
  22. # 'alternative’ MIME type – HTML and plain text bundled in one e-mail message
  23. msg = MIMEMultipart('alternative')
  24. msg['Subject'] = "%s" % Header(subject, 'utf-8')
  25. # Only descriptive part of recipient and sender shall be encoded, not the email address
  26. msg['From'] = "\"%s\" <%s>" % (Header(from_address[0], 'utf-8'), from_address[1])
  27. msg['To'] = "\"%s\" <%s>" % (Header(recipient[0], 'utf-8'), recipient[1])
  28.  
  29. # Attach both parts
  30. htmlpart = MIMEText(html, 'html', 'UTF-8')
  31. textpart = MIMEText(text, 'plain', 'UTF-8')
  32. msg.attach(htmlpart)
  33. msg.attach(textpart)
  34.  
  35. # Create a generator and flatten message object to 'file’
  36. str_io = StringIO()
  37. g = Generator(str_io, False)
  38. g.flatten(msg)
  39. # str_io.getvalue() contains ready to sent message
  40.  
  41. # Optionally - send it – using python's smtplib
  42. # or just use Django's
  43. s = smtplib.SMTP('smtp.gmail.com', 587)
  44. s.ehlo()
  45. s.starttls()
  46. s.ehlo()
  47. s.login("", "")
  48. s.sendmail("", recipient[1], str_io.getvalue())
Add Comment
Please, Sign In to add comment