Question

It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?

The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?

Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.

Was it helpful?

Solution

I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).

The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.

I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.

Also very important:

  1. IP address of the box to send email not already being blacklisted.
  2. make sure postmaster@emailsendingdomain.com is a valid email box
  3. if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.

Here is the email ip address blacklist checker that I used.

Those 5 things will solve perhaps 95% of your email deliverability issues.

This Guide for Fedora/dkim-milter/postfix is also very good.

The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.

Best of luck to you.

OTHER TIPS

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.

  1. 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.

  2. 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
    
  3. 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 <myname@mydomain.com>';
    $to = 'First Recipient <recipient1@domain.com>';
    $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('recipient1@domain.com', 'recipient2@domain.com');
    $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.

Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.

Here some example:

// Create mailDomainSigner Object
include_once './lib/class.mailDomainSigner.php';

$mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
$new_data = $mds->sign(
                $mail_data,
                "Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
                true,true,false);

A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :

https://sourceforge.net/projects/dkim-class-php/

Example :

include_once('dkim.class.php');
$dkim = new DKIM();
$dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
mail($to, $subject, $message, $dkim_header.$headers);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top