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

5

13

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?

Preconscious answered 25/5, 2014 at 16:32 Comment(4)
How do you know that it doesn't send the email?Eskilstuna
Take a look here: #14457173 - it's usually when a host filters mail being sent via the mail() function instead of using SMTP.Layfield
CHECK YOUR SPAM FOLDER !!! https://mcmap.net/q/878911/-php-mail-function-returns-true-but-doesn-39-t-send-mailConnivent
Adding to the comment by @Layfield - in one instance, the host blocked sending mails when the from-address being used in the function did not exist on the server.Benuecongo
W
6

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

Weld answered 25/5, 2014 at 17:11 Comment(0)
A
3

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.

Arthurarthurian answered 15/5, 2015 at 21:15 Comment(3)
@Irfan Kissa @OnlyMAJ I'm having the exact same problem on shared hosting. mail() returns true, but nothing. nothing in my junk folder. what the hell is going onVelites
@Velites You can send your emails to other providers than yahoo and google if it doesn't work on that too, then there is a problem with your hosting otherwise the problem comes from yahoo or google.Platyhelminth
@Platyhelminth last night i tried sending some to the email at my own domain, which worked. my original email is a hotmail address, which hasn't been working. i tried sending to a gmail account yesterday and nothing. i'm going to do some more testing today. supposedly my host supports it... what are some alternatives?Velites
A
1

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

Airy answered 11/8, 2015 at 3:24 Comment(2)
How do I check?Velites
where is mail log dude?Incoming
H
1

I had a similar issue. Sendmail was in use. Note these things:

  • Real sender is where sendmail installed (VPS often).
  • From field in php mail script (supposed to match sender)
  • MX records for the domain name (tells what sender to trust)
  • Sendmail logs could tell a lot (/var/mail in my case). They indeed explain why true is returned despite message is not delivered.

In my case from value in php script was incorrect as well as MX records that didn't trust sender.

I want to mention another possible error is hostname from this guide that could be changed like

hostnamectl set-hostname domain.name
Hachmin answered 10/11, 2022 at 21:46 Comment(0)
L
-1

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.

Luciferase answered 25/5, 2014 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.