如何使用 Pear 邮件发送数字签名电子邮件

Posted

技术标签:

【中文标题】如何使用 Pear 邮件发送数字签名电子邮件【英文标题】:How to send digital signed email using Pear mail 【发布时间】:2011-11-13 01:40:44 【问题描述】:

有什么方法可以用 pear Mail 发送数字签名的电子邮件吗?

我盗用了这段代码,但它没有运行。

    <?php
    require_once("Mail.php");
    require_once('Mail\mime.php');

    $recipients = "petr.kostroun@xxx.cz";

    $headers["From"] = "mailer@xxx.cz";
    $headers["To"]      = $recipients;
    $headers["Subject"] = "Zkouska";

    $params["host"] = "mail.xxx.cz";
    $params["username"] = "server@xxx.cz";
    $params["password"] = "xxx";

    $body = "Zkouska Spojeni !";

    $mime = new Mail_mime();

    $fp = fopen("./msg.txt", "w");
    fwrite($fp, $body );
    fclose($fp);

    echo "Sending mail:\n";
    echo "Headers:\n" . print_r($headers);
    echo "\n\nBody:\n$body";
    echo "\n---END OF MAIL BODY---\n";

    openssl_pkcs7_sign("C:/data/www/msg.txt", "C:/data/www/signed.txt", "file://C:/data/www/04.pem", "file://C:/data/www/newkey.pem", $headers);

    $data = file_get_contents("C:/data/www/signed.txt");

    $mime->setTXTBody($data);

    $body = $mime->get();
    $headers = $mime->headers($headers);

    $headers["Content-Type"] = "Multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha1; boundary=\"------------ms070206070302000302000502\"";

    $mail_object =& Mail::factory("smtp", $params);
    $mail_object->send($recipients, $headers, $body);
    echo "Email sent";


    ?>

有人可以给我建议如何解决这个问题吗?谢谢。

** 我在互联网解决方案上找到了,在那里人们可以使用 openssl_pkcs7_sign 函数在文件中生成签名的电子邮件。我已经生成了这个文件,但我不知道如何为 $mail_object->send 函数 args 解析这个文件。特别是我不知道如何构造 headers 数组

文件内容:

MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
From: info@xxx.cz
To: petr.kostroun@xxx.cz, vysledky@xxx.cz
Subject: Zkouska
MIME-Version: 1.0
Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg="sha1"; boundary="----E3E6271C75DBBBCB30B457C24A97B23B"

This is an S/MIME signed message

------E3E6271C75DBBBCB30B457C24A97B23B
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
From: info@xxx.cz
To: petr.kostroun@xxx.cz, vysledky@xxx.cz
Subject: Zkouska

#Zkusebni podepsanej email, neodpovidejte prosim
------E3E6271C75DBBBCB30B457C24A97B23B
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"

