这里写链接内容
可采用email模块发送电子邮件附件。发送一个未知MIME类型的文件附件其基本思路如下:
1. 构造MIMEMultipart对象做为根容器
2. 构造MIMEText对象做为邮件显示内容并附加到根容器
3. 构造MIMEBase对象做为文件附件内容并附加到根容器
a. 读入文件内容并格式化
b. 设置附件头
4. 设置根容器属性
5. 得到格式化后的完整文本
6. 用smtp发送邮件
具体内容参见<programing python(3rd)> 14章第6节 “email: Parsing and Composing Mails”。发送一个未知MIME类型的文件附件实例代码如下:
[python]view plain copy print?importsmtplibimportemail.MIMEMultipartimportemail.MIMETextimportemail.MIMEBaseimportos.pathFrom=”senderaddress”To=”recipients”file_name=”filename”server=smtplib.SMTP(”smtpserveraddress”)server.login(”username”,“password”)#仅smtp服务器需要验证时#构造MIMEMultipart对象做为根容器main_msg=email.MIMEMultipart.MIMEMultipart()#构造MIMEText对象做为邮件显示内容并附加到根容器text_msg=email.MIMEText.MIMEText(”thisisatesttexttotextmime”)main_msg.attach(text_msg)#构造MIMEBase对象做为文件附件内容并附加到根容器contype=’application/octet-stream’maintype,subtype=contype.split(’/’,1)##读入文件内容并格式化data=open(file_name,’rb’)file_msg=email.MIMEBase.MIMEBase(maintype,subtype)file_msg.set_payload(data.read())data.close()email.Encoders.encode_base64(file_msg)##设置附件头basename=os.path.basename(file_name)file_msg.add_header(’Content-Disposition’,’attachment’,filename=basename)main_msg.attach(file_msg)#设置根容器属性main_msg[’From’]=Frommain_msg[’To’]=Tomain_msg[’Subject’]=“attachtest”main_msg[’Date’]=email.Utils.formatdate()#得到格式化后的完整文本fullText=main_msg.as_string()#用smtp发送邮件try:server.sendmail(From,To,fullText)finally:server.quit()
import email.MIMEMultipart import email.MIMEText import email.MIMEBase import os.pathimport smtplib
From = “sender address”
To = “recipients”
file_name = “file name”
server = smtplib.SMTP(“smtp server address”)
server.login(“username”,”password”) #仅smtp服务器需要验证时
构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("this is a test text to text mime")
main_msg.attach(text_msg)
构造MIMEBase对象做为文件附件内容并附加到根容器
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
读入文件内容并格式化
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
email.Encoders.encode_base64(file_msg)
设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition',
'attachment', filename = basename)
main_msg.attach(file_msg)
设置根容器属性
main_msg['From'] = From
main_msg['To'] = To
main_msg['Subject'] = "attach test "
main_msg['Date'] = email.Utils.formatdate( )
得到格式化后的完整文本
fullText = main_msg.as_string( )
用smtp发送邮件
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
以上内容转载于:/alaska1131/articles/1852653.html
网上关于python的例子所涉及的知识比较烦杂,本着学习Python的心态,掌握发送邮件为知识点,并对代码进行比较分析,对发送邮件相关知识点逐步了解。以下是三段发送邮件代码,均测试成功。
代码一
[python]view plain copy print?#-*-coding:utf-8-*-importsmtplibimportemail.MIMEMultipart#importMIMEMultipartimportemail.MIMEText#importMIMETextimportemail.MIMEBase#importMIMEBaseimportos.pathimportmimetypesFrom=”发送邮箱”To=”接收箱箱”file_name=”c:/1.png”#附件名server=smtplib.SMTP(””)server.login(”username”,“password”)#仅smtp服务器需要验证时#构造MIMEMultipart对象做为根容器main_msg=email.MIMEMultipart.MIMEMultipart()#构造MIMEText对象做为邮件显示内容并附加到根容器text_msg=email.MIMEText.MIMEText(”我thisisatesttexttotextmime”,_charset=“utf-8”)main_msg.attach(text_msg)#构造MIMEBase对象做为文件附件内容并附加到根容器##读入文件内容并格式化[方式1]------------------------------data=open(file_name,’rb’)ctype,encoding=mimetypes.guess_type(file_name)ifctypeisNoneorencodingisnotNone:ctype=’application/octet-stream’maintype,subtype=ctype.split(’/’,1)file_msg=email.MIMEBase.MIMEBase(maintype,subtype)file_msg.set_payload(data.read())data.close()email.Encoders.encode_base64(file_msg)#把附件编码””’测试识别文件类型:mimetypes.guess_type(file_name)rar文件ctype,encoding值:NoneNone(ini文件、csv文件、apk文件)txttext/plainNonepytext/x-pythonNonegifimage/gifNonepngimage/x-pngNonejpgimage/pjpegNonepdfapplication/pdfNonedocapplication/mswordNonezipapplication/x-zip-compressedNoneencoding值在什么情况下不是None呢?以后有结果补充。”’#---------------------------------------------##设置附件头basename=os.path.basename(file_name)file_msg.add_header(’Content-Disposition’,‘attachment’,filename=basename)#修改邮件头main_msg.attach(file_msg)#设置根容器属性main_msg[’From’]=Frommain_msg[’To’]=Tomain_msg[’Subject’]=“attachtest”main_msg[’Date’]=email.Utils.formatdate()#得到格式化后的完整文本fullText=main_msg.as_string()#用smtp发送邮件try:server.sendmail(From,To,fullText)finally:server.quit()
# -*- coding: utf-8 -*-import smtplibimport email.MIMEMultipart# import MIMEMultipartimport email.MIMEText# import MIMETextimport email.MIMEBase# import MIMEBaseimport os.pathimport mimetypesFrom = "发送邮箱"To = "接收箱箱"file_name = "c:/1.png"#附件名server = smtplib.SMTP("")server.login("username","password") #仅smtp服务器需要验证时
构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("我this is a test text to text mime",_charset="utf-8")
main_msg.attach(text_msg)
构造MIMEBase对象做为文件附件内容并附加到根容器
读入文件内容并格式化 [方式1]------------------------------
data = open(file_name, 'rb')
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close( )
email.Encoders.encode_base64(file_msg)#把附件编码
'''
测试识别文件类型:mimetypes.guess_type(file_name)
rar 文件 ctype,encoding值:None None(ini文件、csv文件、apk文件)
txt text/plain None
py text/x-python None
gif image/gif None
png image/x-png None
jpg image/pjpeg None
pdf application/pdf None
doc application/msword None
zip application/x-zip-compressed None
encoding值在什么情况下不是None呢?以后有结果补充。
'''
---------------------------------------------
设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
main_msg.attach(file_msg)
设置根容器属性
main_msg['From'] = From
main_msg['To'] = To
main_msg['Subject'] = "attach test "
main_msg['Date'] = email.Utils.formatdate( )
得到格式化后的完整文本
fullText = main_msg.as_string( )
用smtp发送邮件
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
代码二
[python]view plain copy print?#-*-coding:utf-8-*-importsmtplibimportemail.MIMEMultipart#importMIMEMultipartimportemail.MIMEText#importMIMETextimportemail.MIMEBase#importMIMEBaseimportos.pathFrom=”发送邮箱”To=”接收箱箱”file_name=r”c:/1.png”#附件名server=smtplib.SMTP(””)server.login(”username”,“password”)#仅smtp服务器需要验证时#构造MIMEMultipart对象做为根容器main_msg=email.MIMEMultipart.MIMEMultipart()#构造MIMEText对象做为邮件显示内容并附加到根容器text_msg=email.MIMEText.MIMEText(”我thisisatesttexttotextmime”,_charset=“utf-8”)main_msg.attach(text_msg)#构造MIMEBase对象做为文件附件内容并附加到根容器contype=’application/octet-stream’maintype,subtype=contype.split(’/’,1)##读入文件内容并格式化[方式2]------------------------------data=open(file_name,’rb’)file_msg=email.MIMEBase.MIMEBase(maintype,subtype)file_msg.set_payload(data.read())data.close()email.Encoders.encode_base64(file_msg)#把附件编码#---------------------------------------------##设置附件头basename=os.path.basename(file_name)file_msg.add_header(’Content-Disposition’,‘attachment’,filename=basename)#修改邮件头main_msg.attach(file_msg)#设置根容器属性main_msg[’From’]=Frommain_msg[’To’]=Tomain_msg[’Subject’]=“attachtest”main_msg[’Date’]=email.Utils.formatdate()#得到格式化后的完整文本fullText=main_msg.as_string()#用smtp发送邮件try:server.sendmail(From,To,fullText)finally:server.quit()
# -*- coding: utf-8 -*-import smtplibimport email.MIMEMultipart# import MIMEMultipartimport email.MIMEText# import MIMETextimport email.MIMEBase# import MIMEBaseimport os.pathFrom = "发送邮箱"To = "接收箱箱"file_name = r"c:/1.png"#附件名server = smtplib.SMTP("")server.login("username","password") #仅smtp服务器需要验证时
构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("我this is a test text to text mime",_charset="utf-8")
main_msg.attach(text_msg)
构造MIMEBase对象做为文件附件内容并附加到根容器
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
读入文件内容并格式化 [方式2]------------------------------
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close( )
email.Encoders.encode_base64(file_msg)#把附件编码
---------------------------------------------
设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
main_msg.attach(file_msg)
设置根容器属性
main_msg['From'] = From
main_msg['To'] = To
main_msg['Subject'] = "attach test "
main_msg['Date'] = email.Utils.formatdate( )
得到格式化后的完整文本
fullText = main_msg.as_string( )
用smtp发送邮件
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
代码三
[python]view plain copy print?#-*-coding:utf-8-*-importsmtplibimportemail.MIMEMultipart#importMIMEMultipartimportemail.MIMEText#importMIMETextimportemail.MIMEBase#importMIMEBaseimportos.pathimportmimetypesimportemail.MIMEImage#importMIMEImageFrom=”发送邮箱”To=”接收箱箱”file_name=”c:/1.png”#附件名server=smtplib.SMTP(””)server.login(”username”,“password”)#仅smtp服务器需要验证时#构造MIMEMultipart对象做为根容器main_msg=email.MIMEMultipart.MIMEMultipart()#构造MIMEText对象做为邮件显示内容并附加到根容器text_msg=email.MIMEText.MIMEText(”我thisisatesttexttotextmime”,_charset=“utf-8”)main_msg.attach(text_msg)#构造MIMEBase对象做为文件附件内容并附加到根容器ctype,encoding=mimetypes.guess_type(file_name)ifctypeisNoneorencodingisnotNone:ctype=’application/octet-stream’maintype,subtype=ctype.split(’/’,1)file_msg=email.MIMEImage.MIMEImage(open(file_name,’rb’).read(),subtype)printctype,encoding##设置附件头basename=os.path.basename(file_name)file_msg.add_header(’Content-Disposition’,‘attachment’,filename=basename)#修改邮件头main_msg.attach(file_msg)#设置根容器属性main_msg[’From’]=Frommain_msg[’To’]=Tomain_msg[’Subject’]=“attachtest”main_msg[’Date’]=email.Utils.formatdate()#得到格式化后的完整文本fullText=main_msg.as_string()#用smtp发送邮件try:server.sendmail(From,To,fullText)finally:server.quit()
# -*- coding: utf-8 -*-import smtplibimport email.MIMEMultipart# import MIMEMultipartimport email.MIMEText# import MIMETextimport email.MIMEBase# import MIMEBaseimport os.pathimport mimetypesimport email.MIMEImage# import MIMEImageFrom = "发送邮箱"To = "接收箱箱"file_name = "c:/1.png"#附件名server = smtplib.SMTP("")server.login("username","password") #仅smtp服务器需要验证时
构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("我this is a test text to text mime",_charset="utf-8")
main_msg.attach(text_msg)
构造MIMEBase对象做为文件附件内容并附加到根容器
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg=email.MIMEImage.MIMEImage(open(file_name,'rb').read(),subtype)
print ctype,encoding
设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
main_msg.attach(file_msg)
设置根容器属性
main_msg['From'] = From
main_msg['To'] = To
main_msg['Subject'] = "attach test "
main_msg['Date'] = email.Utils.formatdate( )
得到格式化后的完整文本
fullText = main_msg.as_string( )
用smtp发送邮件
try:
server.sendmail(From, To, fullText)
finally:
server.quit()
相关知识介绍
mailMsg.set_payload(safe_unicode(bodyText).encode(charset), charset=none)
set_payload的参数说明:
参数1:安全二进制流,即文件数据
参数2:编码,默认为none
详情:/codes/search/email.MIMEBase.MIMEBase.set_payload
知识点汇总:
如果用户A通过B邮箱来向C发送邮件。那么C收到邮件后,如果回复的话,回复的邮件地址是B邮箱。如果想要回复的邮箱是A邮箱,那么就要设置回复邮件的地址,通过python如何设置呢?设置的方法如下:
[python]view plain copy print?main_msg[‘Reply-to’]=‘对方回复的邮箱地址’
main_msg['Reply-to'] = '对方回复的邮箱地址'
2、对方收到邮件后显示发送邮件方的名称
一般情况下,采用这样的代码:
[python]view plain copy print?main_msg[‘From’]=‘10000@’
main_msg['From'] = '10000@'
对方看到的是该邮箱。如果还想显示用户名,采用如下代码:
[python]view plain copy print?main_msg[‘From’]=“大头爸爸<10000@>”
main_msg['From']="大头爸爸<10000@>"
这样,收到邮件的人就会在邮箱收件人一栏中看到:大头爸爸<100000@>,而不仅仅显示邮箱的地址。
3、向多人发送邮件
1、一个情况是,如果我向多人发送邮件,但同时我不希望收到邮件的人知道我发给的人。这里就不能设置main_msg[‘To’],也不能设置main_msg[‘Cc’](抄送),因为用这两种方式发送,收件人获取邮件时,会列出所有收取该邮件的邮箱。这里只有采用密件的方式发送。即设置main_msg[‘Bcc’]。
2、向多人发送邮件,一定要注意:
[python]view plain copy print?#用smtp发送邮件try:server.sendmail(From,To.split(’;’),fullText)finally:server.quit()
# 用smtp发送邮件try:server.sendmail(From, To.split(';'), fullText)finally:server.quit()
sendmail中的那个To是list,不是string,否则只能发给第一个人。因为我传递的多个接收邮件的地址是用半角分号(;)分隔,所以我在这里转为list。
但是设置main_msg[‘Bcc’]时,他的值不能为list,而应该是string。如代码:
[python]view plain copy print?main_msg[‘Bcc’]=To
main_msg['Bcc']=To
经过一翻测试,不设置main_msg[‘Bcc’]值也正常,且符合预期,即收件人在访问邮件时,收件人地址为空。如下图所示:
3、一个奇怪的乱码问题
因为我写的脚本编码保存为utf-8。通过QQ企业邮箱发送给网易邮箱时,邮件内容都很正常,如下图所示:
只是当收到邮件的用户回复时,出现部分内容乱码现象。如下图所示:
python邮件发送代码:
[python]view plain copy print?#-*-coding:utf-8-*-importsmtplibimportemail.MIMEMultipart#importMIMEMultipartimportemail.MIMEText#importMIMETextimportemail.MIMEBase#importMIMEBaseimportos.pathimportsysimportmimetypesimportemail.MIMEImage#importMIMEImage#命令mail.py<1:发送方(回复地址)10000@><2:发送地址,多个以;隔开><3:发送文件>From=”合肥顶击科技<itop_business@5186.me>”ReplyTo=sys.argv[1]To=sys.argv[2]file_name=sys.argv[3]#附件名server=smtplib.SMTP(”smtp.”,25)server.login(”itop_business@5186.me”,“xxxxxxx”)#仅smtp服务器需要验证时#构造MIMEMultipart对象做为根容器main_msg=email.MIMEMultipart.MIMEMultipart()#构造MIMEText对象做为邮件显示内容并附加到根容器text_msg=email.MIMEText.MIMEText(”顶击拨号帮你转发的邮件”,_charset=“utf-8”)main_msg.attach(text_msg)#构造MIMEBase对象做为文件附件内容并附加到根容器ctype,encoding=mimetypes.guess_type(file_name)ifctypeisNoneorencodingisnotNone:ctype=’application/octet-stream’maintype,subtype=ctype.split(’/’,1)file_msg=email.MIMEImage.MIMEImage(open(file_name,’rb’).read(),subtype)##设置附件头basename=os.path.basename(file_name)file_msg.add_header(’Content-Disposition’,‘attachment’,filename=basename)#修改邮件头main_msg.attach(file_msg)#设置根容器属性main_msg[’From’]=Frommain_msg[’Reply-to’]=ReplyTo#main_msg[‘To’]=Tomain_msg[’Subject’]=“亲,你朋友给你发送的邮件-顶击科技有限公司转发”main_msg[’Date’]=email.Utils.formatdate()#main_msg[‘Bcc’]=To#得到格式化后的完整文本fullText=main_msg.as_string()#用smtp发送邮件try:server.sendmail(From,To.split(’;’),fullText)finally:server.quit()
# -*- coding: utf-8 -*-import smtplibimport email.MIMEMultipart# import MIMEMultipartimport email.MIMEText# import MIMETextimport email.MIMEBase# import MIMEBaseimport os.pathimport sysimport mimetypesimport email.MIMEImage# import MIMEImage
命令 mail.py <1:发送方(回复地址)10000@> <2:发送地址,多个以;隔开> <3:发送文件>
From ="合肥顶击科技<itop_business@5186.me>"
ReplyTo=sys.argv[1]
To = sys.argv[2]
file_name = sys.argv[3]#附件名
server = smtplib.SMTP("smtp.",25)
server.login("itop_business@5186.me","xxxxxxx") #仅smtp服务器需要验证时
构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("顶击拨号帮你转发的邮件",_charset="utf-8")
main_msg.attach(text_msg)
构造MIMEBase对象做为文件附件内容并附加到根容器
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg=email.MIMEImage.MIMEImage(open(file_name,'rb').read(),subtype)
设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
main_msg.attach(file_msg)
设置根容器属性
main_msg['From'] = From
main_msg['Reply-to'] = ReplyTo
main_msg['To'] = To
main_msg['Subject'] = "亲,你朋友给你发送的邮件-顶击科技有限公司转发"
main_msg['Date'] = email.Utils.formatdate()
main_msg['Bcc'] = To
得到格式化后的完整文本
fullText = main_msg.as_string( )
用smtp发送邮件
try:
server.sendmail(From, To.split(';'), fullText)
finally:
server.quit()
修改内容如下:
首先,在头部增加:
[python]view plain copy print?fromemail.headerimportHeader
from email.header import Header
1、将“合肥顶击科技”转为utf-8
[python]view plain copy print?From=“%s<itop_business@5186.me>”%Header(“合肥顶击科技”,“utf-8”)
From = "%s<itop_business@5186.me>" % Header("合肥顶击科技","utf-8")
结果如下:
2、在内容前面加个u
[python]view plain copy print?main_msg[‘Subject’]=u“亲,你朋友给你发送的邮件-顶击科技有限公司转发”
main_msg['Subject'] = u"亲,你朋友给你发送的邮件-顶击科技有限公司转发"
或者转为utf-8
[python]view plain copy print?Header(“hello,亲,你朋友给你发送的邮件-顶击科技有限公司转发”,“utf-8”)
Header("hello,亲,你朋友给你发送的邮件-顶击科技有限公司转发","utf-8")
现在一切OK,如下图所示:
至此,乱码问题全部解决。上面这个问题是在163邮箱中出现的问题,如果接收邮箱为QQ邮箱,没有发现回复时出现乱码问题。