Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if PHP mail() has successfully delivered mail

Tags:

php

How can I test if mail() has successfully delivered mail?

like image 331
Sunny Avatar asked May 14 '12 10:05

Sunny


People also ask

Why is my PHP mail function not working?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

What is the mail () function in PHP?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

Why does PHP mail take so long?

It is the SMTP mail delivery (which PHP hands off the message to) which is taking time. Possibly, the delay you see is greylisting on the receiving server, meaning that the receiving mail server refuses to accept the message until the sending server (which your PHP script handed it to) tries a few times.


2 Answers

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.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

So you can test to see if it's been "sent", however checking if it's been delivered is another story.

like image 166
Ben Everard Avatar answered Sep 22 '22 02:09

Ben Everard


As per Ben reply you can check successfully email delivery as below

$result = mail('[email protected]', 'Test Subject', $message);
if(!$result) {   
     echo "Error";   
} else {
    echo "Success";
}

For better result you can use PHPMailer. Click on below link for detailed documentation of PHPMailer.

http://phpmailer.worxware.com/index.php?pg=tutorial

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
like image 37
Sanjeev Chauhan Avatar answered Sep 24 '22 02:09

Sanjeev Chauhan