Home » Php » email – PHP mail() function returns true, but doesn't send mail

email – PHP mail() function returns true, but doesn't send mail

Posted by: admin July 12, 2020 Leave a comment


I know that this question was asked before.

When I call the PHP mail() function, it returns true.

I checked my php.ini (I’v running CentOS):

SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On

I read in a forum that I have to install sendmail. So I installed it. Now sites with a mail() function doesn’t load anymore. So I removed sendmail, and the mail() function returns true again, but doesn’t send the mail.

Any idea?

How to&Answers:

To send an email you need a SMTP server (local or remote). Actually your mail function just passes the mail to your SMTP server and is this one which really send your email.

In your php.ini appears this line

sendmail_path = /usr/sbin/sendmail -t -i

You should be aware if you use that configuration parameter (from manual):

If set, smtp, smtp_port and sendmail_from are ignored and the
specified command is executed.

But the most important thing here is you just uninstall sendmail so you can expect your mail goes nowhere. I know sendmail was giving you some problems, possibly configuration problems, but now your php.ini configuration is wrong.

How to solve it?

  • Start removing the sendmail_path parameter from the php.ini.

  • Install a simple to configure SMTP server like postfix.

  • Verify postfix is listening at port 22:

netstat -lnt

  • Try to send a mail from your php mail() function

  • Verify your mail has been sent correctly (check your /var/log/mail.log or /var/log/mail/mail.log files)

  • You also can verify the mail is not in the postfix queue:

postqueue -f


i had a similar problem to this; both mail() and wp_mail() functions were returning TRUE, but no email was being sent to my [email protected] Email account.

It turns out that Yahoo was blocking these emails as spam. I did not have captcha implemented on my form, and therefore many spam emails were being sent to yahoo which is why they blocked the emails. Emails were sent successfully, but Yahoo was marking them as spam.

make sure this is not the problem in your case.


Please check your DNS,
I get the same problem when test on localhost, but working on real host.
The problem is fake DNS, your email was blocked by google.
If you test with other email service (example: yahoo mail), you will receive as a spam mail.
I found it after check mail log


I had issues setting smtp, so i ended up using gmail. You can send mail with PEAR MAIL as instructed here:

$from = "NoReply <[email protected]>";
$to = "someone <[email protected]>";
$subject = "my subject";
$body = "my body";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "[email protected]";
$password = "gmailpass";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);

If you use gmail, your $from will be replaced by the email address used to send it.