在html中嵌入图像以自动发送Outlook365电子邮件 [英] Embed an image in html for automatic outlook365 email send

查看:293
本文介绍了在html中嵌入图像以自动发送Outlook365电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用smtp和python中的电子邮件在html代码中嵌入和图像化. 套餐包括: 导入smtplib 从email.mime.multipart导入MIMEMultipart 从email.mime.text导入MIMEText

I am trying to embed and image in my html code using smtp and email in python. Packages are: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText

这是html中的代码段

This is the snippet in the html

<img border=0 width=231 height=67 src="Hello_files/image001.png">

这是我在实际电子邮件发送中看到的

This is what I see in the actual email send

我感觉自己在做一些非常错误的事情,这可能对大多数人来说都是显而易见的.

I feel like I am doing something very wrong that is probably obvious to most.

推荐答案

根据您的HTML代码段,我将对此进行介绍.

Based on your HTML snippet I'll take a swing at this.

您的HTML

<img border=0 width=231 height=67 src="Hello_files/image001.png">

您所引用的图像与本地文件系统中显示的图像相同,但是在收件人计算机中的电子邮件上下文中,该目录和文件名将不存在.

You're referencing the image as it appears in your local filesystem but in the context of the email in a recipient's computer, that directory and file name won't exist.

示例HTML

<img src='cid:image1' alt='image' style="display: block;margin: auto;width: 100%;">

在这里,我们将图像引用为 cid:image1 .在我们的python代码中,我们加载图像并对其进行编码以与html匹配.在下面的示例中,图像 test.png 与我们的python脚本位于同一目录中.

Here we reference the image as cid:image1. In our python code, we load the image and encode it to match up with our html. In the example below, the image test.png is in the same directory as our python script.

示例MIME图像编码

s = smtplib.SMTP(host='smtp.gmail.com', port=587)
s.starttls()
s.login('email','password')

msg = MIMEMultipart()  # create a message

email_body = "<img src='cid:image1' alt='image' style="display: block;margin: auto;width: 100%;">"

# Read and encode the image
img_data = open('./test.png', 'rb').read()
image = MIMEImage(img_data)
image.add_header('Content-ID', '<image1>')
msg.attach(image)

# Construct the email
msg['From']='swetjen@someplace.com'
msg['To']='swetjen@someplace.com'
msg['Subject']='My email with an embedded image.'

msg.attach(MIMEText(email_body, _subtype='html'))

s.send_message(msg)

这篇关于在html中嵌入图像以自动发送Outlook365电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