简体   繁体   English

如何将图像添加到 Email body_python

[英]How to add image to Email body_python

I use python for sending Email.I need to insert picture to body e-mail like this:我使用 python 发送 Email。我需要像这样将图片插入正文电子邮件:

我需要这样的:

But my output not show image like this:但是我的 output 不显示这样的图像:

在此处输入图像描述

I try many solution for fix it but it ineffectively.我尝试了很多解决方案来修复它,但效果不佳。 This's my code is below:这是我的代码如下:

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 = 'from@example.com'
  TO = 'To@example.com'
  CC = ""  
  #msg = MIMEMultipart()
  msg = MIMEText("<html><body><h1>ดำเนินการส่งให้ทีม AP แล้ว</h1>" +
                 "<p>รายละเอีดตามข้อมูลด้านล่าง" +
                 "html_pages + <br><img src=te.png>"+
                 "<a href=""mailto:CFGFIN002@ngerntidlor.com?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:CFGFIN002@ngerntidlor.com?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()

Please tell me how to add image to my body email /.请告诉我如何将图像添加到我的身体 email /。

Main message has to be set to MIMEMultipart with 'related' subtype (first argument).主消息必须设置为带有'related'子类型(第一个参数)的MIMEMultipart

Then, the image has to be attached with content-id, as such:然后,图像必须附加 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())

[This is part of my email wrapper, hence "images" is a dict and such.] [这是我的电子邮件包装器的一部分,因此“图像”是一个字典等等。]

To refer to the image in the html, in src use cid: and the name (the same that was used to attach the pic to the email), eg要引用 html 中的图像,在 src 中使用cid:和名称(与用于将图片附加到电子邮件的名称相同),例如

<img src="cid:logo"/>

There's a really good post with multiple solutions to this problem here .这里有一个非常好的帖子,有多种解决这个问题方法。 I was able to send an email with HTML, an Excel attachment, and an embedded images.我能够发送带有 HTML、Excel 附件和嵌入图像的 email。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python 2中将正文文本添加到多部分电子邮件中? - How to add a body text to a multipart email in python 2? 如何使用python添加嵌入在电子邮件正文中的图像? - How to add images embedded in the body of the email with python? 如何在python电子邮件正文中添加多个数据框 - How to add more than one dataframe in python email body 如何在python mime multipart(具有附件)电子邮件中添加“正文” - How to add a “Body” to a python mime multipart(has attachment) email 如何使用 Python 在 win32com 的电子邮件正文中添加 2 个变量? - How to add 2 variables in the email body in win32com with Python? python:从电子邮件正文中提取并下载图像 - python: extract and download an image from the body of the email 使用python在电子邮件中添加多个正文 - Add multiple body in email using python 如何在 django 中将图像作为 email 主体的一部分发送 - How to send image as part of email body in django 我无法使用python在电子邮件正文中添加图片,我可以添加图片作为附件,但我想要代码在邮件正文中添加图片 - I am not able to add an image in email body using python , I am able to add a picture as a attachment but i want a code to add image in mailbody 如何在python中使用imaplib获取电子邮件正文? - How to fetch an email body using imaplib in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM