使用 Python 将带有内嵌图像的电子邮件发送到 Gmail

2024-03-16

我的目标是使用 Python 向具有内嵌图像的 Gmail 用户发送电子邮件。无法在线托管此图像,然后通过href,由于图像的敏感性(来自我的工作的数据)。

我尝试过编码base64版本变成HTML然后发送的是HTML,但众所周知这是行不通的。然后我注意到,在 Gmail 中,您可以将图像拖放到发送框中,它将在接收端内嵌显示。鉴于此,我尝试从 Python 发送一封电子邮件,并将图像作为附件。这可以在下面的代码中看到,但不幸的是图像没有内联显示。

那么我的问题是:如何发送图像以使其内嵌显示?

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "[email protected] /cdn-cgi/l/email-protection"
gmail_pwd = "pass"

to = "[email protected] /cdn-cgi/l/email-protection"
subject = "Report"
text = "Picture report"
attach = 'TESTING.png'

msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
   'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()

当我手动将内嵌图像发送给自己时,“原始电子邮件”如下所示:

  Content-Type: multipart/related; boundary=047d7bd761fe73e03304e7e02237

--047d7bd761fe73e03304e7e02237
Content-Type: multipart/alternative; boundary=047d7bd761fe73e03004e7e02236

--047d7bd761fe73e03004e7e02236
Content-Type: text/plain; charset=ISO-8859-1

[image: Inline images 1]

--047d7bd761fe73e03004e7e02236
Content-Type: text/html; charset=ISO-8859-1

<div dir="ltr"><img alt="Inline images 1" src="cid:ii_141810ee4ae92ac6" height="400" width="534"><br></div>

--047d7bd761fe73e03004e7e02236--
--047d7bd761fe73e03304e7e02237
Content-Type: image/png; name="Testing.png"
Content-Transfer-Encoding: base64
Content-ID: <ii_141810ee4ae92ac6>
X-Attachment-Id: ii_141810ee4ae92ac6

当我通过 Python 将其作为附件发送给自己时,情况非常不同:

Content-Type: multipart/mixed; boundary="===============6881579935569047077=="
MIME-Version: 1.0
(.... some stuff deleted here)
--===============6881579935569047077==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

See attachment for report.
--===============6881579935569047077==
Content-Type: application/octet-stream
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="TESTING.png"

看来遵循 gmail 电子邮件模板是有效的:

* multipart/alternative
  - text/plain
  - multipart/related
    + text/html
      <img src="cid:msgid"/>
    + image/png
      Content-ID: <msgid>

基于以下示例email模块文档 https://docs.python.org/3/library/email.examples.html#email-examples:

#!/usr/bin/env python3
import html
import mimetypes
from email.headerregistry import Address
from email.message import EmailMessage
from email.utils import make_msgid
from pathlib import Path

title = 'Picture report…'
path = Path('TESTING.png')
me = Address("Pepé Le Pew", *gmail_user.rsplit('@', 1))

msg = EmailMessage()
msg['Subject'] = 'Report…'
msg['From'] = me
msg['To'] = [me]
msg.set_content('[image: {title}]'.format(title=title))  # text/plain
cid = make_msgid()[1:-1]  # strip <>    
msg.add_alternative(  # text/html
    '<img src="cid:{cid}" alt="{alt}"/>'
    .format(cid=cid, alt=html.escape(title, quote=True)),
    subtype='html')
maintype, subtype = mimetypes.guess_type(str(path))[0].split('/', 1)
msg.get_payload()[1].add_related(  # image/png
    path.read_bytes(), maintype, subtype, cid="<{cid}>".format(cid=cid))

# save to disk a local copy of the message
Path('outgoing.msg').write_bytes(bytes(msg))

To send msg通过 Gmail:

import smtplib
import ssl

with smtplib.SMTP('smtp.gmail.com', timeout=10) as s:
    s.starttls(context=ssl.create_default_context())
    s.login(gmail_user, gmail_password)
    s.send_message(msg)

Python 2/3 兼容版本

* multipart/related
  - multipart/alternative
    + text/plain
    + text/html
      <div dir="ltr"><img src="cid:ii_xyz" alt="..."><br></div>
  - image/jpeg
    Content-ID: <ii_xyz>

基于发送包含嵌入图像和纯文本替代的 HTML 电子邮件 http://code.activestate.com/recipes/473810-send-an-html-email-with-embedded-image-and-plain-t/:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cgi
import uuid
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text      import MIMEText
from email.mime.image     import MIMEImage
from email.header         import Header    

