# -*- coding: utf8 -*-
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
import matplotlib.pyplot as plt
if __name__ == '__main__':
# 以html格式构建邮件内容
send_str = '<html><body>'
send_str += '<center>打钱</center>'
# html中以<img>标签添加图片,align和width可以调整对齐方式及图片的宽度
send_str += '<img src="cid:image1" alt="image1" align="center" width=100% >'
send_str += '<center>打钱</center>'
send_str += '</body></html>'
# 画图并保存到本地
''' pic_path = 'test.png'
plt.plot([1, 3, 2, 4], '--r')
plt.title('ooooo')
plt.savefig(pic_path)
构建message
'''
msg = MIMEMultipart()
'''
# 添加邮件内容
content = MIMEText(send_str, _subtype='html', _charset='utf8')
msg.attach(content)
# 构建并添加图像对象
img1 = MIMEImage(open('c:\\1.jpg', 'rb').read(), _subtype='octet-stream')
img1.add_header('Content-ID', 'image1')
msg.attach(img1)
'''
# 邮件主题
msg['Subject'] = '这是一个打钱'
# 邮件收、发件人
user = "gong@mail.xxx.com.cn"
me = "洪老大" # + "<gong@mail.xxx.com.cn>"
to_list = ["gong@mail.xxx.com.cn"]
msg['To'] = ';'.join(to_list)
msg['From'] = me
# 构建并添加附件对象
# 如果不想直接在邮件中展示图片,可以以附件形式添加
'''
img = MIMEImage(open("c:\\1.jpg", 'rb').read(), _subtype='octet-stream')
img.add_header('Content-Disposition', 'attachment', filename=pic_path)
msg.attach(img)
'''
# 密码(有些邮件服务商在三方登录时需要使用授权码而非密码,比如网易和QQ邮箱)
passwd = "你的授权码"
# 登录邮箱
server = smtplib.SMTP("10.5.1.76",25)
server.connect("10.5.1.76")
print('Login Successful!')
# 发送邮件
server.sendmail(user, to_list, msg.as_string())
print('Send Successful')