PHP Implementation of DKIM

Installation

  1. Simply copy dkim.php and dkim-cfg-dist.php to the same directory as the PHP files of your application.
  2. Rename dkim-cfg-dist.php into dkim-cfg.php and configure it (see below)

Configuration

While DKIM relies on cryptographic signatures, it is quite easy to configure but it requires the use of OpenSSL on the command line (from your web server or on any platform).
  1. Generate the RSA private key (in the example key size if 384 bits which is very small and not very secure but it makes the DNS step easier):
    openssl genrsa -out key.priv 384
    
  2. Generate the RSA public key from the new RSA private key:
    
    
  3. Copy and paste the private & public keys into dkim-cfg.php

    in PHP variables $open_SSL_priv and $open_SSL_pub (note: this means that your private key is readable to anybody able to read dkim-cfg.php)
  4. Configure the remaining items in dkim-cfg.php:
    • $DKIM_d: this your email domain
    • $DKIM_s: the selector, you can choose anything there (respecting the DNS syntax -- like no white space), it allows you to have several DKIM signers/servers for the same email domain
  5. You now have to configure the DNS zone file of your domain. The easiest is to have a look into dkim-test.php and call the function BuildDNSTXTRR() from the command line (or through the web). The exact content of a DNS resource record (RR) of type TXT (mandatory) is displayed and must be entered into your zone file. Depending on your DNS server settings, you may have to wait minutes or hours before the change is propagated world-wide.
  6. That's all Folks ;-)

Using PHP-DKIM

Again, the dkim-test.php file contains an example of sending email with PHP-DKIM. The test email is sent to DKIM testing reflectors, this is your email will bounce with the status of your DKIM signature.

The basic PHP-DKIM usage for an HTML e-mail is:

$sender='john@example.com' ;
$headers="From: \"Fresh DKIM Manager\" <$sender>>\r\n".
	"To: $to\r\n".
	"Reply-To: $sender\r\n".
	"Content-Type: text/html\r\n".
	"MIME-Version: 1.0" ;
$headers = AddDKIM($headers,$subject,$body) . $headers;

$result=mail($to,$subject,$body,$headers,"-f $sender") ;
The core function is AddDKIM which generates the DKIM-Signature: heading (which must preceede the other headers)).
Last update September 2008, Eric Vyncke eric at vyncke dot org.