import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
# 建立连接
connect = smtplib.SMTP_SSL('smtp.qq.com',465)
# 登录邮箱
connect.login('邮箱地址','授权码')
mail = MIMEMultipart()
mail['Subject'] = Header('发送附件','utf-8').encode()
mail['To']= 'lan <邮箱地址>'
mail['From'] = 'zwl <邮箱地址>'
'''
发送图片
'''
f1 = open('files/a.jpg','rb')
image1 = MIMEImage(f1.read())
image1.add_header("Content-ID",'<i1>')
mail.attach(image1)
text = MIMEText('结果是:<br> <img src="cid:i1" width="300">','html','utf-8')
mail.attach(text)
'''
添加附件
'''
# 1.打开文件获得文件对象
f1 = open('files/a.jpg','rb')
# 2.创建附件对应的文本对象
a1 = MIMEText(f1.read(),'base64','utf-8')
# 3.设置附件信息
a1["Content-Disposition"] = 'attachment; filename="a.jpg"'
# 4.将附件添加到邮件中
mail.attach(a1)
connect.sendmail('我的邮箱','对方的邮箱',mail.as_string())
connect.quit()
Day6 邮件自动发送(纯代码)
最新推荐文章于 2024-09-09 18:34:56 发布