Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 394
Florian Avatar asked May 25 '14 16:05

Florian


People also ask

Why is my PHP email not sending?

If you've created a PHP mail form and find it's not sending email, then it's most often due to the FROM address the form is using in its headers. A simple way to confirm if this is the case: Log in to your web server via FTP or SSH.

What is return value mail () in PHP?

Return Value: Returns the hash value of the address parameter, or FALSE on failure. Note: Keep in mind that even if the email was accepted for delivery, it does NOT mean the email is actually sent and received!

How do you check if mail is send or not in PHP?

Well mail() simply returns a boolean value depending on whether the mail was successfully accepted for delivery. From the php.net site: Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

How do I know if my PHP email is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>


2 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

like image 165
Victor Henriquez Avatar answered Oct 19 '22 23:10

Victor Henriquez


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.

like image 34
Irfan Kissa Avatar answered Oct 19 '22 23:10

Irfan Kissa