MIIE9wYJKoZIhvcNAQcCoIIE6DCCBOQCAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3
DQEHAaCCApswggKXMIICAAIBBDANBgkqhkiG9w0BAQQFADCBnDELMAkGA1UEBhMC
Q1oxFzAVBgNVBAgMDkN6ZWNoIFJlcHVibGljMQ8wDQYDVQQHDAZQcmFndWUxDzAN
BgNVBAoMBkRhbnRlbTERMA8GA1UECwwIRW1wbG95ZWUxFzAVBgNVBAMMDkRhbnRl
bSBDQSBhdXRoMSYwJAYJKoZIhvcNAQkBFhdwZXRyLmtvc3Ryb3VuQGRhbnRlbS5j
ejAeFw0xMTA5MDgxMDQyNDJaFw0yMTA3MTcxMDQyNDJaMIGKMQswCQYDVQQGEwJD
WjEXMBUGA1UECBMOQ3plY2ggUmVwdWJsaWMxDzANBgNVBAoTBkRhbnRlbTERMA8G
A1UECxMIRW1wbG95ZWUxFjAUBgNVBAMTDVBldHIgS29zdHJvdW4xJjAkBgkqhkiG
9w0BCQEWF3BldHIua29zdHJvdW5AZGFudGVtLmN6MIGfMA0GCSqGSIb3DQEBAQUA
A4GNADCBiQKBgQCwxNLJPu46NOuGVuQSNtoOUZjN6QYoxSgpOhFtn6Ld/5MaJAaX
SLg3jm2F7NKXvazdma0MZnWiKoomekx77bHSHqSriq5PrO0FxR2V81jGpVKd2jS2
XoMEgiz2bGuy6xWIrdl/ocmMvrusRbL1N/prJGcQCI7TvLlWZr3+otauZwIDAQAB
MA0GCSqGSIb3DQEBBAUAA4GBAJS0U/u8RHlzxVId86lXF1IHdJlmUfqM5uJ0zrjE
a6g1C/7pExSpM9s7RqDkDdgHk2Ok3bl2XBmDvWNWJSsNxc9Gy77gT4qwm93og03p
FDn3ikBIhP+LddDwvre28/eVu/59R6QGMe2VEGNREWvdPHbV7KD1GPim9AL4il3j
VMtHMYICJDCCAiACAQEwgaIwgZwxCzAJBgNVBAYTAkNaMRcwFQYDVQQIDA5DemVj
aCBSZXB1YmxpYzEPMA0GA1UEBwwGUHJhZ3VlMQ8wDQYDVQQKDAZEYW50ZW0xETAP
BgNVBAsMCEVtcGxveWVlMRcwFQYDVQQDDA5EYW50ZW0gQ0EgYXV0aDEmMCQGCSqG
SIb3DQEJARYXcGV0ci5rb3N0cm91bkBkYW50ZW0uY3oCAQQwCQYFKw4DAhoFAKCB
2DAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0xMTA5
MDgxNjE5MzJaMCMGCSqGSIb3DQEJBDEWBBTW5eHrz1I9fM/YTcek/1RBvTrPqzB5
BgkqhkiG9w0BCQ8xbDBqMAsGCWCGSAFlAwQBKjALBglghkgBZQMEARYwCwYJYIZI
AWUDBAECMAoGCCqGSIb3DQMHMA4GCCqGSIb3DQMCAgIAgDANBggqhkiG9w0DAgIB
QDAHBgUrDgMCBzANBggqhkiG9w0DAgIBKDANBgkqhkiG9w0BAQEFAASBgIUpL8bB
w4UpFJEdUO2f0aIc0nm7XnEtVUPkBhhfAfikSJLjNraLmlwQq0f7R/EHdeUzutvW
nuY8A5NtqNF/Efyy8g19GEXCDgxYMgSiUn8AEhhdUvs7Qe45B3ONgLdYfmnF8yX/
w/ZrUGzcmG46dsH/HPapYFmscmbbm5G1GWdU
------E3E6271C75DBBBCB30B457C24A97B23B--

感谢您的帮助

【问题讨论】:

您两次发布代码是否有原因。这是你想要运行的吗? 当然不是,重复代码已经修复。 为 PEAR 的使用点赞! 【参考方案1】:

openssl_pkcs7_sign("C:/data/www/msg.txt", "C:/data/www/signed.txt", "file://C:/data/www/04.pem", "file://C:/data/www/newkey.pem", $标头);

我觉得你应该试试:

openssl_pkcs7_sign("C:/data/www/msg.txt", 
                   "C:/data/www/signed.txt", 
                   "file://C:/data/www/04.pem", 
                   array("file://C:/data/www/newkey.pem","CRT pass"), 
                   $headers);

并发送标头参数:

$data = file_get_contents("C:/data/www/signed.txt");
$parts = explode("\n\n", $data, 2);

// with mail() function you should
mail($to, $subject, $parts[1], $parts[0]);

// I suppose in the same way for Pear Mail
$mail_object =& Mail::factory("smtp", $params);
$mail_object->send($recipients, $parts[0], $parts[1]);

【讨论】:

以上是关于如何使用 Pear 邮件发送数字签名电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件

使用 Pear Mail 发送纯文本和 html 电子邮件(如何设置“Content-Type”并正确使用边界)

发送电子邮件时 PEAR 邮件身份验证失败

来自数据库的 PEAR MIME html 表只发送一行

使用 PEAR 邮件发送测试消息时出现错误消息

如何在php中使用ssl通过smtp发送邮件