Wordpress is not sending emails to Gmail

2.8k views Asked by At

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.

2

There are 2 answers

2
Vlad Preda On BEST ANSWER

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 [email protected]'); // or whatever you want
0
Jason Hendriks On

Gmail (and likely Hotmail and Yahoo eventually) is starting to disable traditional SMTP authentication mechanisms (PLAIN, LOGIN and CRAMMD5) in favour of OAUTH2.

If you send mail with WordPress (PHPMailer) alone, Gmail will discard it or mark it as SPAM. If you send mail with a typical WordPress SMTP plugin, Gmail will give you an authentication error, or make you jump through hoops like application-specific passwords, two-factor authentication, and enabling less secure apps as an account setting.

The first plugin to implement OAuth 2.0 for WordPress (disclaimer, I am the author) is Postman SMTP. If port TCP 465 allows outbound connections to Gmail, your WordPress emails will be delivered by Postman without error.