WordPress has its own email function, wp_mail, that it uses for sending email. There are many plugins, my WP-Members plugin included, that rely on this function to be working properly. WordPress does not provide email settings, so how do you know if this function is working?
Suppose you have some plugins or something in your site that should be sending you emails and you are not receiving any emails. Is something broken in the plugin? Or is it a case of wp_mail not functioning properly? You can’t begin to track your issue until you know where to be looking.
This article will explain how you can run a test script on your site in order to test your WordPress email settings to see if the wp_mail function is working and sending email.
The wp_mail function accepts the following arguments:
- $to (required) – This is the email address you wish to send to
- $subject (required) – The email subject line
- $message (required) – The email message body content.
- $headers (optional) – This is an optional argument. It could contain things like defining plain text or html email, or the “from” name of an email
- $attachments (optional) – Another optional argument. This one allows for an email attachment.
In general, you need to focus on the first three required arguments. If things work OK with those, wp_mail is functioning. If not, further testing may require looking into the $headers a little more. Some hosts require that valid “from” headers be used, so that is something that should be checked.
I’ve put together a simple little script that allows you to test your WordPress email settings by attempting to send an email through the wp_mail function. To use this testing script, save it as a php file and load it to your WP root directory via FTP.
Set your email address as indicated in the example script, save the file as mailtest.php, then browse to it directly in your browser to fire the wp_mail function (http://yourdomain.com/mailtest.php). You will get a result on screen, and if all is functioning, you will receive a test message in your email inbox. If you get an error message or you don’t receive the email, then you know there is an issue with wp_mail.
Here is the script. I plan to put this together in a utility plugin at some point in the future, but for now, you can save it as a php file and run it from your WP root folder.[gist https://gist.github.com/butlerblog/5c9b805529c419b81447 ]
Now, this post and this script are focused on determining if wp_mail is working. Troubleshooting is another matter altogether. For some next steps, this post has some things to know when troubleshooting wp_mail, and it includes some possible solutions.
Incidentally, since this post was originally written, I have become one of the compontent maintainers for WP’s mail component (which includes wp_mail()). Also, I have identified and documented a number of filters in wp_mail for the codex.
For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:
- Troubleshooting wp_mail WordPress Email Configuration – not everything that can go wrong is directly a problem with WP. This post has information on host restrictions and other outside problems that should be checked.
- WordPress Email Settings: Changing the wp_mail address with a simple plugin – here is a very simple and lightweight script you can load as a plugin to change the email address that WordPress sends email from.
- Changing the wp_mail from address in WordPress without a plugin – provides a simple code snippet you can use to change the email address that WordPress sends from, no plugin required.
- Easy SMTP email settings for WordPress – how to change your WordPress email configuration to send email from a valid SMTP server with a simple script, no plugin required.