【发布时间】:2019-02-23 01:04:16
【问题描述】:
我正在尝试在使用 python smtplib 发送电子邮件时附加指向本地文件的链接。
msg.attach(MIMEText(u'<a href="file:///C:\folder\file.txt">Link</a>', 'html'))
但它只是在电子邮件中以纯文本形式出现。
如果我只是在 html 页面中使用该链接,则该链接有效。
<html>
<a href="file:///C:\folder\file.txt">Link</a>
</html>
我该如何解决这个问题?
编辑:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
....
....
....
msg = MIMEMultipart()
msg['From'] = self.username
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(self.username, self.password)
mailServer.sendmail(self.username, to, msg.as_string())
我发送邮件的代码sn-p和这个很像
【问题讨论】:
-
你用什么发送邮件?你能在这里发布一个代码sn-p吗?
-
我已经编辑了帖子
-
您没有将有效的
HTML提供给MIMEText。添加<html>...</html>。参数MIMEText(..., 'html')只用在MIMEText的header中。 -
@stovfl 我尝试以这种方式包含它。它不起作用。
html_text += '<html>' + '\n' html_text += '<a href = file:///C:\folder\file.txt">Link</a>' + '\n' html_text += '</html>' + '\n' msg.attach(MIMEText(html_text, 'html')) -
@ampat:参考sending-html-email-using-python