Guest User

Untitled

a guest
Mar 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. def createhtmlmail (html, text, subject):
  2. """Create a mime-message that will render HTML in popular
  3. MUAs, text in better ones"""
  4. import MimeWriter
  5. import mimetools
  6. import cStringIO
  7. from email.MIMEMultipart import MIMEMultipart
  8. from email.MIMEBase import MIMEBase
  9. from email.MIMEText import MIMEText
  10. from email.Utils import COMMASPACE, formatdate
  11. from email import Encoders
  12. import os
  13.  
  14. out = cStringIO.StringIO() # output buffer for our message
  15. htmlin = cStringIO.StringIO(html)
  16. txtin = cStringIO.StringIO(text)
  17.  
  18. writer = MimeWriter.MimeWriter(out)
  19. #
  20. # set up some basic headers... we put subject here
  21. # because smtplib.sendmail expects it to be in the
  22. # message body
  23. #
  24. writer.addheader("Subject", subject)
  25. writer.addheader("MIME-Version", "1.0")
  26. #
  27. # start the multipart section of the message
  28. # multipart/alternative seems to work better
  29. # on some MUAs than multipart/mixed
  30. #
  31. writer.startmultipartbody("alternative")
  32. writer.flushheaders()
  33. #
  34. # the plain text section
  35. #
  36. subpart = writer.nextpart()
  37. subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
  38. pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
  39. mimetools.encode(txtin, pout, 'quoted-printable')
  40. txtin.close()
  41. #
  42. # start the html subpart of the message
  43. #
  44. subpart = writer.nextpart()
  45. subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
  46. #
  47. # returns us a file-ish object we can write to
  48. #
  49. pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
  50. mimetools.encode(htmlin, pout, 'quoted-printable')
  51. htmlin.close()
  52.  
  53.  
  54. #
  55. # Now that we're done, close our writer and
  56. # return the message body
  57. #
  58. writer.lastpart()
  59. msg = out.getvalue()
  60. out.close()
  61. return msg
  62.  
  63. import smtplib
  64. f = open("/path/to/html/version.html", 'r')
  65. html = f.read()
  66. f.close()
  67. f = open("/path/to/txt/version.txt", 'r')
  68. text = f.read()
  69. subject = "Prova email html da python, con allegato!"
  70. message = createhtmlmail(html, text, subject)
  71. gmail_user = "thegmailaccount@gmail.com"
  72. gmail_pwd = "thegmailpassword"
  73. server = smtplib.SMTP("smtp.gmail.com", 587)
  74. server.ehlo()
  75. server.starttls()
  76. server.ehlo()
  77. server.login(gmail_user, gmail_pwd)
  78. server.sendmail(gmail_user, "example@example.com", message)
  79. server.close()
  80.  
  81. from mailer import Mailer
  82. from mailer import Message
  83.  
  84. message = Message(From="me@example.com",
  85. To=["you@example.com", "him@example.com"])
  86. message.Subject = "Kitty with dynamite"
  87. message.Body = """Kitty go boom!"""
  88. message.attach("kitty.jpg")
  89.  
  90. sender = Mailer('smtp.example.com')
  91. sender.login("username", "password")
  92. sender.send(message)
  93.  
  94. from mailer import Mailer
  95. from mailer import Message
  96.  
  97. message = Message(From="me@example.com",
  98. To="you@example.com",
  99. charset="utf-8")
  100. message.Subject = "An HTML Email"
  101. message.Html = """This email uses <strong>HTML</strong>!"""
  102. message.Body = """This is alternate text."""
  103.  
  104. sender = Mailer('smtp.example.com')
  105. sender.send(message)
  106.  
  107. from django.core.mail import EmailMultiAlternatives
  108.  
  109. subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
  110. text_content = 'This is an important message.'
  111. html_content = '<p>This is an <strong>important</strong> message.</p>'
  112. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  113. msg.attach_alternative(html_content, "text/html")
  114. msg.attach_file('/path/to/file.jpg')
  115. msg.send()
  116.  
  117. #GMAIL STUFF
  118. EMAIL_USE_TLS = True
  119. EMAIL_HOST = 'smtp.gmail.com'
  120. EMAIL_HOST_USER = 'name@gmail.com'
  121. EMAIL_HOST_PASSWORD = 'password'
  122. EMAIL_PORT = 587
  123.  
  124. import turbomail
  125.  
  126. # ...
  127.  
  128. message = turbomail.Message("from@example.com", "to@example.com", subject)
  129. message.plain = "Hello world!"
  130.  
  131. turbomail.enqueue(message)
Add Comment
Please, Sign In to add comment