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. WordPress does not provide email settings, so how do you know if email is working?
Suppose you have some plugins or something in your site that should be sending emails, yet you are not receiving any messages. 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 to test your WordPress email settings. This will tell you if the wp_mail function is both working and sending email.
The wp_mail Function Explained
The 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.
Focus on the first three required arguments. If email sends with those then wp_mail is functioning. If not, further testing may require looking into the $headers. Some hosts require that valid “from” headers be used, so that is something that should be checked.
Simple wp_mail Testing Snippet
The following script that allows you to test WordPress email by attempting to send an email through your site. 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 inbox. If you get an error message or you don’t receive the email, 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.
Testing vs Troubleshooting
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 wordpress.org codex.
For more information:
- 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.