博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
阅读量:2387 次
发布时间:2019-05-10

本文共 5009 字,大约阅读时间需要 16 分钟。

环境:windows ,python3.2.2

文件形式的邮件

[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('你好','plain','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形式的邮件

[python]   

  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邮件

[python]   

  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()  

带附件的邮件

[python]   

  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()  

群邮件

[python]   

  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('你好','plain','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()  

各种元素都包含的邮件

[python]   

  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的邮件

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

  7. receiver = '***'  

  8. subject = 'python email test'  

  9. smtpserver = 'smtp.163.com'  

  10. username = '***'  

  11. password = '***'  

  12.   

  13. msg = MIMEText('你好','plain','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()  

转载地址:http://rlsab.baihongyu.com/

你可能感兴趣的文章
使用ettercap嗅探ssh口令
查看>>
Linux下的内网反弹实例
查看>>
Command execution with a MySQL UDF
查看>>
OTPs: Using s/Key with SSH via OPIE
查看>>
使用arpwatch和arping来排查ARP攻击
查看>>
Linux硬件监控方法
查看>>
RSA SecurID Authentication linux sshd PAM deploy
查看>>
转: pam 禁止某些用户使用ssh 远程登录
查看>>
小包优先+web优先+游戏爆发+单IP限速+连接数限制 脚本V2.0
查看>>
Rhel5 配置NTP服务
查看>>
定制rhel的stage2.img/minstg2.img文件
查看>>
ZZ Quick-Tip: Linux NAT in Four Steps using iptables
查看>>
北京的住房公积金是否可用于还外地的房贷
查看>>
mysqlhotcopy 热备工具体验与总结
查看>>
MooseFS安装笔记
查看>>
GlusterFS分布式集群文件系统安装、配置及性能测试
查看>>
Sakai
查看>>
Adobe ColdFusion Unspecified Directory Traversal Vulnerability
查看>>
Share:A File Checksum Integrity Verifier utility
查看>>
LDAP User Authentication On CentOS 5.x
查看>>