python 发送邮件实例

简介:

文件形式的邮件

  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.   

  7. sender = '***'  

  8. receiver = '***'  

  9. subject = 'python email test'  

  10. smtpserver = 'smtp.163.com'  

  11. username = '***'  

  12. password = '***'  

  13.   

  14. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  

  15. msg['Subject'] = Header(subject, 'utf-8')  

  16.   

  17. smtp = smtplib.SMTP()  

  18. smtp.connect('smtp.163.com')  

  19. smtp.login(username, password)  

  20. smtp.sendmail(sender, receiver, msg.as_string())  

  21. smtp.quit()  

HTML形式的邮件

  1. #!/usr/bin/env python3  

  2. #coding: utf-8  

  3. import smtplib  

  4. from email.mime.text import MIMEText  

  5.   

  6. sender = '***'  

  7. receiver = '***'  

  8. subject = 'python email test'  

  9. smtpserver = 'smtp.163.com'  

  10. username = '***'  

  11. password = '***'  

  12.   

  13. msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  

  14.   

  15. msg['Subject'] = subject  

  16.   

  17. smtp = smtplib.SMTP()  

  18. smtp.connect('smtp.163.com')  

  19. smtp.login(username, password)  

  20. smtp.sendmail(sender, receiver, msg.as_string())  

  21. smtp.quit()  

带图片的HTML邮件

  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.   

  8. sender = '***'  

  9. receiver = '***'  

  10. subject = 'python email test'  

  11. smtpserver = 'smtp.163.com'  

  12. username = '***'  

  13. password = '***'  

  14.   

  15. msgRoot = MIMEMultipart('related')  

  16. msgRoot['Subject'] = 'test message'  

  17.   

  18. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')  

  19. msgRoot.attach(msgText)  

  20.   

  21. fp = open('h:\\python\\1.jpg''rb')  

  22. msgImage = MIMEImage(fp.read())  

  23. fp.close()  

  24.   

  25. msgImage.add_header('Content-ID''<image1>')  

  26. msgRoot.attach(msgImage)  

  27.   

  28. smtp = smtplib.SMTP()  

  29. smtp.connect('smtp.163.com')  

  30. smtp.login(username, password)  

  31. smtp.sendmail(sender, receiver, msgRoot.as_string())  

  32. smtp.quit()  

带附件的邮件

  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.   

  8. sender = '***'  

  9. receiver = '***'  

  10. subject = 'python email test'  

  11. smtpserver = 'smtp.163.com'  

  12. username = '***'  

  13. password = '***'  

  14.   

  15. msgRoot = MIMEMultipart('related')  

  16. msgRoot['Subject'] = 'test message'  

  17.   

  18. #构造附件  

  19. att = MIMEText(open('h:\\python\\1.jpg''rb').read(), 'base64''utf-8')  

  20. att["Content-Type"] = 'application/octet-stream'  

  21. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  

  22. msgRoot.attach(att)  

  23.           

  24. smtp = smtplib.SMTP()  

  25. smtp.connect('smtp.163.com')  

  26. smtp.login(username, password)  

  27. smtp.sendmail(sender, receiver, msgRoot.as_string())  

  28. smtp.quit()  

群邮件

  1. #!/usr/bin/env python3  

  2. #coding: utf-8  

  3. import smtplib  

  4. from email.mime.text import MIMEText  

  5.   

  6. sender = '***'  

  7. receiver = ['***','****',……]  

  8. subject = 'python email test'  

  9. smtpserver = 'smtp.163.com'  

  10. username = '***'  

  11. password = '***'  

  12.   

  13. msg = MIMEText('你好','text','utf-8')  

  14.   

  15. msg['Subject'] = subject  

  16.   

  17. smtp = smtplib.SMTP()  

  18. smtp.connect('smtp.163.com')  

  19. smtp.login(username, password)  

  20. smtp.sendmail(sender, receiver, msg.as_string())  

  21. smtp.quit()  

