【问题标题】:How to embed multiple images in email HTML using Python如何使用 Python 在电子邮件 HTML 中嵌入多个图像
【发布时间】:2021-05-06 01:46:11
【问题描述】:

我在一个文件夹中保存了 15 张 .jpg 图像的列表。我希望所有这些都嵌入到电子邮件的正文中。我的第一种方法是将它们垂直堆叠成一个长图像并嵌入单个图像 - 但随后生成的电子邮件中的大图似乎缩小了,它变得更小,我堆叠在一起的图片越多。

我很难找到一个可重现的示例,其中 多张图片 在邮件中相互嵌入,以使每张图片都保持原始大小。

【问题讨论】:

  • 也许您生成了较小的图像?或者只是email readed 以这种方式显示它,但图像是正确的 - 然后问题不是电子邮件而是email readed
  • 嗨@furas。不知道您所说的“已阅读电子邮件”是什么意思。这 15 张图片也可以更改为 100 张。所以我想这是正确嵌入它们的方法,这可以解决我的问题。
  • 我的意思是email reader,例如网络浏览器中的 Thunderbird、Outlook、Gmail 等。并且您在 HTML 中嵌入了图像,那么您可能需要在 HTML 标签中设置宽度、高度。但首先我会下载带有图像的电子邮件并检查图像是否真的大小正确。我不知道您使用什么代码来生成电子邮件,所以我无法确认您是否正确创建了它 - 只是我不信任我无法查看和测试的代码。

标签: python html image email embed


【解决方案1】:

我在这里找到了一篇好帖子:http://dogdogfish.com/python-2/emailing-multiple-inline-images-in-python/,95% 来自他们的示例。感谢博主! 我将导入更新到 Python 3.x 并调整了代码,以便嵌入特定文件夹中的所有图像

from os import listdir
from os.path import isfile, join
import cgi
import uuid
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
import os
import smtplib
from email.mime.base import MIMEBase
from email import encoders
import numpy as np

gmail_user = "my_email@gmail.com"
gmail_pwd = "pw"
final_path_current = "path/to/folder/with/images"
receive_mail = "friend_email@gmail.com"

def attach_image(img_dict):
    with open(img_dict['path'], 'rb') as file:
        msg_image = MIMEImage(file.read(), name=os.path.basename(img_dict['path']))
    msg_image.add_header('Content-ID', '<{}>'.format(img_dict['cid']))
    return msg_image


def attach_file(filename):
    part = MIMEBase('application', 'octect-stream')
    part.set_payload(open(filename, 'rb').read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename=%s' % os.path.basename(filename))
    return part


def generate_email(gmail_user, to_list, img_dict):
    msg = MIMEMultipart('related')
    msg['Subject'] = Header(u'Subject', 'utf-8')
    msg['From'] = gmail_user
    msg['To'] = ','.join(to_list)
    msg_alternative = MIMEMultipart('alternative')
    msg_text = MIMEText(u'Image not working', 'plain', 'utf-8')
    msg_alternative.attach(msg_text)
    msg.attach(msg_alternative)
    msg_html = u'<h1>Below are the images</h1>'
    for img in img_dict:
        msg_html += u'<h3>'+img["title"][:-4]+'</h3><div dir="ltr">''<img src="cid:{cid}" alt="{alt}"><br></div>'.format(
            alt=cgi.escape(img['title'], quote=True), **img)
    msg_html = MIMEText(msg_html, 'html', 'utf-8')
    msg_alternative.attach(msg_html)
    for img in img_dict:
        msg.attach(attach_image(img))

    return msg


def send_email(msg, gmail_user, gmail_pwd, to_list):
    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user, to_list, msg.as_string())
    mailServer.quit()


img_dict = []
all_files = [f for f in listdir(final_path_current) if isfile(join(final_path_current, f))]

for file in all_files:
        img_dict_single = dict(title=file, path=final_path_current+"/"+file, cid=str(uuid.uuid4()))
        img_dict.append(img_dict_single)

email_msg = generate_email(gmail_user, [receive_mail], img_dict=img_dict)
send_email(email_msg, gmail_user, gmail_pwd, [receive_mail])

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 2011-08-16
    • 2011-10-06
    • 2015-03-14
    • 2018-06-29
    • 2019-04-30
    • 2014-10-03
    • 2020-12-14
    相关资源
    最近更新 更多