S/MIME 第 2 部分:通过 SparkPost 签名、密封和交付
鸟
2018年12月31日
电子邮件
1 min read

关键要点
Premise: Part 2 moves from S/MIME theory to practice — demonstrating how to digitally sign and encrypt emails using SparkPost as the delivery platform.
Goal: Equip developers with a lightweight, command-line workflow to sign outgoing messages, verify signatures, and (optionally) encrypt content for specific recipients.
Highlights:
Setup: Install the open-source demo tools from GitHub (with automated Travis + pytest checks). The Pipfile handles all Python dependencies.
Sender keys:
Create a self-signed or CA-issued certificate (
.p12) for your email identity.Split it into private (
.pem) and public (.crt) files for use by the signing tool.
Signing:
Use the included
sparkpostSMIME.pyscript to sign test messages (e.g.,tests/declaration.eml).Confirm signatures visually in clients like Thunderbird (red dot icon).
Encryption:
Obtain each recipient’s public certificate (
.crt).Run the tool again to produce a signed + encrypted message.
Recipients can verify and decrypt using their private keys.
Delivery via SparkPost:
Configure a valid sending domain and API key.
Send messages through SparkPost’s API with tracking disabled to preserve integrity.
Bonus utility – mimeshow:
Displays human-readable RFC822 MIME structure for debugging or inspection.
Practical tips:
Keep filenames aligned with the From: address.
Avoid modifying message bodies after signing.
Use Bcc only for archival copies — those recipients can’t decrypt if the mail is encrypted to a single To address.
Q&A 精华
Why use S/MIME signing?
It authenticates the sender and ensures message integrity — clients like Thunderbird show a visual indicator when the signature is valid.
How do I get my sender certificate?
Either self-sign via OpenSSL (for testing) or obtain a trusted certificate from providers such as Comodo (free for non-commercial use).
Can I encrypt messages for multiple recipients?
Only if you have each recipient’s public key. The demo script encrypts to the single To address by default.
What safeguards keep signatures from breaking in transit?
The tool sets SparkPost API options for transactional sending and disables open/click tracking, so the payload passes through unchanged.
What’s the role of mimeshow?
It parses raw email files and prints their multipart structure — useful for inspecting S/MIME signatures, attachments, and headers.
What’s next in the series?
Part 3 extends these S/MIME capabilities to on-premises secure email platforms such as PowerMTA and Momentum.
在第一部分中,我们快速介绍了 S/MIME,查看了在一系列邮件客户端中对我们消息流的签名和加密。S/MIME 消息可以被签名(提供发件人身份的证明)、加密(保持消息内容的秘密)或两者兼而有之。

在本期中,我们将:
安装一些用于签名和加密电子邮件的简单命令行工具
获取您的发件人密钥/证书以进行签名
通过SparkPost发送签名消息,并查看接收到的消息
(可选)获取您的收件人证书以进行加密
通过SparkPost发送签名和加密的消息,并查看接收到的消息
尝试一个方便的独立工具“mimeshow”查看电子邮件文件内部结构。
好的——让我们开始吧!
2. 获取您的发件人密钥/证书以进行签名
3. 通过 SparkPost 发送签名消息
现在让我们使用一个真实的发送域,根据 SparkPost New User Guide 的指导进行设置。我们在当前目录中有发送者证书和密钥文件:
文件 tests/declaration.eml 包含在项目中。它只是一个文本文件,因此您可以自定义 From: 地址以适应您自己的发送域,并自定义 To: 地址以适应您的测试接收者。文件的开头如下所示:
To: Bob <bob.lumreeker@gmail.com> From: Steve <steve@thetucks.com> Subject: Here is our declaration MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB 当在人类事件的发展过程中有必要的时候……
设置您的 API 密钥:
发送电子邮件:
您会看到:
已打开到 https://api.sparkpost.com/api/v1 的连接 发送 tests/declaration.eml From: Steve <steve@thetucks.com> To: Bob <bob.lumreeker@gmail.com> OK - 在 1.15 秒内
大约一秒钟后,电子邮件到达 Bob 的收件箱。Thunderbird 用信封上的红点显示它,表示有效的发送者签名。

