Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I recently implemented a comment area on my website and tried to get the email notification functionality to work. It doesn't seem to want to send email notifications when new comments are made. Just to see if PHP can send emails, i tried to reset the password (because you'll get a new password via mail), and i then got the message:

The e-mail could not be sent. Possible reason: your host may have disabled the mail() function

I've checked the checkboxes in Settings -> Discussion, and the email is valid, so it's not a setting issue. I tried to create a php file and send using mail(), and it sent successfully. So there must be something weird going on with Wordpress.

Any ideas?

share|improve this question
    
Please provide debugging information –  s_ha_dum Apr 8 '13 at 15:12

5 Answers 5

Step by step: First find the file where the error message appear. I use Notepad++ and the CTRL + F command to search in files. It is a good idea to search only the first few words of the error message, because some error messages are combined of different messages.

Your error message appear in wp-login.php and holy luck, only there. So let's have a look why this error could occur.

if ( $message && !wp_mail($user_email, $title, $message) )

There are two conditions. $messagehave to be true (not an empty string, not false, not null, etc). And wp_mail() shouldn't return false.

One line above, there is a filter $message = apply_filters('retrieve_password_message', $message, $key);, so it is possible that a plugin (or theme) use this filter and returns a value that is not true (empty string, false, null, etc.).

But it is much easier to check if wp_mail() is working or not. Write a small plugin to send a test mail to yourself:

<?php
/**
 * Plugin Name: Stackexchange Testplugin
 * Plugin URI:  http://yoda.neun12.de
 * Description: Send me a test email
 * Version:     0.1
 * Author:      Ralf Albert
 * Author URI:  http://yoda.neun12.de
 * Text Domain:
 * Domain Path:
 * Network:
 * License:     GPLv3
 */

namespace WordPressStackexchange;

add_action( 'init', __NAMESPACE__ . '\plugin_init' );

function plugin_init(){
    $to      = 'your-email-adress@some-domain.tld';
    $subject = 'Testemail';
    $message = 'FooBarBaz Testmail is working';

    wp_mail( $to, $subject, $message );
}

(This is PHP5.3 code. If you are running PHP5.2, remove the namespace things)

The plugin should send a testmail immediately after activation. If not, calling some backend pages (e.g. dashboard) should do it.

If the testmail does not arrive, then you probably have an issue with wp_mail(). So turn on debugging:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
@ini_set( 'display_errors',1 );

Put this code into your wp-config.php and retry sending yourself a testmail. Now you should get some error messages and they also should be logged into wp-content/debug.log (The debug log can grow very large if there are more errors caused by plugins and/or themes).

At this point, you got good informations if wp_mail() fails and if so, why. If wp_mail() work correctly and the testmail arrived, go back to top and find out why $message is not true.

If you have issues with wp_mail(), so keep in mind that wp_mail() does not use PHPs mail() function. WordPress use a PHP class (PHPMailer). Maybe you just need a plugin to use SMTP instead of sendmail. Or the problem is located at another place. We don't know. You have to investigate.

share|improve this answer
    
Yeah i tried digging into the core and it also lead me to PHPMailer, and it actually does use php's mail(). At least in some cases (see line 732 in wp-includes/class-phpmailer.php. I don't have access to the ftp atm but i will try your suggestions as soon as i can. Surely this must lead me somewhere. Thanks a lot! –  qwerty Apr 8 '13 at 17:52
    
