"Signing" email in PHP

How would I "sign" an outgoing email using PHP?

The correct title I am looking at is as follows:

signed-by   mydomain.com

      

+2


source to share


2 answers


If I understand you correctly, you want to generate a signed email using PHP.

The standard way to send signed emails is S / MIME (another less common way is PGP). S / MIME is basically a MIME message containing the base64-encoded CMS message (CMS is also sometimes called PKCS # 7).



One way to do this from PHP is PHP bindings to OpenSSL openssl_pkcs7_sign

.

I have no idea what the signed header should be used for.

+4


source


If you have a .p12 file, you probably need to extract the public certificate and private key from it:

Private key:

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

      

Public Cert:



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

      

Then you can send a signed email with this PHP code:

<?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");

      

+1


source







All Articles