Python mail instance

Source: Internet
Author: User

File-form emails

Link: http://blog.csdn.net/wyuan8913/article/details/6917873

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. Text import mimetext
  5. From email. header import Header
  6. Sender = '***'
  7. Else ER = '***'
  8. Subject = 'python email Test'
  9. Smtpserver = 'smtp .163.com'
  10. Username = '***'
  11. Password = '***'
  12. MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
  13. MSG ['subobject'] = header (subject, 'utf-8 ')
  14. SMTP = smtplib. SMTP ()
  15. SMTP. Connect ('smtp .163.com ')
  16. SMTP. login (username, password)
  17. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  18. SMTP. Quit ()

HTML emails

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. Text import mimetext
  5. Sender = '***'
  6. Else ER = '***'
  7. Subject = 'python email Test'
  8. Smtpserver = 'smtp .163.com'
  9. Username = '***'
  10. Password = '***'
  11. MSG = mimetext ('<HTML>
  12. MSG ['subobject'] = subject
  13. SMTP = smtplib. SMTP ()
  14. SMTP. Connect ('smtp .163.com ')
  15. SMTP. login (username, password)
  16. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  17. SMTP. Quit ()

HTML emails with images

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. multipart import mimemultipart
  5. From email. Mime. Text import mimetext
  6. From email. Mime. Image import mimeimage
  7. Sender = '***'
  8. Else ER = '***'
  9. Subject = 'python email Test'
  10. Smtpserver = 'smtp .163.com'
  11. Username = '***'
  12. Password = '***'
  13. Msgroot = mimemultipart ('related ')
  14. Msgroot ['subobject'] = 'test message'
  15. Msgtext = mimetext ('<B> some <I> HTML </I> text </B> and an image. <br> <br> good! ', 'Html', 'utf-8 ')
  16. Msgroot. Attach (msgtext)
  17. Fp = open ('H: \ Python \ 1.jpg ', 'rb ')
  18. Msgimage = mimeimage (FP. Read ())
  19. FP. Close ()
  20. Msgimage. add_header ('content-id', '<image1> ')
  21. Msgroot. Attach (msgimage)
  22. SMTP = smtplib. SMTP ()
  23. SMTP. Connect ('smtp .163.com ')
  24. SMTP. login (username, password)
  25. SMTP. Sendmail (sender, explorer, msgroot. as_string ())
  26. SMTP. Quit ()

Emails with attachments

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. multipart import mimemultipart
  5. From email. Mime. Text import mimetext
  6. From email. Mime. Image import mimeimage
  7. Sender = '***'
  8. Else ER = '***'
  9. Subject = 'python email Test'
  10. Smtpserver = 'smtp .163.com'
  11. Username = '***'
  12. Password = '***'
  13. Msgroot = mimemultipart ('related ')
  14. Msgroot ['subobject'] = 'test message'
  15. # Construct an attachment
  16. ATT = mimetext (open ('H: \ Python \ 1.jpg ', 'rb'). Read (), 'base64', 'utf-8 ')
  17. ATT ["Content-Type"] = 'application/octet-stream'
  18. ATT ["content-disposition"] = 'attachment; filename = "1.jpg "'
  19. Msgroot. Attach (ATT)
  20. SMTP = smtplib. SMTP ()
  21. SMTP. Connect ('smtp .163.com ')
  22. SMTP. login (username, password)
  23. SMTP. Sendmail (sender, explorer, msgroot. as_string ())
  24. SMTP. Quit ()

Group email

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. Text import mimetext
  5. Sender = '***'
  6. Else ER = ['***', '*****',…]
  7. Subject = 'python email Test'
  8. Smtpserver = 'smtp .163.com'
  9. Username = '***'
  10. Password = '***'
  11. MSG = mimetext ('hello', 'plain ', 'utf-8 ')
  12. MSG ['subobject'] = subject
  13. SMTP = smtplib. SMTP ()
  14. SMTP. Connect ('smtp .163.com ')
  15. SMTP. login (username, password)
  16. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  17. SMTP. Quit ()

Emails containing various elements

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. multipart import mimemultipart
  5. From email. Mime. Text import mimetext
  6. From email. Mime. Image import mimeimage
  7. Sender = '***'
  8. Else ER = '***'
  9. Subject = 'python email Test'
  10. Smtpserver = 'smtp .163.com'
  11. Username = '***'
  12. Password = '***'
  13. # Create message container-the correct MIME type is multipart/alternative.
  14. MSG = mimemultipart ('alternative ')
  15. MSG ['subobject'] = "Link"
  16. # Create the body of the message (a plain-text and an HTML Version ).
  17. TEXT = "Hi! \ Nhow are you? \ Nhere is the link you wanted: \ nhttp: // www.python.org"
  18. Html = """\
  19. <HTML>
  20. <Head>
  21. <Body>
  22. <P> Hi! <Br>
  23. How are you? <Br>
  24. Here is the <a href = "http://www.python.org"> link </a> you wanted.
  25. </P>
  26. </Body>
  27. </Html>
  28. """
  29. # Record the MIME types of both parts-text/plain and text/html.
  30. Part1 = mimetext (text, 'plain ')
  31. Part2 = mimetext (HTML, 'html ')
  32. # Attach parts into message container.
  33. # According to RFC 2046, the last part of a multipart message, in this case
  34. # The HTML message, is best and preferred.
  35. MSG. Attach (Part1)
  36. MSG. Attach (Part2)
  37. # Construct an attachment
  38. ATT = mimetext (open ('H: \ Python \ 1.jpg ', 'rb'). Read (), 'base64', 'utf-8 ')
  39. ATT ["Content-Type"] = 'application/octet-stream'
  40. ATT ["content-disposition"] = 'attachment; filename = "1.jpg "'
  41. MSG. Attach (ATT)
  42. SMTP = smtplib. SMTP ()
  43. SMTP. Connect ('smtp .163.com ')
  44. SMTP. login (username, password)
  45. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  46. SMTP. Quit ()

SSL-based Mail

[Python]
View plaincopy
  1. #! /Usr/bin/ENV python3
  2. # Coding: UTF-8
  3. Import smtplib
  4. From email. Mime. Text import mimetext
  5. From email. header import Header
  6. Sender = '***'
  7. Else ER = '***'
  8. Subject = 'python email Test'
  9. Smtpserver = 'smtp .163.com'
  10. Username = '***'
  11. Password = '***'
  12. MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
  13. MSG ['subobject'] = header (subject, 'utf-8 ')
  14. SMTP = smtplib. SMTP ()
  15. SMTP. Connect ('smtp .163.com ')
  16. SMTP. EHLO ()
  17. SMTP. starttls ()
  18. SMTP. EHLO ()
  19. SMTP. set_debuglevel (1)
  20. SMTP. login (username, password)
  21. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  22. SMTP. Quit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.