使用bash发送S / MIME加密的html电子邮件

时间:2018-02-28 14:13:38

标签: bash html-email smime

如何通过命令行发送加密 html格式电子邮件?这是我到目前为止的代码:

# Encrypt email with a certificate
openssl cms -encrypt -in "/tmp/email_to_be_sent.html" -out "/tmp/encrypted.txt" -from $SENDER -to $RECEIVER -subject "Test: Encrypted message" -des3 "/tmp/$CERT.pem" 
# Send the encrypted email
cat "/tmp/encrypted.txt" | sendmail -f $SENDER $RECEIVER

生成的加密电子邮件/tmp/encrypted.txt如下

To: recipient@mail.com
From: sender@mail.com
Subject: Test: Encrypted message
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/pkcs7-mime; smime-type=enveloped-data;name="smime.p7m"
Content-Transfer-Encoding: base64

MIIDjAYJKoZIhvcNAQcDoIIDfTCCA3kCAQAxggFZMIIBVQIBADA9MDcxHDAaBgNVBAoME0V1cm9wZWFu
AxAlApQsmjzCwQoonT57JetCp7DHJdHWU1bkLIZWPPBRwa2EB0ZdxOXIvtg7rJavnnbxeTghblM45Pur
A+6BDKJbWvXFyxb...

问题是,一旦进入收件人收件箱并解密,邮件就不会被html格式化,而且<html><body></body></html>之类的html代码仍然可以在邮件中读取。

2 个答案:

答案 0 :(得分:1)

S / MIME要求原始邮件已封装。这意味着原始邮件已加密,并且此事实和加密类型将添加到外部邮件头中,因此客户端知道如何处理邮件内容。

因此,定义原始邮件格式的邮件头必须位于S / MIME信封内,因此客户端在解密邮件后知道它是哪种内容类型。

正确的方法是从原始邮件中提取这些标头,然后在原始邮件正文之前添加它们。请注意,这些标头必须从第一行开始,并且在这些标头之后,在原始邮件正文开始之前需要一个空行。

应移入封装消息数据的标题是

  • MIME-版
  • 内容类型
  • 内容传输编码

&#34;已移动&#34;意味着它们应包含在从外部邮件头中删除的封装邮件数据中。

其余标题应保留在信封邮件中。然后,openssl cms -encrypt命令将根据S / MIME加密消息的需要添加上述标头。

实施例

原始信息

From: someone@somedomain.net
To: receipient@otherdomain.net
Subject: It's a test
MIME-Version: 1.0
Content-Type: text/plain;
    charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Custom-Header: Additional data

This is the message text.

Good night.

加密前移动标题(注意附加空白行)

From: someone@somedomain.net
To: receipient@otherdomain.net
Subject: It's a test
X-Custom-Header: Additional data

MIME-Version: 1.0
Content-Type: text/plain;
    charset=UTF-8
Content-Transfer-Encoding: 7bit

This is the message text.

Good night.

加密后的消息

From: someone@somedomain.net
To: receipient@otherdomain.net
Subject: It's a test
X-Custom-Header: Additional data
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
Content-Transfer-Encoding: base64

MIJ5lAYJKoZIhvcNAQcDoIJ5hTCCeYECAQAxggHZMIIB1QIBADCBvDCBtjEaMBgG
A1UEAwwRc2F2aWduYW5vIENFUlQtaTIxJTAjBgNVBAoMHHNhdmlnbmFubyBzb2Z0
d2FyZSBzb2x1dGlvbnMxHjAcBgNVBAsMFUNlcnRpZmljYXRpb24gU2VydmljZTEL
(more encrypted data removed)

答案 1 :(得分:0)

所以Stefan的评论引导我找到解决方案。 未加密的电子邮件/tmp/email_to_be_sent.html在加密之前应该有这样的标题:

To: recipient@mail.com
From: sender@mail.com
Subject: Test: Encrypted message
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8

<html><body><p> test message </p></body></html>

请注意,电子邮件标题和html代码之间需要换行符。