#coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os
_user = "XXX@qq.com"
_pwd = "123445"
areceiver = "aa@qq.com,bb@qq.com"
acc = "cc@qq.com"
#如名字所示Multipart就是分多个部分
msg = MIMEMultipart()
msg["Subject"] = '患者基本信息及训练情况统计'
msg["From"] = _user
msg["To"] = areceiver
msg["Cc"] = acc
def send_email():
#---这是文字部分---
part = MIMEText('''Deal all,
附件是患者基本信息及训练情况统计表,请查收!''')
msg.attach(part)
#---这是附件部分---
#xls类型附件
part = MIMEApplication(open('文件名20170509.xls','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="文件名20170509.xls")
msg.attach(part)
s = smtplib.SMTP("smtp.qq.com", timeout=30)#连接smtp邮件服务器,端口默认是25
s.login(_user, _pwd)#登录服务器
s.sendmail(_user, areceiver.split(',') + acc.split(','), msg.as_string())#发送邮件
print("Eamil send successfully")
s.close()
if __name__=="__main__":
send_email()
'''
#jpg类型附件
part = MIMEApplication(open('foo.jpg','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.jpg")
msg.attach(part)
#pdf类型附件
part = MIMEApplication(open('foo.pdf','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
msg.attach(part)
#mp3类型附件
part = MIMEApplication(open('foo.mp3','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
msg.attach(part)
'''