Working with the WordPress email function wp_mail

 Most people are completely unaware of how the WordPress wp_mail function works, how their hosting plan plays a role in tracking down email issues, and what to do to fix things so that wp_mail sends email with some reliability.   This article discusses how to track down some common problems sending email with wp_mail, how to troubleshoot wp_mail problems, and some solutions to common issues with wp_mail.

What is wp_mail?

wp_mail is the function the WordPress uses to send email.  A large number of plugins rely on it as well since it is a part of WordPress.  So when things don’t work right, it can be difficult to track down the real issue if you don’t know where to start.

wp_mail is essentially a wrapper for the PHPmailer class, a php class that allows sending of email via php script.  It also happens to be a pluggable function, which means that you can create your own mail function and replace wp_mail altogether (but that’s a fairly advance project).

Between my own personal troubleshooting and handling support needs of my plugin customers, I have accidentally become a bit of a wp_mail expert and I have written a few posts on how to troubleshoot wp_mail, as well as some possible fixes and solutions to common problems.


How do I know wp_mail is working?

If you are having trouble with emails in WordPress, whether from WP itself, or from a specific plugin, it is important to know if wp_mail is working or not.  If you don’t know whether the function is able to send email, then you don’t know if your plugin is broken or if it’s a WP problem.

I have written a simple test script you can use to see if wp_mail can actually send email.  This can be quite helpful in troubleshooting.  If wp_mail is functioning, then you know your problem lies elsewhere; but if you can’t even get a valid email sent through wp_mail, that can tell you a lot, too.

If wp_mail is not working, what can I do?

If wp_mail is not sending emails, then you need to figure out why.  This can be the result of hosting requirements and restrictions, shared server issues, and other things.

I’ve put together a post that has more information on understanding script-based email and what some of the issues can be.  Some of the problem might be requirements by your host.

What are the possible solutions to problems with wp_mail?

The easiest and best solution, in my opinion, is to use SMTP to send email.  An SMTP server is an actual email server, which is going to be more reliable than using your webserver’s process for sending script-based email.

I have put together two posts on this.  The first discusses how to handle this without a plugin.  It is not a difficult process, and I prefer this method (and use it myself) over loading YAP (Yet Another Plugin).  The other possibility is of course to use a plugin – and there are several for doing this (see the last half of this post).

Join in the discussion