This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.
I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.
Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka
require 'dkimNEW.php';
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$subject = 'Test of DKIM';
$from = 'My Name <[email protected]>';
$to = 'First Recipient <[email protected]>';
$pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
$text = strip_tags($pbody);
// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
// create the message
$mime = new Mail_mime("\n");
$mime->setTXTBody($text);
$mime->setHTMLBody($pbody);
// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);
require 'dkimNEW.php' ;
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
// create the smtp mail object
$smtp_params = array(
'host' => 'mail.mydomain.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);
// send the message
$recipients = array('[email protected]', '[email protected]');
$mail = $smtp->send($recipients, $headers, $body);
?>
PS. Just in case you did not notice, replace values with your own!
Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.