【发布时间】:2023-03-18 08:51:01
【问题描述】:
如何发送带有内嵌图像的python电子邮件作为签名?目前,我使用一个链接来显示这样的图像:
<img border="0" src="https://website.com/logo.png" role="presentation" alt="logo mail icon" style="max-width: 300px; width: 300px; display: block;">
但在 Outlook 中,图像未加载,用户需要单击“显示图像”,这会破坏电子邮件,因为我将其用于商业目的(这是注册的确认链接)。有没有办法如何内联显示图像并通过电子邮件而不是链接发送它们?
到目前为止我的代码:
from email.message import EmailMessage
import smtplib, ssl
import email.utils
import html2text
with open(html_template, 'r') as f:
html_string = f.read()
plain_text = html2text.html2text(html_string)
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = mail_address
msg['To'] = email_receiver
msg['Date'] = email.utils.formatdate()
msg.add_header('Content-Type','text/html')
msg.add_header('Content-Disposition', 'attachment', filename=file_attachment)
msg.set_content(plain_text)
msg.add_alternative(html_string.format(name=surname), subtype='html')
files = [file_attachment]
for file in files:
with open(file, 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
with smtplib.SMTP(smtp, port) as smtp:
smtp.ehlo()
smtp.starttls(context=ssl.create_default_context())
smtp.ehlo()
smtp.login(mail_address, mail_password)
try:
smtp.send_message(msg)
smtp.quit()
except Exception as e:
print('ERROR')
我找不到太多方法或解决方案。我已经阅读了有关将图像编码为 base64 的信息,但我有点困惑。
提前致谢!
【问题讨论】: