使用 mail(( PHP 时如何检查邮件发送成功与否?
我用这个代码向用户发送邮件,但是我想知道如何在这个代码上检查发送邮件状态是否成功?
<?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');
?>
像这样的东西。
if(@mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
从文档中
如果邮件已成功接受传递,则返回 TRUE,FALSE 否则。
重要的是要注意,仅仅因为邮件被接受 交付,这并不意味着邮件将实际到达预期 目的地。
mail()
根据邮件是否成功接受传递返回布尔值。添加if
条件以检查mail()
返回TRUE
还是FALSE
如下所示。
if(mail($to, $subject, $message, $headers, '-freturn@example.com'))
echo "Success"; // on TRUE
else
echo "fail"; // on FALSE
if(@mail($to, $subject, $message, $headers))
{
echo "Sent";
}else{
echo "Failed";
}
这应该可以完成您想要的工作。
请像这样尝试
<?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";
}
?>