各种元素都包含的邮件

  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.   

  8. sender = '***'  

  9. receiver = '***'  

  10. subject = 'python email test'  

  11. smtpserver = 'smtp.163.com'  

  12. username = '***'  

  13. password = '***'  

  14.   

  15. # Create message container - the correct MIME type is multipart/alternative.  

  16. msg = MIMEMultipart('alternative')  

  17. msg['Subject'] = "Link"  

  18.   

  19. # Create the body of the message (a plain-text and an HTML version).  

  20. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"  

  21. html = """\ 

  22. <html> 

  23.   <head></head> 

  24.   <body> 

  25.     <p>Hi!<br> 

  26.        How are you?<br> 

  27.        Here is the <a href="http://www.python.org">link</a> you wanted. 

  28.     </p> 

  29.   </body> 

  30. </html> 

  31. """  

  32.   

  33. # Record the MIME types of both parts - text/plain and text/html.  

  34. part1 = MIMEText(text, 'plain')  

  35. part2 = MIMEText(html, 'html')  

  36.   

  37. # Attach parts into message container.  

  38. # According to RFC 2046, the last part of a multipart message, in this case  

  39. # the HTML message, is best and preferred.  

  40. msg.attach(part1)  

  41. msg.attach(part2)  

  42. #构造附件  

  43. att = MIMEText(open('h:\\python\\1.jpg''rb').read(), 'base64''utf-8')  

  44. att["Content-Type"] = 'application/octet-stream'  

  45. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  

  46. msg.attach(att)  

  47.      

  48. smtp = smtplib.SMTP()  

  49. smtp.connect('smtp.163.com')  

  50. smtp.login(username, password)  

  51. smtp.sendmail(sender, receiver, msg.as_string())  

  52. smtp.quit()  

