如何向电子邮件body_python添加图像

问题描述 投票:0回答:1

我使用python发送电子邮件。我需要将图片插入这样的正文电子邮件中:

I need like this:

但是我的输出未显示如下图像:

enter image description here

我尝试了许多解决方案来解决它,但是效果不佳。这是我的代码如下:

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import pandas as pd
import win32com.client as win32
import openpyxl
import sys
from PIL import ImageGrab
from pathlib import Path
df = pd.read_excel("xxxxx", sheet_name = "sample",nrows = 2, usecols = "A:W")
df1 = pd.read_excel("xxxx", sheet_name = "sample")
excel_path = ("sample")
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(excel_path)
ws = wb.Worksheets(1)
win32c = win32.constants
ws.Range("A1:H33").CopyPicture(Format=win32c.xlBitmap)
img = ImageGrab.grabclipboard()
image_path = str("path" + 'te.png')
img.save(image_path)
outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(0)
user = df1.loc[34,"Unnamed: 4"]
approver = df1.loc[36,"Unnamed: 4"]
def FIN(xReceiveTO,xReceiveCC, xsubject, xBody):
  FROM = '[email protected]'
  TO = '[email protected]'
  CC = ""  
  #msg = MIMEMultipart()
  msg = MIMEText("<html><body><h1>ดำเนินการส่งให้ทีม AP แล้ว</h1>" +
                 "<p>รายละเอีดตามข้อมูลด้านล่าง" +
                 "html_pages + <br><img src=te.png>"+
                 "<a href=""mailto:[email protected]?cc=" + user + ";"+ approver + "&subject=Invoice%20ฉบับนี้ผ่านการพิจารณาอนุมัติจาก%20หัวหน้าทีมแล้ว%20รบกวนทีม%20AP%20ตรวจสอบสอบส่งต่อไปทีม%20Financial%20Planing%20and%20Analytics%20ครับ&body=ทีม%20AP%20ได้ทำการตรวจสอบแล้วมีความเห็นว่าสมควร%20Approve%20ครับ>Approve</a> "+" "+"<a href=""mailto:[email protected]?cc=" +user + ";"+ approver + "&subject=Invoice%20ฉบับนี้ไม่ผ่านการพิจารณาอนุมัติจากทีม%20AP&body=ทีม%20AP%20ได้ทำการตรวจสอบแล้วมีความเห็นว่าสมควร%20Reject%20ครับ%20>Reject</a>"
                 "</body></html>","html","utf-8""")
  #msg['Subject'] =  Header(subject, 'utf-8')
  msg['Subject'] =  "Test"
  msg['To'] = ', '.join(xReceiveTO)
  msg['CC'] = ', '.join(xReceiveCC)
  msg['FROM'] = FROM
  s = smtplib.SMTP('10.45.1.25')
  s.send_message(msg)
  s.quit()

请告诉我如何将图像添加到我的体内电子邮件/。

python html email outlook
1个回答
0
投票

必须将主消息设置为具有MIMEMultipart子类型(第一个参数)的'related'

然后,图像必须带有content-id,如下所示:

msg = MIMEMultipart("related")
msg["Subject"] = subject
msg["From"] = from_addr
msf["To"] = to_addr

html_output = "your html here"

msg.attach(MIMEText(html_output, "html"))
if images:
    for image_name, image_location in images.items():
        with open(image_location, "rb") as fp:
            img = MIMEImage(fp.read())
        img.add_header("Content-ID", "<{}>".format(image_name))
        msg.attach(img)

smtplib.SMTP(host, port).sendmail(self.from_addr, to_addr, msg.as_string())

[[这是我的电子邮件包装程序的一部分,因此“图像”是字典等。]

要引用html中的图像,请在src中使用cid:和名称(例如,用于将图片附加到电子邮件的名称),例如

<img src="cid:logo"/>
© www.soinside.com 2019 - 2024. All rights reserved.