0

I am using the wp_mail function within WordPress, but it only works sometimes. When I look at the error log, it says that mail is succeeding. This function is also being used for another email being sent - but that one works (at least sometimes, anyway).

if ( wp_mail( $emailTo, $subject, $body, $headers ) ) {
    error_log('mail success');
} else {
    error_log('mail fail');
}

Note that I am sending email to my own email address - so I can check the spam folder, etc.

What am I missing?

space_food_
  • 800
  • 1
  • 7
  • 23
  • What is missing is why you draw the conclusion that "it only works sometimes", despite what the error log tells you. – KIKO Software Dec 18 '21 at 10:54
  • Because I am testing the mail and it is not sending? – space_food_ Dec 18 '21 at 10:55
  • Not sending or not received? How are you so sure it is the sending that is faulty? – KIKO Software Dec 18 '21 at 10:56
  • Exactly what the problem is unknown. That's why I'm asking the question. All I know is that sometimes, it sends emails, other times, it does not. And in the scenario where it does not, it returning true, not an error. – space_food_ Dec 18 '21 at 10:58
  • For all the mails send [only roughly 15% are accepted by the receiving end](https://dataprot.net/statistics/spam-statistics/). To have emails arrive you need to set up [SPF, DKIM and DMARC](https://www.csoonline.com/article/3254234/mastering-email-security-with-dmarc-spf-and-dkim.html). – KIKO Software Dec 18 '21 at 11:01
  • For my business, I've given up on managing my own mail server. There are webhosters who help you sending mail, but I use an external service to send mail through an API. They do all the hard work of getting mails accepted by the likes of Google and Microsoft. – KIKO Software Dec 18 '21 at 11:03

1 Answers1

1

From wp documentation: A true return value does not automatically mean that the user received the email successfully. It just only means that the method used was able to process the request without any errors. https://developer.wordpress.org/reference/functions/wp_mail/

Consider using smtp. It will be more reliable. You could write your own plugin or use existing one like "wp mail smtp"

Also as others have pointed out setting up DMARC, DKIM and SPF is a good idea.

  • In this instance, I need the mail to be sending consistently, without relying on using smtp. I did notice that it can return true without errors, but that does not mean it actually worked, however am uncertain what the potential issues would be that would cause the email not to send. Especially given that it works sometimes – space_food_ Dec 18 '21 at 11:02