如何在通过smtplib发送电子邮件时在电子邮件内容中添加href链接

Yuw*_*Yan 11 python email smtp 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())
Run Code Online (Sandbox Code Playgroud)

我想要收到的是

ABC

我实际收到的是:

<a href="www.google.com">abc</a>
Run Code Online (Sandbox Code Playgroud)

Ana*_*mar 10

你应该指定'html'为子类型 -

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

如果不单独指定子类型,子类型默认为'plain'(纯文本).来自文件 -

class email.mime.text.MIMEText(_text [,_ subtype [,_ charset]])

MIMENonMultipart的子类,MIMEText类用于创建主要类型文本的MIME对象._text是有效负载的字符串._subtype是次要类型,默认为plain.

(强调我的).


Wal*_*alk 8

这对我有用:)

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')
Run Code Online (Sandbox Code Playgroud)

奥/普:恭喜!我们已经成功创建帐户了。

前往页面:点击这里

谢谢,

XYZ 团队。