最后更新时间:2020/04/22
"""user_info: {'mail': [user, name, pwd, dn, mail]}
"""
# 第三方 SMTP 服务
mail_host="smtp.exmail.qq.com" #设置服务器
mail_user="[email protected]" #用户名
mail_pass="XXXXXXXXXXXXXXX" #口令
sender = '[email protected]'
#邮件标题
mail_title = '【AD域初始密码告知】'
# 登录
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user,mail_pass)
try:
for k, v in zip(users_info.keys(), users_info.values()):
print(k, v)
# message = MIMEMultipart('mixed') # 消息基础,这种图片会作为附件
message = MIMEMultipart('related') # 消息基础
message["Subject"] = Header(mail_title, 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(str(k), 'utf-8')
msgAlternative = MIMEMultipart('alternative')
message.attach(msgAlternative)
mail_msg = """
这里是下一步做出来的htm静态文件源码粘贴过来
"""
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
# 指定图片为当前目录,html中引用图片,但是不作为附件
# image = MIMEImage(open(r'E:\\Files\\202002ad\\xxxx.png', 'rb').read())
# image.add_header('Content-ID', '')
# image["Content-Disposition"] = 'attachment; filename="xxxx.png"'
# # message.attach(msg)
# message.attach(image)
fp = open('E:\\Files\\202002ad\\logo_xx.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '' )
message.attach(msgImage)
smtpObj.sendmail(mail_user, [str(k)], message.as_string())
print('发送成功!')
except smtplib.SMTPException:
print("Error: 无法发送邮件")
smtpObj.quit()
可视化编辑html邮件内容campaignmonitor
拖拽创建好html,将文件发送到邮箱,下载下来,html代码复制到python代码中
不支持svg,若是svg可以在网上转换成png,然后加载进邮件