美文网首页
python 发送带附件邮件

python 发送带附件邮件

作者: 博翼腾飞 | 来源:发表于2020-01-19 14:37 被阅读0次
#!/usr/bin/python
# -*- coding: UTF-8 -*
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication 
from_addr = "xxxx@xxxx.com.cn"
pwd = "Aaxxxxxxxx"
to_addr = ['xxxx@xxxx.com.cn,xxxx@xxxx.com.cn,xxxx@xxxx.com.cn,xxxx@xxxx.com.cn']
def send_enclosure():
    # 2.创建实例对象,设置主题等信息
    msg = MIMEMultipart()
    msg["Subject"] = "代码行数统计结果"
    msg["From"] = from_addr
    msg["To"] = ','.join(to_addr)
 
    # 邮件内容(按每个部分)
    part1 = MIMEText("附件压缩包是前端、后端、大数据的代码行数统计结果")
    msg.attach(part1)
 
    # 添加压缩包附件
    part2 = MIMEApplication(open('20200106-代码行数统计结果.zip', 'rb').read())
    part2.add_header('Content-Disposition', 'attachment', filename="20200106-代码行数统计结果.zip")
    msg.attach(part2)
 
 
    # 3.连接smtp服务器,登录服务器并发送文本
    smtp_server = "xxxx.xxxx.com.cn"
    server = smtplib.SMTP(smtp_server,587)
    server.starttls()
    server.login(from_addr,pwd)
    server.sendmail(from_addr,msg['To'].split(','),msg.as_string()) # as_string()把MIMEText变成一个str
    server.close()
 
if __name__ == '__main__':
 
    send_enclosure()

相关文章

网友评论

      本文标题:python 发送带附件邮件

      本文链接:https://www.haomeiwen.com/subject/tzurzctx.html