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

    |
  • Added:
  • |
  • In: Basic PHP

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?

This Question Has 4 Answeres | Orginal Question | Florian

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

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 issues setting smtp, so i ended up using gmail. You can send mail with PEAR MAIL as instructed here:

$from = "NoReply <noreply@random.com>";
$to = "someone <someone@somemail.com>";
$subject = "my subject";
$body = "my body";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "gmailaccount@gmail.com";
$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.

i had a similar problem to this; both mail() and wp_mail() functions were returning TRUE, but no email was being sent to my xyz@yahoo.com 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.


Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.
Connect Social With PHPAns
Top