Python 3 嵌入式图像未附加在电子邮件中

时间:2020-12-30 16:44:20

标签: python email

我的代码将图像嵌入到电子邮件中,但似乎没有附加。我收到了邮件,但是 ?图像图标。我搜索和查看的所有内容都表明它们已正确附加。

邮件模板的片段:

<body>
    <img src="cid:image_1"></img>
    {{ content }}
    <img src="cid:image_2"></img>
</body>

邮件功能

template = 'Communications'
recipients =['me@me.com']
subject = 'Test Email'
text_data = 'PLAIN TEXT'
html_data = '<h2>HTML TEXT</h2>'

mail_template = MailTemplate.objects.filter(name=template).prefetch_related('mailimage_set').first()

# create jinja env for mail templates 
env = Environment(autoescape=False, optimized=False) 
plain_text_template = env.from_string(mail_template.plain_text_template)
html_template = env.from_string(mail_template.html_template)
# create templates and render content
plain_text_content = plain_text_template.render(content=text_data)
html_content = html_template.render(content=html_data)
# create message types
text = MIMEText(plain_text_content, 'plain')
html = MIMEText(html_content, 'html')
# create new mail
msg = MIMEMultipart('alternative')
msg.attach(text)
msg.attach(html)   

# set mail attribs
mail_to = ','.join(recipients)
msg['Subject'] = subject
msg['From'] = 'test@test.com'
msg['To'] = mail_to
# add images
i = 1
for img in mail_template.mailimage_set.all():
    # open the image from s3 bucket
    img_data = urlopen(signed_url(img.image.url)).read()
    msgImage = MIMEImage(img_data)
    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<image_{}>'.format(i))
    # attach the image
    msg.attach(msgImage)
    # increase the image number
    i +=1

server = smtplib.SMTP('mxrelay.test.com', 25)
server.ehlo()
server.sendmail('test@test.com', mail_to, msg.as_string())
server.close()

2 个答案:

答案 0 :(得分:1)

因为图像在 Html 中是 referenced,所以它们是 related

您可以创建另一个related MIMEMultipart,将图像mimetype 和替代MIMEMultipart 附加到它并保持替代原样

template = 'Communications'
recipients =['me@me.com']
subject = 'Test Email'
text_data = 'PLAIN TEXT'
html_data = '<h2>HTML TEXT</h2>'

mail_template = MailTemplate.objects.filter(name=template).prefetch_related('mailimage_set').first()

# create jinja env for mail templates 
env = Environment(autoescape=False, optimized=False) 
plain_text_template = env.from_string(mail_template.plain_text_template)
html_template = env.from_string(mail_template.html_template)
# create templates and render content
plain_text_content = plain_text_template.render(content=text_data)
html_content = html_template.render(content=html_data)
# create message types
text = MIMEText(plain_text_content, 'plain')
html = MIMEText(html_content, 'html')
# create new mail
msg = MIMEMultipart('related')                   # related part for image
alt_msg = MIMEMultipart('alternative')
alt_msg.attach(text)
alt_msg.attach(html)
msg.attach(alt_msg)                              # attached alternative to related

# set mail attribs
mail_to = ','.join(recipients)
msg['Subject'] = subject
msg['From'] = 'test@test.com'
msg['To'] = mail_to
# add images
i = 1
for img in mail_template.mailimage_set.all():
    # open image read bytes from url
    img_data = urlopen(signed_url(img.image.url)).read()
    img = MIMEImage(img_data)
    # define the image's ID as referenced above
    img.add_header('Content-ID', '<image_{}>'.format(i))
    # image would be displayed inline with content
    img.add_header("Content-Disposition", "inline", filename='<image_{}>'.format(i))
    # attach the image
    msg.attach(img)                              # attached image to related
    # increase the image number
    i +=1

server = smtplib.SMTP('mxrelay.test.com', 25)
server.ehlo()
server.sendmail('test@test.com', mail_to, msg.as_string())
server.close()

答案 1 :(得分:0)

您可能会发现使用 base64 encoding

嵌入图像更容易
import base64
# read image file to bytes
with open("alexw.png", "br") as fh:
    content = fh.read()
# base64 the image bytes, then convert to a UTF-8 string
b64_img_str = base64.b64encode(content).decode("utf-8") 

# prepare a string ready to embed in the email
img_src = f"""<img src="data:image/png;base64, {b64_img_str} alt="foo"</img>"""

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAA7EAAAOxAGVKw4bAAADnklEQVRYha2XQWgcVRjHf5tddhCy0VJppEIsWlyQbt1l91BF0ICgl1zMKbkkOWSjlyxFb4rMRZGqyNZLNgWz8ZBcmoPNQUTpUgzoQpJdukVNbWObg6EFU0z2koGwHuJ7ffPmvZ3dtP/T+883833f+97/+2Ymcv6nj1qVrRVsqE1U5Hru+gLDySH6nITkE2dHffZdr0kh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