通过smtplib发送邮件时如何在邮件内容中添加href link

how to add href link in email content when sending email through smtplib

我正在通过以下代码发送电子邮件:

msg = MIMEText(u'<a href="www.google.com">abc</a>')
msg['Subject'] = 'subject'
msg['From'] = 'xxx'
msg['To'] = 'xxx'

s = smtplib.SMTP(xxx, 25)
s.sendmail(xxx, xxx, msg.as_string())

我想收到的是

abc

我实际收到的是:

<a href="www.google.com">abc</a>

您应该指定 'html' 作为子类型 -

msg = MIMEText(u'<a href="www.google.com">abc</a>','html')

不单独指定子类型,子类型默认为'plain'(纯文本)。来自 documentations -

class email.mime.text.MIMEText(_text[, _subtype[, _charset]])

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain.

(强调我的)。

这对我有用:)

email_body = """<pre> 
Congratulations! We've successfully created account.
Go to the page: <a href="https://www.google.com/">click here</a>
Thanks,
XYZ Team.
</pre>"""

msg = MIMEText(email_body ,'html')

O/P: 恭喜!我们已成功创建帐户。

转到页面:click here

谢谢,

XYZ 团队。