基于SSL的邮件

  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. receiver = '***'  

  8. subject = 'python email test'  

  9. smtpserver = 'smtp.163.com'  

  10. username = '***'  

  11. password = '***'  

  12.   

  13. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  

  14. msg['Subject'] = Header(subject, 'utf-8')  

  15.   

  16. smtp = smtplib.SMTP()  

  17. smtp.connect('smtp.163.com')  

  18. smtp.ehlo()  

  19. smtp.starttls()  

  20. smtp.ehlo()  

  21. smtp.set_debuglevel(1)  

  22. smtp.login(username, password)  

  23. smtp.sendmail(sender, receiver, msg.as_string())  

  24. smtp.quit()  

    文件形式的邮件

    HTML形式的邮件

    带图片的HTML邮件

    带附件的邮件

    群邮件

    各种元素都包含的邮件

    基于SSL的邮件

    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. receiver = '***'  

    8. subject = 'python email test'  

    9. smtpserver = 'smtp.163.com'  

    10. username = '***'  

    11. password = '***'  

    12.   

    13. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  

    14. msg['Subject'] = Header(subject, 'utf-8')  

    15.   

    16. smtp = smtplib.SMTP()  

    17. smtp.connect('smtp.163.com')  

    18. smtp.ehlo()  

    19. smtp.starttls()  

    20. smtp.ehlo()  

    21. smtp.set_debuglevel(1)  

    22. smtp.login(username, password)  

    23. smtp.sendmail(sender, receiver, msg.as_string())  

    24. smtp.quit()  

    25. #!/usr/bin/env python3  

    26. #coding: utf-8  

    27. import smtplib  

    28. from email.mime.multipart import MIMEMultipart  

    29. from email.mime.text import MIMEText  

    30. from email.mime.image import MIMEImage  

    31.   

    32. sender = '***'  

    33. receiver = '***'  

    34. subject = 'python email test'  

    35. smtpserver = 'smtp.163.com'  

    36. username = '***'  

    37. password = '***'  

    38.   

    39. # Create message container - the correct MIME type is multipart/alternative.  

    40. msg = MIMEMultipart('alternative')  

    41. msg['Subject'] = "Link"  

    42.   

    43. # Create the body of the message (a plain-text and an HTML version).  

    44. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"  

    45. html = """\ 

    46. <html> 

    47.   <head></head> 

    48.   <body> 

    49.     <p>Hi!<br> 

    50.        How are you?<br> 

    51.        Here is the <a href="http://www.python.org">link</a> you wanted. 

    52.     </p> 

    53.   </body> 

    54. </html> 

    55. """  

    56.   

    57. # Record the MIME types of both parts - text/plain and text/html.  

    58. part1 = MIMEText(text, 'plain')  

    59. part2 = MIMEText(html, 'html')  

    60.   

    61. # Attach parts into message container.  

    62. # According to RFC 2046, the last part of a multipart message, in this case  

    63. # the HTML message, is best and preferred.  

    64. msg.attach(part1)  

    65. msg.attach(part2)  

    66. #构造附件  

    67. att = MIMEText(open('h:\\python\\1.jpg''rb').read(), 'base64''utf-8')  

    68. att["Content-Type"] = 'application/octet-stream'  

    69. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  

    70. msg.attach(att)  

    71.      

    72. smtp = smtplib.SMTP()  

    73. smtp.connect('smtp.163.com')  

    74. smtp.login(username, password)  

    75. smtp.sendmail(sender, receiver, msg.as_string())  

    76. smtp.quit()  

    77. #!/usr/bin/env python3  

    78. #coding: utf-8  

    79. import smtplib  

    80. from email.mime.text import MIMEText  

    81.   

    82. sender = '***'  

    83. receiver = ['***','****',……]  

    84. subject = 'python email test'  

    85. smtpserver = 'smtp.163.com'  

    86. username = '***'  

    87. password = '***'  

    88.   

    89. msg = MIMEText('你好','text','utf-8')  

    90.   

    91. msg['Subject'] = subject  

    92.   

    93. smtp = smtplib.SMTP()  

    94. smtp.connect('smtp.163.com')  

    95. smtp.login(username, password)  

    96. smtp.sendmail(sender, receiver, msg.as_string())  

    97. smtp.quit()  

    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.   

    8. sender = '***'  

    9. receiver = '***'  

    10. subject = 'python email test'  

    11. smtpserver = 'smtp.163.com'  

    12. username = '***'  

    13. password = '***'  

    14.   

    15. msgRoot = MIMEMultipart('related')  

    16. msgRoot['Subject'] = 'test message'  

    17.   

    18. #构造附件  

    19. att = MIMEText(open('h:\\python\\1.jpg''rb').read(), 'base64''utf-8')  

    20. att["Content-Type"] = 'application/octet-stream'  

    21. att["Content-Disposition"] = 'attachment; filename="1.jpg"'  

    22. msgRoot.attach(att)  

    23.           

    24. smtp = smtplib.SMTP()  

    25. smtp.connect('smtp.163.com')  

    26. smtp.login(username, password)  

    27. smtp.sendmail(sender, receiver, msgRoot.as_string())  

    28. smtp.quit()  

    29. #!/usr/bin/env python3  

    30. #coding: utf-8  

    31. import smtplib  

    32. from email.mime.multipart import MIMEMultipart  

    33. from email.mime.text import MIMEText  

    34. from email.mime.image import MIMEImage  

    35.   

    36. sender = '***'  

    37. receiver = '***'  

    38. subject = 'python email test'  

    39. smtpserver = 'smtp.163.com'  

    40. username = '***'  

    41. password = '***'  

    42.   

    43. msgRoot = MIMEMultipart('related')  

    44. msgRoot['Subject'] = 'test message'  

    45.   

    46. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')  

    47. msgRoot.attach(msgText)  

    48.   

    49. fp = open('h:\\python\\1.jpg''rb')  

    50. msgImage = MIMEImage(fp.read())  

    51. fp.close()  

    52.   

    53. msgImage.add_header('Content-ID''<image1>')  

    54. msgRoot.attach(msgImage)  

    55.   

    56. smtp = smtplib.SMTP()  

    57. smtp.connect('smtp.163.com')  

    58. smtp.login(username, password)  

    59. smtp.sendmail(sender, receiver, msgRoot.as_string())  

    60. smtp.quit()  

    1. #!/usr/bin/env python3  

    2. #coding: utf-8  

    3. import smtplib  

    4. from email.mime.text import MIMEText  

    5.   

    6. sender = '***'  

    7. receiver = '***'  

    8. subject = 'python email test'  

    9. smtpserver = 'smtp.163.com'  

    10. username = '***'  

    11. password = '***'  

    12.   

    13. msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  

    14.   

    15. msg['Subject'] = subject  

    16.   

    17. smtp = smtplib.SMTP()  

    18. smtp.connect('smtp.163.com')  

    19. smtp.login(username, password)  

    20. smtp.sendmail(sender, receiver, msg.as_string())  

    21. smtp.quit()  

    22. #!/usr/bin/env python3  

    23. #coding: utf-8  

    24. import smtplib  

    25. from email.mime.text import MIMEText  

    26. from email.header import Header  

    27.   

    28. sender = '***'  

    29. receiver = '***'  

    30. subject = 'python email test'  

    31. smtpserver = 'smtp.163.com'  

    32. username = '***'  

    33. password = '***'  

    34.   

    35. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  

    36. msg['Subject'] = Header(subject, 'utf-8')  

    37.   

    38. smtp = smtplib.SMTP()  

    39. smtp.connect('smtp.163.com')  

    40. smtp.login(username, password)  

    41. smtp.sendmail(sender, receiver, msg.as_string())  

    42. smtp.quit()  










