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
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
6 Answers
Reset to default 13Wordpress 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 your mv_optin_mail
function to a minimum in order to see if the wp_mail
functions works.
Example:
$mailResult = false;
$mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' );
echo $mailResult;
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:
function mv_optin_mail_backup( $id, $data ) {
And add a temporary function with the same name to mess around with like so:
function mv_optin_mail( $id, $data ) {
$mailResult = false;
$mailResult = wp_mail( '[email protected]', 'test if mail works', 'hurray' );
echo $mailResult;
}
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):
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->isSMTP();
//$phpmailer->Host = 'smtp.example';
// $phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
$phpmailer->Port = 25;
// $phpmailer->Username = 'yourusername';
// $phpmailer->Password = 'yourpassword';
// Additional settings…
//$phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server
$phpmailer->setFrom( "[email protected]", "From Name" );
$phpmailer->addAddress( "[email protected]", "Your name" );
$phpmailer->Subject = "Testing PHPMailer";
$phpmailer->Body = "Hurray! \n\n Great.";
if( !$phpmailer->send() ) {
echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
echo "Message sent!";
}
}
You can use the 'wp_mail_failed' action to catch a send error.
https://developer.wordpress/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 uses wp_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:
function wpse_100047() {
echo wp_mail( '[email protected]', 'WP Mail Test', 'Mail is working' );
}
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 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:
add_action('wp_mail_failed', function ($error) {
wp_die("<pre>".print_r($error, true)."</pre>");
});
Very interesting suggestions were in this document.
https://github/PHPMailer/PHPMailer/wiki/Troubleshooting
In particular working with Azure Virtual Machine the SELinux config was blocking the outgoing connections.
If you see an error like
SMTP -> ERROR: Failed to connect to server: Permission denied (13)
, you may be running into SELinux preventing PHP or the web server from sending email. This is particularly likely on RedHat / Fedora / Centos. Using thegetsebool
command we can check if the httpd daemon is allowed to make a connection over the network and send an email:getsebool httpd_can_sendmail getsebool httpd_can_network_connect
This command will return a boolean on or off. If it's off, we can turn it on:
sudo setsebool -P httpd_can_sendmail 1 sudo setsebool -P httpd_can_network_connect 1
If you're running PHP-FPM via fastcgi, you may need to apply this to the fpm daemon rather than httpd.
mv_mail_token()
and what does it return and where does the constantsender_signature
come from and what does it contain? – kaiser Commented Jun 23, 2013 at 18:01