gpt4 book ai didi

python - 在电子邮件 Python 中嵌入图像

转载 作者:太空狗 更新时间:2023-10-30 00:19:32 25 4
gpt4 key购买 nike

下面给出了使用 python 发送嵌入图像的电子邮件的代码。

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Define these once; use them twice!
strFrom = 'from@sender.com'
strTo = 'to@example.com'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlternative.attach(msgText)

# This example assumes the image is in the current directory
fp = open('test.jpg', 'rb')

msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

我的问题是针对接收者 电子邮件服务器的。我使用相同的代码向 GMail id 发送了一封电子邮件。它运作良好。但是在这里,每当我尝试在电子邮件中嵌入图像(如上面的代码所示)时,接收方电子邮件服务器就会将此电子邮件视为垃圾邮件。如果我不尝试嵌入图像,那么 html 和纯文本电子邮件都会按预期在目的地收到。我还尝试将带有静态 http url 的图像作为图像 src 嵌入。但随后也存在问题。但是当我尝试使用一些 https 图片 url 时,电子邮件在接收方正确接收。

接收方电子邮件过滤由 postini 提供支持。

可能是什么问题?有什么办法可以修改上面的代码来解决这个问题。

谢谢。

最佳答案

你可以看看这个答案: Python: Mail sent by script is marked as spam by Gmail

另一种选择 - 避免被标记为垃圾邮件 - 是使用一些服务,如 AWS SES。

关于python - 在电子邮件 Python 中嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com