How can I test if mail()
has successfully delivered mail?
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.
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.
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.
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.
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.';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With