在电子邮件中嵌入图像Python [英] Embedding image in email Python

查看:182
本文介绍了在电子邮件中嵌入图像Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Code used for sending an image embedded email using python is given below.

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 图像网址时,在接收方正确收到的电子邮件。

My problem is very specific to the receiver email server. I used the same code to send an email to a GMail id. It worked fine. But here, the receiver email server is considering this email as spam whenever I tries to embed an image in the email as shown in the above code. If I am not trying to embed the image, then both html and plain text emails are getting received at the destination as expected. I tried also embedding images with static http urls as the image src. But then also the problem exists. But when i tried using some https image urls, the emails where properly received at the receiver side.

接收方电子邮件过滤由 postini

The receiver side email filtering is powered by postini.

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

What might be the problem? Is there any way by which I can modify the above code to get rid of this problem.

谢谢。

推荐答案

你可以看看这个答案:
Python:由脚本发送的邮件被Gmail标记为垃圾邮件

You might take a look at this answer: Python: Mail sent by script is marked as spam by Gmail

另一个选项 - 要避免被标记为垃圾邮件 - 是使用像AWS SES这样的服务。

Another option - to avoid been marked as SPAM - is to use some service like AWS SES.

这篇关于在电子邮件中嵌入图像Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