Search code examples
phpwordpressgmailexim

Wordpress is not sending emails to Gmail


None of my Wordpress emails are sent to Gmail. More info:

  • I am using EXIM mail server
  • I am using wordpress latest version
  • it happens only when emails are sent to Gmail

EXIM logs say that the email is sent succesfully to Gmail, but they are not sent, or sent in spam.


Solution

  • The problem is a combination of factors:

    • EXIM, unlike postfix, is not automatically setting the Sender header
    • Gmail recently made some changes to deal with spam better, and if the email headers are missing the Sender header, it will most likely silently discard it, or send it as spam.
    • Wordpress doesn't set the Sender header

    Once you know these, the fix is quite simple. If you are using Wordpress, the quick and dirty way to do it is to go to wp-includes/pluggable.php, look for the wp_mail() function search for:

    $phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
    

    then add the following right after it:

    $phpmailer->Sender   = $phpmailer->From;
    

    Once you do this, emails will work, and you can fix the problem the proper way, without overwriting the core, by writing a plugin. Wordpress uses phpmailer which knows about this issue, but wordpress doesn't use it. There is also a bug report on this issue.

    To fix the problem using the core mail() function, you have to do the following:

    // $sender can be the same email address as the From header
    mail($to, $subject, $message, $additional_headers, "-f {$sender}")
    

    There is another option that may work, depending on the configuration of your server (I couldn't test it, would love if someone could test this):

    $sendmailFrom = ini_get('sendmail_path');
    ini_set('sendmail_path', $sendmailFrom . ' -f sender@mysite.com'); // or whatever you want