Hi Mat & Bob,
Most servers these days should come with OpenSSL installed, which is what we need for the encryption.
Next, due to the way pki (public key interchange) works, the recipient needs a signed certificate (public key) made available to the mailer (your server), and a private key on his/her email client. The Personal E-mail Certificate you mentioned in your first post should cover this, as long as it's your client's certificate.
So, what to do next;
Well, CF does not support generating S/MIME (pkcs7) email, so you'll either have to edit that part of CF, or do the emailing manually in the "on submit - after email" box (don't worry 'bout the name, it'll be run even though we disable the builtin email in CF). I'll take the second approach for this, as rewriting CF itself would probably be a more challenging task...
First off, we need the signed certificate uploaded on the server (we could also store this in the form code itself, but lets keep the code tidy for now...). We'll call it 'client_email_cert.x509'.
<?
$key = file_get_contents('client_email_cert.x509');
?>
Since we're not using CF email, we also need to generate a message:
<?
$msg = "Hello Client,
There's been a new submission to our form, the details are as follows:
Name: " . JRequest::getString('name') . "
Email: " . JRequest::getString('email') . "
Have a nice day.";
?>
In the code above, we use JRequest::getString() to get the submitted data and insert them into our message.
Now, we get to the funny part of actually creating the crypted message:
<?
//we've got our key in $key, and message in $msg..
$parm = array(
'To' => 'yourclient@somesite.it',
'From' => 'Yoursite <webmaster@yoursite.it',
'Subject' => 'Submitted form data'
);
//Create a file to hold our original message and our encrypted message:
$config = JFactory::getConfig();
$orig = $config->getValue('config.tmp_path').DS.uniqid('txt');
$crypt = $config->getValue('config.tmp_path').DS.uniqid('enc');
$fd = fopen($orig, "w");
fwrite($fd, $msg);
fclose($fd);
if (openssl_pkcs7_encrypt($orig, $crypt, $key, $parm, PKCS7_TEXT, OPENSSL_CIPHER_RC2_128))
{
//Encryption successful, proceed to send the email:
//Unfortunately, the PHPMailer used by Joomla is too limited to allow us to do this in Joom-space..
//and using the mail() function rendered issues with reading the From: header... so we'll go for sendmail here...
exec(ini_get("sendmail_path") . " < " . $crypt);
//And remove the original and encrypted files from the filesystem, just to make sure noone finds them.
unlink($crypt);
}
unlink($orig);
?>
As mentioned in the comments, I've ended up manually calling sendmail. This might not be supported on all systems, in which case you'll probably have to use some smpt-mailer or pray the mail() command will work. We cannot use the JMail class in joom-space, as this will override a few important headers we need to inform the mail client that it's an encrypted email.
/Fredrik