用PHP签署的“签名”电子邮件

时间:2009-09-28 15:24:54

标签: php email encryption cryptography

如何使用PHP“签署”外发电子邮件?

我正在看的正确标题是:

signed-by   mydomain.com

2 个答案:

答案 0 :(得分:4)

如果我理解正确,您希望使用PHP生成签名的电子邮件。

发送签名电子邮件的标准方法是S / MIME(另一种不太常见的方式是PGP)。 S / MIME基本上是包含CMS消息的base64编码的MIME消息(CMS有时也称为PKCS#7)。

从PHP执行此操作的一种方法是使用PHP绑定到OpenSSL的openssl_pkcs7_sign

我不知道应该使用sign-by标头。

答案 1 :(得分:1)

如果您有.p12文件,那么您可能需要从中提取公共证书和私钥:

私钥:

openssl pkcs12 -in <YOUR_FILE>.p12 -nocerts -out privateKey.pem

公共证书:

openssl pkcs12 -in <YOUR_FILE>.p12 -clcerts -nokeys -out publicCert.pem

然后您可以使用以下PHP代码发送已签名的电子邮件:

<?php

$data = "This is a test signed email";

// create a temporary file
$fp = fopen("msg.txt", "w");
fwrite($fp, $data);
fclose($fp);


// sign the email
openssl_pkcs7_sign("msg.txt", "signed.txt",
    file_get_contents(realpath("publicCert.pem")),
    array(file_get_contents(realpath("privateKey.pem")), '<YOUR_PASS>'),
    array(
        "To" => "john@example.com",
        "From: Jane Doe <jane@example.com>",
        "Subject" => "This is a test"
        )
    );



exec(ini_get("sendmail_path") . " < signed.txt");