thorpedosg

Untitled

Jul 24th, 2018
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import smtplib
  2. from email.message import EmailMessage
  3. from email.message import MIMEPart
  4. from email.mime.multipart import MIMEMultipart
  5. from base64 import encodebytes
  6. import mimetypes
  7.  
  8. mSubject = 'Another new family reunion'
  9. sPass = 'mypass'
  10. mServer = "smtp.gmail.com"
  11. mPort = 465
  12.  
  13. filename1 = "file1.jpg"
  14. filename2 = "file2.jpg"
  15. filename3 = "file3.jpg"
  16. image_cid1 = filename1 + "@xxxx1"
  17. image_cid2 = filename2 + "@xxxx2"
  18.  
  19.  
  20. def inline_part(filename, icid):
  21. """ Creates a MIMEMultipart for embedded images """
  22. part = MIMEMultipart('related')
  23.  
  24. fp = open(filename, 'rb')
  25. part.set_payload(encodebytes(fp.read()).decode())
  26. fp.close()
  27. part.add_header('Content-ID', '<' + icid + '>')
  28. part.add_header("Content-Transfer-Encoding", "base64")
  29. return part
  30.  
  31. def attach_part(filename):
  32. """ Creates a MIMEPart for attached images """
  33. part = MIMEPart()
  34. fp = open(filename, 'rb')
  35. part.set_payload(encodebytes(fp.read()).decode())
  36. fp.close()
  37.  
  38. part.add_header("Content-Type", mimetypes.guess_type(filename2)[0] + '; filename="%s"' % filename)
  39. part.add_header("Content-Transfer-Encoding", "base64")
  40. part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename)
  41. return part
  42.  
  43. #set up the mime types
  44. mimetypes.init()
  45.  
  46. # Create the container email message.
  47. msg = EmailMessage()
  48.  
  49. msg['Subject'] = mSubject
  50. msg['From'] = mFrom
  51. msg['To'] = mTo
  52. msg['Bcc'] = mCC
  53.  
  54. body ="""<HTML>
  55. <head></head>
  56. <body>
  57. This is a test email to send an attachment.
  58. This is a linked image:
  59. <div align="center"><p><img src="http://www.freedigitalphotos.net/images/previews/success-road-sign-10031660.jpg"><br></p><p> </p></div>
  60. we will try to add two in-line images as well, named file1.jpg and file2.jpg
  61. """
  62. # add each of the the in-line photo body info
  63. body += ' <p>This is file 1</p>n<p><img id="'+ filename1 + '" src="cid:' + image_cid1 + '" alt="file01.jpg"></p>n'
  64. body += ' <p>This is file 2</p>n<p><img id="'+ filename2 + '" src="cid:' + image_cid2 + '" alt="file02.jpg"></p>n'
  65.  
  66. #add the rest of the body
  67. body +=""" <p>and we have attached one file "file3.jpg"
  68. </body>
  69. </HTML>
  70. """
  71.  
  72. # add the body to the message
  73. msg.add_alternative(body, subtype='html')
  74. msg.set_boundary("===myBoundry")
  75. # attach the in-line files
  76. msg.attach(inline_part(filename1, image_cid1))
  77. msg.attach(inline_part(filename2, image_cid2))
  78. # attach the attachment file
  79. msg.attach(attach_part(filename3))
  80.  
  81. # Open a connection to the mail server, log-on, send the message and disconnect
  82. try:
  83. server = smtplib.SMTP_SSL(mServer, mPort)
  84. server.set_debuglevel(0)
  85. server.login(sLogin, sPass)
  86. server.send_message(msg)
  87. server.quit()
  88. print ("Message sent Successfully")
  89. except Exception as e:
  90. print ("Error sending mail:nt" + str(e))
Add Comment
Please, Sign In to add comment