python发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)

简介:

环境:windows ,python3.2.2

文件形式的邮件

[python]  view plain copy

  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]  view plain copy

  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]  view plain copy

  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]  view plain copy

  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]  view plain copy

  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]  view plain copy

  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]  view plain copy

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










本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1612999,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
前端开发 搜索推荐 编译器
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
138 34
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
|
2月前
|
移动开发 JavaScript 前端开发
【Html.js——图片折叠效果】折叠手风琴(蓝桥杯真题-1763)【合集】
本项目实现了一个图片折叠手风琴效果,使用jQuery完成。主要包括以下部分: - **介绍**:任务是通过点击图片实现折叠和展开的效果。 - **准备**:内置初始代码,包含 `css/style.css`、`images/` 文件夹、`js/` 文件夹及 `index.html` 等文件。启动 Web Server 服务可运行项目。 - **目标**:完善 `index.js` 文件,使页面达到预期的折叠效果。 - **规定**:严格按步骤操作,保持默认文件结构不变,并在完成后保持 Web 服务正常访问状态。 - **通关代码**:使用 jQuery 实现点击事件,为选中元素添加 `act
51 19
|
2月前
|
人工智能 编译器 Python
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
60 0
python已经安装有其他用途如何用hbuilerx配置环境-附带实例demo-python开发入门之hbuilderx编译器如何配置python环境—hbuilderx配置python环境优雅草央千澈
|
3月前
|
移动开发 前端开发 JavaScript
HTML(HyperText Markup Language,超文本标记语言)
HTML(HyperText Markup Language,超文本标记语言)
124 11
|
3月前
|
数据挖掘 vr&ar C++
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
235 5
|
4月前
利用html2canvas插件自定义生成名片信息并保存图片
这是一个利用html2canvas插件自定义生成名片信息并保存图片,自定义上传头像,自定义输入个人信息内容,自定义图片名称,并将生成的图片保存到本地
80 1
利用html2canvas插件自定义生成名片信息并保存图片
|
4月前
|
测试技术 API 数据安全/隐私保护
Python连接到Jira实例、登录、查询、修改和创建bug
通过使用Python和Jira的REST API,可以方便地连接到Jira实例并进行各种操作,包括查询、修改和创建Bug。`jira`库提供了简洁的接口,使得这些操作变得简单易行。无论是自动化测试还是开发工作流的集成,这些方法都可以极大地提高效率和准确性。希望通过本文的介绍,您能够更好地理解和应用这些技术。
400 0
|
10月前
|
API Python
Python邮箱API发送邮件的方法和步骤
使用Python发送邮件涉及导入smtplib和email模块,设置发件人、收件人、主题和内容,然后连接SMTP服务器(如示例中的smtp.example.com)并使用SMTP方法发送。完整代码示例包括异常处理,确保邮件发送成功或提供错误信息。通过这种方式,可以实现Python的自动化邮件发送功能。
|
数据安全/隐私保护 Python
Python 技术篇-连接qq邮箱服务器,调用qq邮箱发送邮件实战演示,qq邮箱授权码开通方法
Python 技术篇-连接qq邮箱服务器,调用qq邮箱发送邮件实战演示,qq邮箱授权码开通方法
683 0
Python 技术篇-连接qq邮箱服务器,调用qq邮箱发送邮件实战演示,qq邮箱授权码开通方法
|
Web App开发 Python
python发送各类邮件的主要方法
python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点。     一、相关模块介绍     发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍:    1、smtplib模块       smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])    SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件)。
1039 0