Kladskull
Kladskull

Reputation: 10742

"Signed" email in PHP

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

The proper header I am looking at is:

signed-by   mydomain.com

Upvotes: 2

Views: 6132

Answers (2)

Josh LaMar
Josh LaMar

Reputation: 209

If you have a .p12 file then you likely need to extract the public cert 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" => "[email protected]",
        "From: Jane Doe <[email protected]>",
        "Subject" => "This is a test"
        )
    );



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

Upvotes: 1

Rasmus Faber
Rasmus Faber

Reputation: 49649

If I understand you correctly, you want to generate a signed e-mail using PHP.

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

One way to do this from PHP is with the PHP-bindings to OpenSSL's openssl_pkcs7_sign.

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

Upvotes: 4

Related Questions