【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)

简介:

特别留意群邮件方式,这是工作中用得多的。

附件,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.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()  
复制代码

目录
打赏
0
0
0
0
83
分享
相关文章
【Html.js——图片折叠效果】折叠手风琴(蓝桥杯真题-1763)【合集】
本项目实现了一个图片折叠手风琴效果,使用jQuery完成。主要包括以下部分: - **介绍**:任务是通过点击图片实现折叠和展开的效果。 - **准备**:内置初始代码,包含 `css/style.css`、`images/` 文件夹、`js/` 文件夹及 `index.html` 等文件。启动 Web Server 服务可运行项目。 - **目标**:完善 `index.js` 文件,使页面达到预期的折叠效果。 - **规定**:严格按步骤操作,保持默认文件结构不变,并在完成后保持 Web 服务正常访问状态。 - **通关代码**:使用 jQuery 实现点击事件,为选中元素添加 `act
57 19
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
98 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
Yagmail邮件发送库:如何用Python实现自动化邮件营销?
本文详细介绍了如何使用Yagmail库实现自动化邮件营销。Yagmail是一个简洁强大的Python库,能简化邮件发送流程,支持文本、HTML邮件及附件发送,适用于数字营销场景。文章涵盖了Yagmail的基本使用、高级功能、案例分析及最佳实践,帮助读者轻松上手。
145 4
|
5月前
利用html2canvas插件自定义生成名片信息并保存图片
这是一个利用html2canvas插件自定义生成名片信息并保存图片,自定义上传头像,自定义输入个人信息内容,自定义图片名称,并将生成的图片保存到本地
97 1
利用html2canvas插件自定义生成名片信息并保存图片
|
5月前
|
使用Python实现自动化邮件通知:当长时程序运行结束时
本文介绍了如何使用Python实现自动化邮件通知功能,当长时间运行的程序完成后自动发送邮件通知。主要内容包括:项目背景、设置SMTP服务、编写邮件发送函数、连接SMTP服务器、发送邮件及异常处理等步骤。通过这些步骤,可以有效提高工作效率,避免长时间等待程序结果。
168 9
|
6月前
HTML图片
【10月更文挑战第4天】HTML图片。
59 2
|
7月前
|
Twaver-HTML5基础学习(37)network导出图片并下载
本文介绍了如何在Twaver-HTML5中将network导出为图片并提供下载,主要通过将network转换为canvas对象,然后转换为base64编码的图片进行展示和下载。
69 5
【前端web入门第一天】02 HTML图片标签 超链接标签 音频标签 视频标签
本文档详细介绍了HTML中的图片、超链接、音频和视频标签的使用方法。首先讲解了`&lt;img&gt;`标签的基本用法及其属性,包括如何使用相对路径和绝对路径。接着介绍了`&lt;a&gt;`标签,用于创建超链接,并展示了如何设置目标页面打开方式。最后,文档还涵盖了如何在网页中嵌入音频和视频文件,包括简化写法及常用属性。
114 13
Python使用urllib或者urllib2模块打开网页遇到ssl报错
Python使用urllib或者urllib2模块打开网页遇到ssl报错
74 0
|
8月前
在线将多张图片拼接起来图工具HTML源码
在线将多张图片拼接成一张图片,多图合一并导出下载。无需本地安装软件。 下载时,使用日期时间作为文件名,规避图片文件名相同造成的覆盖问题;也能省去一部覆盖确认操作 多语言支持
78 0
在线将多张图片拼接起来图工具HTML源码