In my last post, I mentioned that after the public launch of HelpEncourage.Me, many of our emails were being sent to the SPAM folder by the big three (Google, Yahoo, and Hotmail).
The first step I took to resolve this issue was adding an SPF record to authorize mail servers to send email on behalf of the HelpEncourage.Me domain. This helps prevent spoofing, since it is trivial to set whatever domain you like on your outgoing mail.
Unfortunately, implementing the SPF record by itself was not enough.
Enter DomainKeys Identified Mail
With the SPF record correctly authorizing our outgoing mail server, and the messages successfully passing the â€œSPAMâ€ content test, my only remaining option before moving to one of the dedicated mail-sending services was to implement DomainKeys Identified Mail (DKIM).
At its core, DKIM is a message-signing protocol that uses public-key cryptography to verify that a message is coming from a given domain (and has not been tampered with). It does this by creating an encrypted hash of the message contents (both the headers and body) and appending it to the message via a â€œDKIM-Signatureâ€ header. The recipient of a message can then verify the message content and sender by decrypting the message hash using the associated domainâ€™s public key.
Generating a private key
The first step in implementing DKIM is to generate a public and private key which can be used to encrypt and decrypt the message hash. Since I am on Windows, the simplest way to generate a private key is to use the Win32 OpenSSL tool. You can also check out openssl.org for more information on generating a key.
Once OpenSSL is installed, generating a key is straightforward. Simply navigate to the OpenSSL executable directory on a command line and type:
openssl genrsa â€“out private.key 1024
This will generate a 1024-bit private key file called â€œprivate.key”. The size of the key is specified in bits at the end of the command. The â€œgenrsaâ€ option instructs OpenSSL to generate a RSA key. For most purposes, 1024-bits should be sufficient and is the recommended key size for â€œcorporate useâ€ according to RSA Laboratories.
Generating a public key from the private key
Once you have a private key, you need to retrieve the public key. Again, OpenSSL makes this process rather painless:
openssl rsa â€“in private.key -out public.key -pubout
Note that we no longer want to generate a key, so we use the â€œrsaâ€ option instead of â€œgenrsaâ€. Since we arenâ€™t generating a key, we tell it to use the private key we generated earlier, create a new key file called â€œpublic.keyâ€. The â€œpuboutâ€ option tells OpenSSL to only output the public portion of the key.
Creating a DKIM record
Now that we have a public and private key, we can create the appropriate DKIM records in DNS. DKIM records consist of two TXT records. The first record has a name of â€œ_domainkey.<domain>â€ and specifies the policy for the domain given by <domain>:
_domainkey.helpencourage.me TXT o=~\; firstname.lastname@example.org
For our purposes, the important options in this record are â€œo=~â€ (note the tilde) and â€œremail@example.comâ€. The first option specifies that some emails from this domain will be signed. If you want to specify that all emails will be signed, use a minus (â€œ-â€œ) instead of a tilde. The second option specifies the â€œresponsible email addressâ€.
Once we have set the policy for our domain, we need to publish the public key. Again, we create a TXT record, but this time the name will have a <selector> prepended to it (â€œ<selector>._domainkey.<domain>â€).
The <selector> is just a unique identifier that can be used to reference the correct record. For example, for HelpEncourage.Me, we use â€œp1â€ as our selector (short and easy to remember):
p1._domainkey.helpencourage.me TXT k=rsa\; p=...PUBLIC_KEY...
There are two important options for this record. The first, â€œk=rsaâ€, specifies that we are using an RSA key. Currently this is the only algorithm supported and this option can be omitted, but I always like to be thorough in case it changes in the future. The second option is the reason for the record and is required to successfully use DKIM. It specifies the public key to use to verify the message signature, and takes the form â€œp=<PUBLIC KEY>â€.
To get the appropriate value for this option, you must use the public key file we created above and copy all the text between â€œ—–BEGIN PUBLIC KEY—–â€œ and â€œ—–END PUBLIC KEY—–â€œ. Do not include the delimiters.
Signing your messages
Once you have setup you DNS records, you can start signing your email messages. This is usually done at the server level, and if your SMTP server does not support signing your message, you may be in for some additional work. Fortunately for us, our server supports DKIM signing, and we could enable it via the control panel. All we needed to do was upload our private key and point the SMTP server to it.
Verifying your signature
Fortunately, many of the same tools we used to verify our SPF record can be used to verify that you have correctly setup DKIM signing. Notably, the Port25 verifier provides a wealth of verification information, and Google makes available all sorts of information via the message headers.