Search code examples
wordpressphpmailerdkim

Using DKIM records in Wordpress PHPMailer in a way that it survives an WP update


Has someone found a solution for using the DKIM option in PHPMailer to send emails DKIM signed without editing wp-core files?

The PHPMailer has public properties that start with $DKIM_ that can be used to send messages DKIM-signed. I know how to do that. I can hardcode the values in the PHPMailer.php file, which will work until WP gets updated and overwrites these changes.

I've also looked at /wp-includes/pluggable.php which calls the PHPMailer class but cannot find a way to hook into that to set the right DKIM property values.

Is there a plugin or another solution?

Best regards, Pieter


Solution

  • Yes. You can get your hands on the PHPMailer instance that WordPress uses via the phpmailer_init hook, and you can save your hook code as part of your site's functions.php file, which will survive updates. Docs are here, and a basic example is:

    add_action('phpmailer_init', function ($mailer){
      $mailer->isSMTP();
      $mailer->Host = "mail.example.com"; // your SMTP server
      $mailer->Port = 25;
      $mailer->SMTPDebug = 2;
      $mailer->CharSet  = "utf-8";
      //etc
    });
    

    You can set your DKIM params in there too.