python 自动发送邮件--不关注目标文件夹下的附件名称与数量

对于接收方有容量限制的邮箱,自动发送带附件的邮件无疑可以降低个人的工作量,特别是有的资料太大,而接收方的邮箱有限制的时候,就要用压缩软件执行分卷压缩,之后就是漫长的重复性发邮件的过程,费事又费力。

 

个人于是想用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()

------------------------------------------------------------------------------------------

代码就是这样了,不过觉得自己写的有点繁琐。。。。还请高手帮忙修改下啊,过两天等我可以自动的调用压缩软件,压缩文件了以后就可以了。。

备注:这里我查找的是制定文件夹下的内容,因此,如果你想使用的话,就要把压缩之后的文件放到制定的文件夹下,后面如果有时间我再优化下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值