如何使用Python在电子邮件HTML中嵌入多个图像

How to embed multiple images in email HTML using Python(如何使用Python在电子邮件HTML中嵌入多个图像)

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

问题描述

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

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

推荐答案

我在这里找到了一个很好的帖子:http://dogdogfish.com/python-2/emailing-multiple-inline-images-in-python/,95%来自于他们的例子。感谢博主们! 我将导入更新到了Python3.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])

这篇关于如何使用Python在电子邮件HTML中嵌入多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用Python在电子邮件HTML中嵌入多个图像

基础教程推荐