成功!喝完那杯咖啡,你值得拥有。如果您遇到困难,请检查电子邮件文件中的 From: 地址是否与您的 .crt 和 .pem 文件名称匹配。
4. 加密消息
要加密消息,您需要收件人的公钥以证书形式。这是一个文本文件,看起来像这样:
Bag Attributes friendlyName: s COMODO CA Limited ID #2 localKeyID: 32 84 AB 9C 56 5C 80 C6 89 4D 40 46 DD D4 7C 71 E8 CD ED C1 subject=/emailAddress=bob.lumreeker@gmail.com issuer=/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Client Authentication and Secure Email CA -----BEGIN CERTIFICATE-----这里看起来像是随机字符-----END CERTIFICATE-----
在tests目录中有一个为bob@example.com的虚拟收件人证书,因此您可以在拥有真实证书之前进行练习:
您将看到:
您会注意到输出的长度比加密消息要长得多,因为它包含大量额外信息以及消息的加密内容。
4.1 通过SparkPost发送加密签名的消息
让我们向一个真实的电子邮件地址发送加密消息。您可以按照与之前相同的流程(自签名或使用Comodo等提供商)获取您自己收件地址的公钥/证书。您只需要.crt文件——收件人不需要给您他们的私钥(.p12和.pem文件)。
我有我预期的收件人的文件bob.lumreeker@gmail.com.crt,与我文件中的From:地址相匹配。
以下是发送的命令:
我看到:
Opened connection to https://api.sparkpost.com/api/v1 Sending tests/declaration.eml From: Steve <steve@thetucks.com> To: Bob <bob.lumreeker@gmail.com> OK - in 1.168 seconds
邮件在Thunderbird中显示,带有“红点”签名图标和“挂锁”加密图标。

您可以像在第1部分中显示的那样轻松发送带有链接和图片的复杂HTML电子邮件。某些客户端(如Thunderbird)请求许可以显示加密S/MIME消息中的外部链接和图片,但仅签名消息在包括Thunderbird和Gmail在内的客户端中显示良好:


注意下拉菜单显示“已验证的电子邮件地址”。
进一步的想法和需要注意的事项
这个工具采用了超简单的方法来拉取必要的密钥——它只是在当前目录中查找命名文件。更复杂的安排,例如将所有密钥保存在数据库中,可以很容易地添加,但我希望代码尽可能简单。
您可以包含其他收件人使用 Cc: 和 Bcc:,他们将会收到邮件;这对归档目的可能很有用。签名的邮件会被接收,其他收件人可以连同签名一起展示。工具会从已发送的邮件中去掉 Bcc: 标头(就像桌面邮件客户端一样)。
为了确保邮件通过 SparkPost 时不被更改(这可能会破坏签名),工具设置了 “transactional” 邮寄的 API 选项,并禁用了打开和点击跟踪。
如果您使用加密,请记住,该工具会为此提取单一的 To: 地址。只有当其他收件人拥有 To: 收件人的私钥时,才能解码邮件正文。如果您只是使用次要收件人作为发送记录,例如,那可能也是可以的。
签好、封好、送达...I’m yours
这是我们关于如何通过SparkPost签署、封装和传递S/MIME消息的快速概述。快速提醒:演示项目可在GitHub上获得, 我已尽力使其易于安装和使用。
额外功能:使用“mimeshow”显示MIME部分
RFC822 MIME多部分文件的内部结构对人类来说相当复杂。项目包括一个独立工具,称为mimeshow,以简化这一过程。
这可以处理您拥有的任何电子邮件文件(不仅仅是S/MIME文件)并显示内部结构。这是一个例子:
您将看到:
您也可以使用它作为过滤器,以提供sparkpostSMIME输出的可读总结:
您将看到:
最后…
总结一下——我们已经安装了一些用于签署和加密电子邮件的简单命令行工具(GitHub repo 包括完整的安装说明)。
我们获得了用于签署的发件人密钥/证书,并通过 SparkPost 发送了签名消息。我们获得了用于加密的收件人证书,然后通过 SparkPost 发送了一封签名和加密的消息。
最后,我们尝试了方便的独立工具“mimeshow”来查看电子邮件文件内部。
就这样了!在我们的下一期内容中,我们将向您展示如何将这些 S/MIME 功能扩展到 本地安全电子邮件平台,如 PowerMTA 和 Momentum。再见!






