【发布时间】:2014-10-03 13:26:08
【问题描述】:
下面给出了用于使用 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()
我的问题非常具体到 receiver 电子邮件服务器。我使用相同的代码向 GMail id 发送电子邮件。它工作得很好。但是在这里,每当我尝试在电子邮件中嵌入图像时,接收方电子邮件服务器都会将此电子邮件视为垃圾邮件,如上面的代码所示。如果我不尝试嵌入图像,那么 html 和纯文本电子邮件都会按预期在目的地收到。 我还尝试使用静态 http url 嵌入图像作为图像 src。但是问题也存在。但是当我尝试使用一些 https 图片网址时,接收方正确接收的电子邮件。
接收方电子邮件过滤由 postini 提供支持。
可能是什么问题?有什么办法可以修改上面的代码来解决这个问题。
谢谢。
【问题讨论】:
-
接收服务器是否在电子邮件中添加任何垃圾邮件标头?他们可以提示你它不喜欢什么。
-
带有少量文本的图像被视为试图通过在图像中放置垃圾邮件来绕过垃圾邮件过滤器的标志。
-
RossRidge 的评论是正确的,并且与您要得到 Nidhin Joseph 的答案一样接近。您应该尝试再次发送电子邮件,除非图像中包含大量文本,然后查看它是否没有被标记。
-
我试过了。但结果相同。
-
收件人电子邮件服务器是公司的吗?如果是,则可以将反垃圾邮件过滤器配置为偏执级别。我曾经看到一个邮件服务器拒绝任何带有附加 zip 文件的“危险”邮件。与其尝试猜测,不如询问实际规则——或者更确切地说,具体询问您的电子邮件被拒绝的原因。
标签: python email sendmail html-email spam