I’m trying to use wp_mail
(testing on local machine) but no mail is received.
The php.ini
has smtp_port = 25
set and the php mail()
is working so far.
- how can I check if wp_mail is working
- what can fail
Here is the code of my mail function:
function mv_optin_mail($id, $data){
$url = $id."-".mv_mail_token($id, $data['token']);
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
add_filter( 'wp_mail_charset', 'utf8' );
$headers[] = 'From: '.sender_signature.' <'.noreply_address.'>';
ob_start();
include("optin-mail.php");
$html_mail = ob_get_contents();
ob_end_clean();
wp_mail( $data['email'], 'Some Subject', $html_mail, $headers );
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
remove_filter( 'wp_mail_charset', 'utf8' );
}
I don’t get any errors. Is there a way to toggle error-loggin for wordpress?
The noreply_address
is noreply@root
WordPress relies on the PHPMailer class to send email through PHP’s
mail
function.Since PHP’s
mail
function returns very little information after execution (only TRUE or FALSE), I suggest temporarily stripping down yourmv_optin_mail
function to a minimum in order to see if thewp_mail
functions works.Example:
Since you’ve tested PHP’s
mail
function already, the mail should arrive.If it does not, the problem lies in the other statements of your function or in the PHPMailer class.
In cases like this, I usually rename my function to something like:
And add a temporary function with the same name to mess around with like so:
When I have figured out what the problem is, I start using the backup version again.
To send a mail using PHPMailer directly you can do something like this (not for production, just for debugging):
You can use the ‘wp_mail_failed’ action to catch a send error.
https://developer.wordpress.org/reference/hooks/wp_mail_failed/
What I usually do to test if
wp_mail()
is sending e-mails properly is just registering on my website with another e-mail address and seeing if the e-mail arrives. If it does, it means that WordPress is properly sending e-mails (it useswp_mail()
to send registration e-mails) and you should inspect your mail sending function for any errors. To do that, as @Tobias suggested, you should strip everything from your mail sending function and only leave the basic:Additionally, the e-mails sent by WordPress (as all e-mails sent by PHP’s
mail()
function might be blocked by some e-mail servers (or marked as spam) so it’s always a good idea to use SMTP (multiple plugins that do that) for e-mails on the live website.I would start by enabling WP_DEBUG in wp-config and see if that shows you anything about your code or the code for the wp_mail function. That is about it for debugging right out of the box with WP.
Also, you can use Easy WP SMTP and enable debugging and/or set that up to use SMTP. There are similar plugins on WordPress.org but I know this one has a good debug option. If something like GMail works then you’ll know it is a server setting and not this code.
If you want to stop the script and just see the $error object, try adding this BEFORE the
wp_mail()
method fires:Very interesting suggestions were in this document.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
In particular working with Azure Virtual Machine the SELinux config was blocking the outgoing connections.