12

I recently implemented a comment area on my website and tried to get the email notification 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 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?

1

10 Answers 10

9

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      = '[email protected]';
    $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.

5
  • 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
    Commented Apr 8, 2013 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
    Commented Apr 9, 2013 at 7:05
  • I'm not getting any error and even not mail
    – baldraider
    Commented Aug 19, 2017 at 7:29
  • Where to save your plugin ?
    – Black
    Commented Nov 4, 2021 at 9:19
  • As you can see, the answer is about 8 years old. I have not worked with WordPress more than 5 years. As incan remember, in the wp-plugins dieectory, ascusual. But maybe things was changed in the last 5 years.
    – Ralf912
    Commented Nov 4, 2021 at 13:30
2

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.

2

I has same issue with Ubuntu server on Amazon EC2.I get issue while using reset password link and also other notification email were not working.

So here is solutions which worked for me.Word-press used wp_mail() function to send email which need PHPMailer class which used php mailer stored in /usr/sbin/sendmail.

Use this simple php function first to check php mail

<?php
$to = "[email protected]";
$subject = "Test Email Function";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";

mail($to,$subject,$txt,$headers);
?>

If this is not working then you need to install php mailer. Use this command to install php mail on Ubuntu server.

sudo apt-get install sendmail

Then check word-press email functions.

1
  • this answer is the one anyone should try before any other answers, this is the way to go
    – hatenine
    Commented Jan 25, 2019 at 12:53
1

If the other great answers here don't help, try this:

I encountered this same problem and nothing I could find in any of the suggestions for WordPress solved it for me.

Then I started investigating if it was the PHP installation itself that had disabled the mail function, but none of that worked either. Everything looked like it was configured properly.

All of these problems started for me once I upgraded my server to CentOS 7 which uses SELinux (Security Enhanced Linux) and what I've learned in the last couple of weeks with SELinux is that if something isn't working, but everything looks like it should be working... that means SELinux is silently and secretly blocking you in the background.

And viola.

If you are running and OS that uses SELinux, just execute the following command as root:

setsebool -P httpd_can_sendmail=1

There is a security setting that inherently prevents the webserver from sending email. When you flip that switch and tell SELinux it's ok for the webserver to send email, everything suddenly works.

0

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.

0

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'      => '[email protected]',
      );


    $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      = '[email protected]';
$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      = '[email protected]';
    $subject = 'Testemail';
    $message = 'It works Live!';
    //echo 'To is: ' + $to;
    wp_mail( $to, $subject, $message, array() );
    //print_r('Just sent!');*/
}

And it worked!

0

I had the same error, both functions(mail and wp_mail) worked, but I still had this annoying error. The fix was very easy, but it took me few hours to find the reason. So I will share here my solution on the problem which might be (or might not) the same with yours.

I tried mail() function and it worked, but when you test it you don't specify the last parameter called 'parameters' in mail() function. And WP does use it.

@mail("[email protected]",$title,$body,$headers,"[email protected]");

So, basically, this parameter ("[email protected]") with flag "-f" makes mail() function check if the email address "[email protected]" listed in the "trusted emails" list.

So if it doesn't, it returns false, which makes wp_mail() returns false and leads to the error message.

So, solution is to ask hoster to do this for you, or if you are using cPanel, just add email account for this address and it will automatically will add it into the "trusted list".

0

it called -Manage Registered Email-Ids For Sending Mails via Scripts ie.(Wordpress)

  1. Login your Cpanel.
  2. Go to Email Section > then Click Registered Email IDs.
  3. then add ([email protected]) or where your wordpress hosted. ie ([email protected]) . then submit, it takes few minute to activate wait 15minute to 1 hour depending to your hosting provider, then it will work.
0

I had this error for ages and tried so many solutions that didn't work. I have a custom Wordpress install on AWS EC2. First off ensure your AWS SES mail is enabled through support, they must be in the same (or close) region in SES and EC2. I used Google suite(gsuite) for email for receiving/sending mail.

Make sure the test email sends in AWS SES and Gsuite.

Install the Wordpress plugin WP Mail SMTP, use the option "Other SMTP", grab your SMTP credentials from AWS SES, this is where I got stuck.

You must enable the tick box "SSL" for Encryption, this changes the port to 465 for me. At last my email test sent from Worpdress successfully.

0

I fixed this issue by changing the "From Email" on my local test environment, by changing it from wordpress@localhost to [email protected], for example using a WordPress plugin like "WP Mail SMTP" and selecting the default PHP mailer instead of using SMTP.

The issue happened even with all other plugins disabled and I did not have this issue in the past. The issue is also not present when using the PHP mail function directly, so maybe it happened because of a recent WordPress change.

I sent and received emails on my local computer using Postfix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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