This blog was lacking, from the very beginning, a new comment email notification. Finally, today, I’ve had enough of this feature not functioning properly. I did a research, I asked, as always, google for answers but I couldn’t get it to work easily.

What I found out after couple of google queries, is the official answer on this subject, which states that you should double check three things:

  • First check to make sure you are the author on the post where the comments are getting added.
  • Check the Settings → Discussion page to make sure email notifications are turned on.
  • Check the spam/junk folder in your email program to make sure the messages weren’t filed there by mistake.

I double checked those and all was fine. In that case WordPress official site suggest that you should contact with them about this issue. I didn’t, but feel free to do that. What I did was, to investigate a bit more. I finally found Chad Butler blog who wrote few articles about wp_mail method – which WordPress engine uses to send emails. I started with the one that talks about testing your wp_mail method, and used his code to check if my wp_mail method works:

<?php

/**

* Update variable settings.

* Load to your WP root folder.

*/

// Set $to as the email you want to send the test to

$to = "your email address in quotes";

// No need to make changes below this line

// Email subject and body text

$subject = 'wp_mail function test';

$message = 'This is a test of the wp_mail function: wp_mail is working';

$headers = '';

// Load WP components, no themes

define('WP_USE_THEMES', false);

require('wp-load.php');

// Call the wp_mail function, display message based on the result.

if( wp_mail( $to, $subject, $message, $headers ) ) {

// the message was sent...

echo 'The test message was sent. Check your email inbox.';

} else {

// the message was not sent...

echo 'The message was not sent!';

};

?>

It did not.

Then I went deeper into his articles and found the one that he writes about troubleshooting with the wp_mail function. He explains in it, why wp_mail method can fail to send an email. Then I read another of his blog posts, which talks about how to turn your wp_mail function to use other email address without using a plugin which in the end took me to the last, but not least, of his blog posts about turning your wp_mail method to use SMTP server without using a plugin. I followed his guildlines and used his code:

add_action('phpmailer_init','send_smtp_email');

function send_smtp_email( $phpmailer )

{

// Define that we are sending with SMTP

$phpmailer->isSMTP();

// The hostname of the mail server

$phpmailer->Host = "smtp.example.com";

// Use SMTP authentication (true|false)

$phpmailer->SMTPAuth = true;

// SMTP port number - likely to be 25, 465 or 587

$phpmailer->Port = "587";

// Username to use for SMTP authentication

$phpmailer->Username = "yourusername";

// Password to use for SMTP authentication

$phpmailer->Password = "yourpassword";

// Encryption system to use - ssl or tls

$phpmailer->SMTPSecure = "tls";

$phpmailer->From = "your-email-address";

$phpmailer->FromName = "Your Name";

}

I specified my own – taken from web hosting that I’m using – SMTP configuration values, then I created an email address, in my web hosting administration panel, which could be then used as $phpmailer->Uername and $phpmailer->Password and then I pasted the code at the end of my function.php file, but before closing tag ?>. Afterwards I tested it by adding a new comment to one of my posts and waited for the notification.

It came after few seconds! It finally works!

Thanks Chad!