img = dict(title=u'Picture report…', path=u'TESTING.png', cid=str(uuid.uuid4()))

msg = MIMEMultipart('related')
msg['Subject'] = Header(u'Report…', 'utf-8')
msg['From'] = gmail_user
msg['To'] = ", ".join([to])
msg_alternative = MIMEMultipart('alternative')
msg.attach(msg_alternative)
msg_text = MIMEText(u'[image: {title}]'.format(**img), 'plain', 'utf-8')
msg_alternative.attach(msg_text)

msg_html = MIMEText(u'<div dir="ltr">'
                     '<img src="cid:{cid}" alt="{alt}"><br></div>'
                    .format(alt=cgi.escape(img['title'], quote=True), **img),
                    'html', 'utf-8')
msg_alternative.attach(msg_html)

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

To send msg通过 Gmail:

import ssl

s = SMTP_SSL('smtp.gmail.com', timeout=10,
             ssl_kwargs=dict(cert_reqs=ssl.CERT_REQUIRED,
                             ssl_version=ssl.PROTOCOL_TLSv1,
                             # http://curl.haxx.se/ca/cacert.pem
                             ca_certs='cacert.pem')) 
s.set_debuglevel(0)
try:
    s.login(gmail_user, gmail_pwd)
    s.sendmail(msg['From'], [to], msg.as_string())
finally:
    s.quit()

SMTP_SSL是可选的,你可以使用starttls方法来自你的问题:

import smtplib
import socket
import ssl
import sys

class SMTP_SSL(smtplib.SMTP_SSL):
    """Add support for additional ssl options."""
    def __init__(self, host, port=0, **kwargs):
        self.ssl_kwargs = kwargs.pop('ssl_kwargs', {})
        self.ssl_kwargs['keyfile'] = kwargs.pop('keyfile', None)
        self.ssl_kwargs['certfile'] = kwargs.pop('certfile', None)
        smtplib.SMTP_SSL.__init__(self, host, port, **kwargs)

    def _get_socket(self, host, port, timeout):
        if self.debuglevel > 0:
            print>>sys.stderr, 'connect:', (host, port)
        new_socket = socket.create_connection((host, port), timeout)
        new_socket = ssl.wrap_socket(new_socket, **self.ssl_kwargs)
        self.file = getattr(smtplib, 'SSLFakeFile', lambda x: None)(new_socket)
        return new_socket
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 将带有内嵌图像的电子邮件发送到 Gmail 的相关文章

