对于接收方有容量限制的邮箱,自动发送带附件的邮件无疑可以降低个人的工作量,特别是有的资料太大,而接收方的邮箱有限制的时候,就要用压缩软件执行分卷压缩,之后就是漫长的重复性发邮件的过程,费事又费力。
个人于是想用python写了个程序用于发邮件。。
前面自动调用压缩软件分卷压缩文件的还没弄懂怎么写,先把自动发送邮件的这个程序 晒出来。。
程序如下:
#module
import os
import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
#variable
#--get filename
#rootdir = os.getcwd()
rootdir = 'D:\EmailSendRec'
filenames = os.listdir(rootdir)
#-- search the file and then send mail
for fname in filenames:
filelist.append(fname)
lb = os.path.basename(fname)
#--create mail
msg=MIMEMultipart()
#--config the address and subject
msg["From"] = "user1@126.com"
msg["To"] = ”user2@126.com“
#set smtp service
smtp=smtplib.SMTP('smtp.126.com')
msg["Subject"] = "Send Email --"+os.path.basename(lb)
#add content to mail
text=MIMEText("This is text content .")
msg.attach(text)
#--config the file name and ctype
filename=rootdir+"\\"+lb
ctype,encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
#append the attachment
#att=MIMEImage(open(filename,'r').read(),subtype)
att=MIMEImage(open(filename, 'rb').read(),subtype)
#here the print show the info
print(ctype,encoding)
#att["Content-Disposition"] = 'attachmemt;filename =
os.path.basename(lb)'
att["Content-Disposition"] = 'attachmemt;filename = %s'%(lb)
msg.attach(att)
smtp.login('user1@126.com','password')
smtp.sendmail(msg["From"],msg["To"],msg.as_string())
smtp.quit()
------------------------------------------------------------------------------------------
代码就是这样了,不过觉得自己写的有点繁琐。。。。还请高手帮忙修改下啊,过两天等我可以自动的调用压缩软件,压缩文件了以后就可以了。。
备注:这里我查找的是制定文件夹下的内容,因此,如果你想使用的话,就要把压缩之后的文件放到制定的文件夹下,后面如果有时间我再优化下。