23

Currents i am using phpmailer to send mail's. now how its possible to send email in phpmailer with DKIM keys

i search in phpmailer Class file and i found the below code

    /**
     * DKIM selector.
     * @type string
     */
    public $DKIM_selector = '';

    /**
     * DKIM Identity.
     * Usually the email address used as the source of the email
     * @type string
     */
    public $DKIM_identity = '';

    /**
     * DKIM passphrase.
     * Used if your key is encrypted.
     * @type string
     */
    public $DKIM_passphrase = '';

    /**
     * DKIM signing domain name.
     * @example 'example.com'
     * @type string
     */
    public $DKIM_domain = '';

    /**
     * DKIM private key file path.
     * @type string
     */
    public $DKIM_private = '';

Can i know how its possible.

Synchro
  • 35,538
  • 15
  • 81
  • 104
Varun Sridharan
  • 1,983
  • 2
  • 20
  • 54
  • What specifically are you having trouble with? The comments seem fully explanatory. – icktoofay Jun 28 '14 at 04:02
  • I experimented with this but couldn't make it work. I opted for an OpenDKIM filter for Sendmail which has the advantage that all the outbound mail is signed –  Jun 28 '14 at 04:02

2 Answers2

45

If you take a look in the PHPMailer unit tests, there is an example of how to set up DKIM.

Here are the basics beyond what you already need to do to send a message (obviously change the domain, key path and selector to match your config, and add a passphrase if you use one); this also assumes that you are intending to sign using the same identifier as your From address:

$mail->DKIM_domain = 'example.com';
$mail->DKIM_private = '/path/to/my/private.key';
$mail->DKIM_selector = 'phpmailer';
$mail->DKIM_passphrase = '';
$mail->DKIM_identity = $mail->From;

When you send() the message (and not before), it will use these settings to generate a DKIM signature.

Dominique
  • 1,080
  • 14
  • 29
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • 1
    from where i can get the key path `/path/to/my/private.key` and what is this `$mail->DKIM_passphrase` – Varun Sridharan Jul 08 '14 at 10:38
  • 1
    If you look at the unit test code it includes key creation, but if you're signing for DKIM you will have already done that and you will have a key somewhere, but I don't know where you've put it! When you create a key pair, you can optionally encrypt the key using a passphrase, and anything that needs to use the key needs to be able to decrypt it, so it needs the passphrase, and this is where you put it. If you don't encrypt your key (as is common on servers), you don't need this. – Synchro Jul 08 '14 at 15:08
  • 2
    Is the `DKIM_selector` always `'phpmailer'` ? – ZurabWeb Mar 27 '15 at 17:06
  • 1
    No, it's whatever you set `DKIM_selector` to! – Synchro Mar 27 '15 at 17:09
  • 8
    Thanks for the help on this. For a full guide on how to setup DKIM before you get to this point i put together a guide http://yomotherboard.com/how-to-setup-email-server-dkim-keys/ – Dan Hastings Jun 05 '15 at 13:49
  • 7
    `DKIM_identifier` is now `DKIM_identity` (PHPMailer 5.2.13) – gamliela Sep 29 '15 at 13:31
  • 4
    DKIM_selector is the selector key setup on your DNS TXT record. It's not always phpmailer as in the example. – Hao Nguyen Aug 27 '17 at 05:14
  • 5
    To use a private key string instead of setting a path to a file, you can simply specify `$mail -> DKIM_private_string` property on your phpMailer object. – contrid Jul 03 '18 at 11:57
  • @Synchro my web hosting server automatically gives me a public DKIM key...where do I find it's associated 'private' key for ```$mail->DKIM_private = '/path/to/my/private.key';```? – anna Feb 16 '19 at 15:54
  • I've no idea! Ask them? – Synchro Feb 16 '19 at 15:56
  • Oh okay, I thought that it's not unusual for a web hosting server to store/provide the DKIM keys – anna Feb 16 '19 at 20:45
  • @contrid how do i use DKIM_private_string ? base64encode the key ? – Tomer Ofer Jul 25 '22 at 14:03
2

I have the following experience:

  1. The pair of the keys generated at http://dkim.worxware.com/createkeys.php is probably intended for the SHA1, while the latest version 5.2.14 of the class.phpmailer.php is intended for SHA256.
    The example above was not functional.
  2. I changed all settings and functions in the class.phpmailer.php from SHA256 on SHA1 (I replaced simply all strings SHA256 with the strings SHA1).
    My PHP script for DKIM signature has became functional.
Zuul
  • 16,217
  • 6
  • 61
  • 88
Dusan
  • 29
  • 1
  • 1
    Took me a couple of hours to find out this was my issue to! Very frustrating! I used this site to generate my keys https://www.socketlabs.com/domainkey-dkim-generation-wizard/. Working fine with the latest version of phpmailer (5.2.14) – Timo002 Mar 04 '16 at 10:22
  • 6
    Avoid the SHA1 family of functions. Practical attacks on SHA1 were published earlier this year (https://shattered.io/). There are plenty of instructions on how to generate a DKIM keypair using openssl. Also, it is generally a bad idea to have a third party website generate any security keys for you. If they store the key, they can impersonate you. Instead, generate it offline using openssl. – techdude May 30 '17 at 18:21