【转】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()  
复制代码

目录
相关文章
|
15天前
|
机器学习/深度学习 自然语言处理 数据可视化
数据代码分享|PYTHON用NLP自然语言处理LSTM神经网络TWITTER推特灾难文本数据、词云可视化
数据代码分享|PYTHON用NLP自然语言处理LSTM神经网络TWITTER推特灾难文本数据、词云可视化
|
2天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
8 1
|
2天前
|
机器学习/深度学习 自然语言处理 算法
Gensim详细介绍和使用:一个Python文本建模库
Gensim详细介绍和使用:一个Python文本建模库
11 1
|
3天前
|
前端开发 文件存储 Python
python之xhtml2pdf: HTML转PDF工具示例详解
python之xhtml2pdf: HTML转PDF工具示例详解
8 0
|
3天前
|
数据采集 数据挖掘 Python
Python之html2text: 将HTML转换为Markdown 文档示例详解
Python之html2text: 将HTML转换为Markdown 文档示例详解
6 0
|
3天前
|
数据采集 Python
Python HTML解析详解
Python HTML解析详解
7 0
|
10天前
|
存储 索引 Python
python图片九宫格图片处理
本篇文章介绍了一个Python项目的实现,项目能够处理图片并将其组合成九宫格或四宫格,同时还具备音乐播放功能,对于初学者来说是一个可以进行实战学习的初级项目。
|
10天前
|
存储 计算机视觉 Python
python实现Gif图片的字符画
这是一个Python实战项目,旨在将GIF动态图转化为ASCII字符动画。项目适合有一定Python基础的学习者,主要使用os、imageio、PIL库。首先,代码导入所需库,然后通过PIL创建空白图片并添加文本。接着,程序读取GIF,拆分帧并转为字符画,存入“tmp”目录。同时,代码提供了清空“tmp”目录、将灰度值映射为ASCII字符、将图片处理成字符画的函数。此外,还有创建新画布和合成GIF的步骤。主函数调用这些模块,最终将ASCII字符画合并成GIF。项目展示了将动态图像转换为ASCII艺术的过程。
|
11天前
|
前端开发 UED 开发者
【专栏:HTML与CSS实战项目篇】制作一个响应式图片画廊
【4月更文挑战第30天】本文介绍了如何使用HTML和CSS创建响应式图片画廊。响应式画廊能根据用户设备屏幕大小自动调整布局。首先规划结构,包含一个图片容器和每张图片元素,并为图片提供替代文本。接着设计样式,设置图片大小、间距和视觉效果。然后通过媒体查询实现响应式设计,根据不同屏幕尺寸调整图片排列。同时考虑性能优化,如压缩图片和使用懒加载技术。最后,测试和调试确保画廊在各种设备上正常工作。这个过程强调了响应式设计和用户体验的重要性。
|
11天前
|
机器学习/深度学习 自然语言处理 算法
【Python机器学习专栏】文本数据的特征提取与表示
【4月更文挑战第30天】本文探讨了文本特征提取与表示在机器学习和NLP中的重要性。介绍了词袋模型、TF-IDF和n-gram等特征提取方法,以及稀疏向量和词嵌入等表示方式。Python中可利用sklearn和gensim库实现这些技术。有效的特征提取与表示有助于将文本数据转化为可处理的数值形式,推动NLP和机器学习领域的进步。