发送HTML格式的邮件其实只要再使用MIMEText函数构造邮件消息体的时候将第二个参数指定格式为html即可
代码看看吧,我也不知道为什么发送的邮件点不开超链接
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'xxx@xxxxxx.com'
password = 'xxxxxxx'
receiver = ['xxx@xx.com', ]
mail_msg = """
<p>使用Python发送邮件</p>
<br>
<p><a href="http://www.baidu.com">这是一个超链接</p>
"""
#指定消息体使用html格式
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header('Python邮件', 'utf-8')
message['To'] = Header('邮件', 'utf-8')
#邮件标题
message['Subject'] = 'Python SMTP 发送邮件2'
try:
smtp = smtplib.SMTP_SSL('smtp.qiye.aliyun.com', 465)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, message.as_bytes())
print('邮件已发送!')
except smtplib.SMTPException as e:
x = e.args[1]
a = x.decode('gbk')
print('出现错误!', a)