#!/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()
网友评论