python3发送smtp邮件代码
代码语言:python
所属分类:其他
代码描述:python3发送smtp邮件代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -* # -*- coding:utf-8 -*- import smtplib import email # import json # import base64 from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # from email.mime.image import MIMEImage # from email.mime.base import MIMEBase # from email.mime.application import MIMEApplication from email.header import Header from email.utils import formataddr # username,通过控制台创建的发信地址 username = 'XXXXXXXX' # password,通过控制台创建的SMTP密码 password = 'XXXXXXXX' # 自定义的回信地址,与控制台设置的无关。邮件推送发信地址不收信,收信人回信时会自动跳转到设置好的回信地址。 replyto = 'XXXXXXXX' # 显示的To收信地址 rcptto = ['address1@test.com', 'address2@test.com'] # 显示的Cc收信地址 rcptcc = ['address3@test.com', 'address4@test.com'] # Bcc收信地址,密送人不会显示在邮件上,但可以收到邮件 rcptbcc = ['address5@test.com', 'address6@test.com'] # 全部收信地址,包含抄送地址,单次发送不能超过60人 receivers = rcptto + rcptcc + rcptbcc # 构建alternative结构 msg = MIMEMultipart('alternative') msg['Subject'] = Header('自定义信件主题') msg['From'] = formataddr(["自定义发信昵称", username]) # 昵称+发信地址(或代发) # list转为字符串 msg['To'] = ",".join(rcptto) msg['Cc'] = ",".join(rcptcc) msg['Reply-to'] = replyto msg['Message-id'] = email.utils.make_msgid() msg['Date'] = email.utils.formatdate() # 若需要开启邮件跟踪服务,请使用以下代码设置跟踪链接头。 # 首先域名需要备案,设置且已正确解析了CNAME配置;其次发信需要打Tag,此Tag在控制台已创建并存在,Tag创建10分钟后方可使用; # 设置跟踪链接头 # 控制台创建的标签tagname # tagName = 'xxxxxxx' # # # OpenTrace对应值是字符串1,固定 # trace = { # "OpenTrace": '1', # "LinkTrace": '1', #smtp新增点击跟踪 # "TagName": tagName # } # jsonTrace = json.dumps(trace) # base64Trace = str(base64.b64encode(jsonTrace.encode('utf-8')), 'utf-8') # # print(base64Trace) # msg.add_header("X-AliDM-Trace", base64Trace) # 构建alternative的text/plain部分 # textplain = MIMEText('自定义TEXT纯文本部分', _subtype='plain', _charset='UTF-8') # msg.attach(textplain) # 构建alternative的text/html部分 texthtml = MIMEText('自定义HTML超文本部分.........完整代码请登录后点击上方下载按钮下载查看
网友评论0