I tested wp_mail() and it seems to work fine, i received the mail as expected. WP still wouldn't send the comment/password-reset emails though, and i didn't get anything in the log file (it wasn't created), so i tried installing an SMTP mail plugin and set up a new email account for Wordpress. It works now but i still don't understand why it couldn't send before. Thanks! –  qwerty Apr 9 '13 at 7:05

That's a typical issue with some web hosts who disable or restrict php mail. Try a plugin to use your hosts SMTP server instead. See http://wordpress.org/extend/plugins/search.php?q=mail+smtp

share|improve this answer
    
PHP's mail() works fine though, i tried it. I also mentioned it in the question. –  qwerty Apr 8 '13 at 17:44
    
I also said "restrict" php mail. Try an SMTP plugin. –  songdogtech Apr 8 '13 at 18:02
    
My bad. SMTP works but i still don't understand why it failed before, as both php's mail() and wordpress' wp_mail() both worked fine. –  qwerty Apr 9 '13 at 7:09

I ran into this today; in my case the situation happened because the server's hosts file has the same domain name of the email address, pointing to localhost. The mx record points to a different server, but the hosts file is overriding DNS and WP is trying to deliver the email locally. Removing the domain from the hosts file and restarting sendmail resolved this issue.

share|improve this answer

I don't know whether this is still relevant to you or not, but since there is no answer chosen, I thought let me give it a try once.

Actually, I had faced the exact same problem since my openshift host all of a suddenly gave way today and stopped sending mails. Digging through the code and codex, I came to know about the wp_mail() function and finally google led me here and I saw how it could be overridden.

Building on @Ralf912's answer, I modified the script a bit so that the code uses sendgrid.com's web api to send mails instead of wordpress default one (that I presume :

<?php

function sendgridmail($to, $subject, $message, $headers)
{
    $url = 'https://api.sendgrid.com/';
    //$user = 'yourUsername';
    //$pass = 'yourPassword';

    $params = array(
        'api_user'  => $user,
        'api_key'   => $pass,
        'to'        => $to,
        'subject'   => $subject,
        'html'      => '',
        'text'      => $message,
        'from'      => 'abc@hotmail.com',
      );


    $request =  $url.'api/mail.send.json';

    // Generate curl request
    $session = curl_init($request);
    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);
    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    // obtain response
    $response = curl_exec($session);
    curl_close($session);

    // print everything out
    //print_r($response);
}

//only for testing:
/*$to      = 'abc@yahoo.com';
$subject = 'Testemail';
$message = 'It works!!';
echo 'To is: ' + $to;
#wp_mail( $to, $subject, $message, array() );
sendgridmail($to, $subject, $message, $headers);
print_r('Just sent!');*/

if (!function_exists('wp_mail')) {
    function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
    {
        // use the PHP GnuPG library here to send mail.
        sendgridmail($to, $subject, $message, $headers);
    }
}

function plugin_init()
{
   /* $to      = 'xyz@yahoo.com';
    $subject = 'Testemail';
    $message = 'It works Live!';
    //echo 'To is: ' + $to;
    wp_mail( $to, $subject, $message, array() );
    //print_r('Just sent!');*/
}

And it worked!

share|improve this answer

This is a super annoying error message as it could be many things, and it doesn't reveal the actual error (which is often silenced in other parts of the code).

This error appears when the wp_mail() function returns false, which in turn could happen if phpmailer->Send() returns false or raises an exception.


How to display warnings from PHP's mail() function

These are normally silenced by default, but unfortunately WordPress never captures them. To show them, simply remove the @ signs from @mail(... in wp-includes/class-phpmailer.php in the mailPassthru() function:

if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
    $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header);
} else {
    $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header, $params);
}


How to hunt down other possible causes:

  1. Add a single line to the bottom of wp_mail() in /wp-includes/pluggable.php:

    // Send!
    try {
        return $phpmailer->Send();
    } catch ( phpmailerException $e ) {
        //------------- This next line is the one to add -------------------
        if (WP_DEBUG) echo '<pre>' . esc_html(print_r($e, TRUE)) . '</pre>';
        return false;
    }
    
  2. It will dump the full details of where the exception was raised. Unfortunately it sometimes includes this unhelpful exception message: "Could not instantiate mail function". Yeah thanks WordPress, that's real helpful.

  3. By looking at the exception you can find the line number of the error, and can hopefully trace it back through the code to find the real cause.

Good luck. Hopefully WordPress improves email error handling at some point in the future.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.