使用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');
?>
答案 0 :(得分:2)
像这样的东西。
if(@mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
来自docs
如果邮件成功接受传递,则返回TRUE,FALSE 否则。
重要的是要注意,因为邮件被接受了 交付,并不意味着邮件实际上会达到预期的目的 目的地。
答案 1 :(得分:1)
mail()
返回布尔值,具体取决于邮件是否已成功接受传递。添加if
条件以检查mail()
是否返回TRUE
或FALSE
,如下所示。
if(mail($to, $subject, $message, $headers, '-freturn@example.com'))
echo "Success"; // on TRUE
else
echo "fail"; // on FALSE
答案 2 :(得分:0)
if(@mail($to, $subject, $message, $headers))
{
echo "Sent";
}else{
echo "Failed";
}
这应该可以完成你想要的工作。
答案 3 :(得分:0)
请尝试这样
<?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";
}
?>