Is WordPress telling you that it is not set up to send emails? Read on.
This problem took me roughly 20hrs to get to the bottom of (I’m not embarrassed). If that sounds extreme, good! This is the type of problem that keeps you up at night. When you check so many different logs, so many times and still nothing.
In my defense, I was very strict about my standards for a solution. For example, I did not want to use a plugin (SMTP plugin or otherwise — unless absolutely necessary), and I didn’t want a duct tape fix, I actually wanted to get to the bottom of the issue and implement the most efficient fix possible (spoiler alert — I succeeded).
WordPress Email Localhost Issues
Actually, a big portion of the time was spent researching debugging methods. It’s ridiculous how much time I spent to fix this and what’s even more ridiculous is how simple the fix is.
The problem was that WordPress was trying to send an email form => wordpress@localhost. Which of course!! Is not a valid email address!!
Conveniently, that didn’t show up on ANY logs. How did I find out the issue? I installed the Check & Log Email plugin.
I also found another helpful script for debugging wp_mail. The implementation that worked well for me was as follows:
add_action( 'wp_loaded', 'mail_debugger' );
function mail_debugger() {
$res = wp_mail($to, $subject, $message);
debug_wpmail($res); // Will print_r array of errors
}
What’s the solution? Tell WordPress which email address to use in the wp_mail function. Fortunately, there’s an easy-to-use filter for that!
add_filter('wp_mail_from', 'get_admin_email');
if(!function_exists('get_admin_email')) {
function get_admin_email($email) {
$wp_from = get_option('admin_email');
return $wp_from;
}
}
The code above tells WordPress to use the admin email address as the from address. If you want to use a custom email address, just return something else (see the example snippet below).
add_filter('wp_mail_from', 'get_admin_email');
if(!function_exists('get_admin_email')) {
function get_admin_email($email) {
return 'your-custom-email@gmail.com';
}
}
You may also find some of my other configurations helpful. See below:
[sendmail]
smtp_server=localhost
smtp_port=25
smtp_ssl=none
error_logfile=error.log
debug_logfile=debug.log
auth_username=your-custom-email@gmail.com
auth_password=your-custom-password
force_sender=your-custom-email@gmail.com
sendmail.ini (complete file) — Located in W:\xampp\sendmail
[mail function]
SMTP=localhost
smtp_port=25
sendmail_from=your-custom-email@gmail.com
sendmail_path = "\"W:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
mail.log = "W:\xampp\php\mklogs\phpmail.log"
php.ini (partial file) — Located in W:\xampp\php
It’s 1:20 in the morning as I type the sentence you just read. Yes, please read it again. Was it a waste of time troubleshooting this problem?
Not at all, as much as I wish I hadn’t spent so much time fixing what turned out to be a ridiculous problem, I learned so much about the phpmailer, SMTP, and Mercury (which I had never used before), and a whole lot more.
Hopefully, you found this post before spending 20 hours that you can never get back (I know you did because you’re not crazy).
Found this post about “Why WordPress is not sending emails on localhost” helpful? Let me know in the comments below 🙂 .
I too have had issues sending email from localhost. I decided to use SMTP and use one of my external accounts: https://www.damiencarbery.com/2019/01/send-wordpress-emails-with-smtp/
Awesome! I like the tip in your article about using “Mail Tester” 👌