随机推荐

  • JPA 配置布尔字段以保留为整数

    在 JPA 中 有一个注释指定布尔字段应保留为整数 我正在使用 OpenJPA 它当前将布尔字段保留为位 我宁愿使用整数 1 或 0 您可以指定列定义 Column name boolColumn columnDefinition INT
  • 导航栏不显示

    这里我有2个看法 欢迎VC WebViewVC First 应用程序代理 calls 欢迎VC通过下面的代码 void presentWelcomeViewController WelcomeViewController welcomeVi
  • 为每个 cURL 请求返​​回 AJAX 结果

    基本上 我的想法是做某种形式的 实时 cURL 结果 正在生产的系统实时结果例如 当执行每个请求时 我将有一个表 其中包含需要通过 cURL 请求访问的网站列表 其中根据每个 cURL 响应的结果 我需要使用 AJAX 将数据发送回我的页面
  • 循环语句的 freemarker 模板

    我想在 freemarker 模板中创建 for 语句 我正在阅读指南http freemarker sourceforge net http freemarker sourceforge net 但只有清单 我如何创建 for 语句或 f
  • 关于将临时绑定到构造函数中的引用成员的虚假警告

    据我所知 如果临时对象绑定到构造函数的初始值设定项列表中的引用成员 则该对象将在构造函数返回时被销毁 However 考虑以下代码 include
  • 使用 jQuery 重新排序 div

    是否可以使用 jQuery 对某些 div 重新排序 我有几个 div 我想根据 div 中的数据索引号在页面加载时重新排序它们 Now div class score 3 div div class score 2 div div cla
  • 如何在 Vagrant 和 Homestead 中回滚 PHP 版本?

    因此 我的公司正在使用 PHP 和 Laravel 为客户进行软件开发 我是公司的新人 正在使用 VirtualBox 设置较新的 Macbook 使用 Homestead 和 Vagrant 设置 Laravel 我已经完成了所有设置 以
  • Lua 中对 Table[] 进行排序

    我有一个 Lua 表 我正在尝试对其进行排序 该表的格式如下 tableOfKills PlayerName NumberOfKills 这意味着 例如 如果我有一个名为 Robin 的玩家总共击杀 8 次 而另一个名为 Jon 的玩家总共
  • TFS2012 - 无法上传大于 5MB 的文件

    我正在虚拟机 Windows Azure 内 上运行 TFS 2012 安装 一切工作正常 除了无法签入大于 5MB 的文件 在客户端 它显示 请求已中止 请求已取消 在服务器端 事件日志包含一条错误消息
  • 更改两级 DropdownButtonFormField :应该只有一项具有 [DropdownButton] 的值

    尽管这里有多个条目似乎有类似的问题 但我无法让它真正发挥作用 我有两个依赖的 DropdownButtonFormFields 的设置 其中第二个在第一个列表更改后更改为另一个列表 我能够将问题分解为第二个选择的选定值的持续剩余 我预计它会
  • 如果我使用 APNs 身份验证密钥,是否需要 APNs 证书?

    我正在使用 Flutter 和 Firebase 编写一个跨平台应用程序 我一直致力于发送通知 它在 Android 上完美运行 我通过 firebase 管理功能 sdk 发送消息 没有任何问题 该请求如下所示 const payload
  • 'UITableView' 没有 @interface 声明选择器 'initWithStyle:reuseIdentifiers

    我是 iOS 开发新手 正在寻求有关 UITableView 问题的帮助 好吧 我正在研究有关 UITableView 代码的所有内容 并且在开发过程中 当我尝试重用标识符时 如果界面上没有要创建的单元格 XCode 会显示以下消息 UIT
  • gnuplot:图例隐藏在数据后面

    我是 gnuplot 的新手 在绘制堆积直方图时 我发现图例隐藏在数据后面 有没有办法将图例放在数据上方 非常感谢你的帮助 编辑 我目前正在使用设置键外部底部将图例放置在外部 但这不是我想要的最佳解决方案 最近的版本允许将图例的背景设为白色
  • Unity3D 中 Update() 循环方法内的执行顺序

    我正在尝试找到合适的词语来描述我遇到的问题 希望这能解释问题 我有两个Update 两个不同类中的方法 并且一个类中的某些功能依赖于另一个类中的数据 代码 A 依赖于代码 B 的数据 使用调试日志 我发现代码B的Update 在代码 A 之
  • 如何从 HttpPost Create 操作方法中了解选定的复选框?

    我之间有多对多关系Student and Course 链接实体集是Enrollment 为了简单起见 它们都定义如下 Models public class Course public int Id get set public stri
  • 共享指针递归删除递归数据结构导致堆栈溢出

    我有许多长链接列表 它们最多有 20 000 个项目 它们有不同的起点 但最终可以从某个节点开始指向同一个节点 我决定让这样的链表一起成长并共享它们之间的记忆 这就是为什么我决定使用共享指针实现链表 include
  • Google 或其他搜索引擎执行 JavaScript 吗?

    我只是想知道 Google 或其他搜索引擎是否在您的网页上执行 JavaScript 例如 如果您使用 JavaScript 设置标题标签 Google 搜索引擎会看到它吗 出于 SEO 目的进行的一些实验表明 至少大公司 例如 Googl
  • 将 IDENTITY 转换为数据类型 int 时出现算术溢出错误 [已关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 通过 vb 代码运行时 将 IDENTITY 转换为数据类型 int 时发生算术溢出错误 但是当我执行存储过程时 数据会
  • AngularJS - 获取字段的标签文本

    Question 我想知道 AngularJS 获取字段标签的 最佳实践 方法是什么 使用 jQuery 您只需使用 label for 查询进行查询 然后提取文本 虽然用 AngularJS 可以做到这一点 但总感觉有些不对劲 假设您的
  • 使用 Python 将带有内嵌图像的电子邮件发送到 Gmail

    我的目标是使用 Python 向具有内嵌图像的 Gmail 用户发送电子邮件 无法在线托管此图像 然后通过href 由于图像的敏感性 来自我的工作的数据 我尝试过编码base64版本变成HTML然后发送的是HTML 但众所周知这是行不通的