mongmongmong seesee February 2016

How to check mail send success or not when using mail() PHP?

How to check mail send success or not when using mail() PHP ?

I use this code for send mail to user , But i want to know how can i check send mail status success or not on this code ?

<?PHP
include("connect.php");
$to      = "test@example.com";
$subject = "Subject";
$message = "Message";
$headers  = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: EXMAPLE <noreply@example.com>' . "\r\n";
$headers .= 'Return-Path: return@example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn@example.com');
?>

Answers


Jenz February 2016

mail() returns boolean value depending on whether the mail was successfully accepted for delivery. Add an if condition to check whether mail() returns TRUE or FALSE like shown below.

if(mail($to, $subject, $message, $headers, '-freturn@example.com'))
  echo "Success";   // on TRUE
else
  echo "fail";     // on FALSE


SaucedApples February 2016

if(@mail($to, $subject, $message, $headers))
{
  echo "Sent";
}else{
  echo "Failed";
}

This should do the job you desire.


Drudge February 2016

Something like this.

if(@mail($emailRecipient, $subject, $message, $headers))
    {
      echo "Mail Sent Successfully";
    }else{
      echo "Mail Not Sent";
    }

From the docs

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.


Jalpa February 2016

Please try like this

<?PHP
include("connect.php");
$to      = "test@example.com";
$subject = "Subject";
$message = "Message";
$headers  = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: EXMAPLE <noreply@example.com>' . "\r\n";
$headers .= 'Return-Path: return@example.com' . "\r\n";
if(mail($to, $subject, $message, $headers, '-freturn@example.com')) {
   echo "Your mail sent successfully";
} else {
   echo "Your mail was not sent something wrong";
}
?>

Post Status

Asked in February 2016
Viewed 2,132 times
Voted 6
Answered 4 times

Search




Leave an answer