带有嵌入式图像的Python MIME邮件无法正确显示

时间:2018-08-23 09:40:09

标签: python email html-email mime

我正在使用下面的代码以html格式构建邮件,以便每天自动发送。我必须在邮件中包含 7张图片。图像存储在脚本所在的文件夹中的.jpg(我也尝试过.png)中。

我遇到的问题是图片显示不正确:如果我尝试将电子邮件发送到gmail地址,我可以正确看到所有图片,但是每当我将电子邮件发送到企业电子邮件时,都可以看到某些图片其他则不是(我尝试使用Outlook和Web Outlook尝试从两家不同公司发送电子邮件)。

所显示的图像是随机变化的,但是可见的图像似乎总是四幅。有谁知道这可能是什么原因以及如何解决这个问题?非常感谢!

这是我的代码

names = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg']

msg = EmaiMessage()
msg['Subject'] = """AAA {d}-{m}-{y}""".format(d = day.day,m = day.month,y = day.year)
msg['From'] = "test@gmail.com"
msg['To'] = "test@gmail.com"
image_cid1 = make_msgid()
image_cid2 = make_msgid()
image_cid3 = make_msgid()
image_cid4 = make_msgid()
image_cid5 = make_msgid()
image_cid6 = make_msgid()
image_cid7 = make_msgid()

msg.add_alternative("""\
<html>
    <body>
        <p>Hi,<br><br>
           Images for {d}-{m}-{y}.<br><br>
           <hr>
        </p>
        <img src="cid:{image_cid1}" width="75%"><br><br><hr>    
        <img src="cid:{image_cid2}" width="75%"><br><br><hr>
        <img src="cid:{image_cid3}" width="75%"><br><br><hr>
        <img src="cid:{image_cid4}" width="75%"><br><br><hr>
        <img src="cid:{image_cid5}" width="75%"><br><br><hr>
        <img src="cid:{image_cid6}" width="75%"><br><br><hr>
        <img src="cid:{image_cid7}" width="75%"><br><br><hr>
    </body>
</html>
""".format(d = day.day, m = day.month, y = day.year,
           image_cid1=image_cid1[1:-1],
           image_cid2=image_cid2[1:-1],
           image_cid3=image_cid3[1:-1],
           image_cid4=image_cid4[1:-1],
           image_cid5=image_cid5[1:-1],
           image_cid6=image_cid6[1:-1],
           image_cid7=image_cid7[1:-1]), subtype='html')

with open(names[0], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid1)
with open(names[1], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid2)
with open(names[2], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid3)
with open(names[3], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid4)
with open(names[4], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid5)
with open(names[5], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid6)
with open(names[6], 'rb') as img:
    maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
    msg.get_payload()[1].add_related(img.read(), 
                                         maintype=maintype, 
                                         subtype=subtype, 
                                         cid=image_cid7)

try:
    smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=600)
    smtp.starttls()
    smtp.login('test', 'testpwd')
    smtp.sendmail(fromaddr, toaddr, msg.as_string())
    smtp.quit()
except:
    print("Unexpected error while sending email:", sys.exc_info()[0])
    raise
    sys.exit(1)

0 个答案:

没有答案