本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1693201,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
测试技术 Python
Python中的装饰器:概念、用法及实例
【2月更文挑战第25天】 装饰器在Python中是一种强大的工具,它允许我们在不修改原始函数代码的情况下,增加函数的功能。本文将深入探讨装饰器的概念,解析其工作原理,并通过实例来展示如何在Python中使用装饰器。
|
1月前
|
存储 缓存 Python
Python中的装饰器:概念、用法和实例
【2月更文挑战第25天】 在Python编程中,装饰器是一种强大的工具,它允许我们在不修改原始函数代码的情况下,增加或修改函数的行为。本文将深入探讨装饰器的概念、用法以及通过实例来使其更易于理解。我们将看到,装饰器不仅仅是语法糖,而是一种可以极大提高代码复用性和可读性的有效工具。
|
1月前
|
Python
Python 中的装饰器:概念、用法和实例
【2月更文挑战第23天】 在编程世界中,装饰器是一种强大的工具,它允许我们在不改变现有代码的情况下增加或修改函数和类的行为。本文将深入探讨 Python 中装饰器的概念、用法以及如何创建自定义装饰器。我们将通过实例来演示装饰器的实用性和灵活性,帮助读者更好地理解和应用这一技术。
|
1月前
|
缓存 开发者 Python
Python中的装饰器:概念、应用与实例
【2月更文挑战第16天】 在编程的海洋中,Python以其清晰的语法和强大的功能成为众多开发者的宠儿。其中,装饰器作为Python的一项重要特性,不仅增强了代码的可读性和重用性,还为函数和方法提供了一种灵活的扩展方式。本文将深入探讨装饰器的核心概念、常见应用场景以及通过具体实例来揭示其魔法般的工作原理。无论你是Python新手还是经验丰富的开发者,本文都将为你打开装饰器的大门,带你领略其在程序设计中的独到魅力。
|
1月前
|
机器学习/深度学习 人工智能 数据可视化
Python在数据分析中的威力及应用实例
本文探讨了Python在数据分析领域的重要性和灵活性,着重介绍了Python在大数据处理、机器学习和可视化方面的应用实例,展示了其强大的功能和便捷的特点。
|
1月前
|
测试技术 开发者 Python
Python中的装饰器:概念、应用与实例
【2月更文挑战第22天】 本文深入探讨了Python中的一个重要特性——装饰器。我们将详细解析装饰器的概念,阐述其在Python编程中的应用,并通过具体的代码实例来展示其使用方法和效果。无论你是Python初学者还是有经验的开发者,本文都将帮助你更好地理解和使用装饰器。
16 0
|
8天前
|
存储 机器学习/深度学习 数据可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
56 6
|
8天前
|
机器学习/深度学习 数据可视化 算法
PYTHON用决策树分类预测糖尿病和可视化实例
PYTHON用决策树分类预测糖尿病和可视化实例
17 0
|
9天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
13 0
|
11天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象