是的,这是正确的做法,这是故障安全做法的一部分。您应该检查 mail() 是否返回 true 以确定邮件是否已成功发送。
邮件如果从服务器转发,即如果从服务器发送的邮件将返回true,此外我同意Akam 的评论。邮件不必成功发送。(这确认邮件已发送。)
我给出了一个更长的片段,检查提交时只会发送此邮件,您可以忽略它..
<?php
if(isset($_POST['submit'])) {
$to = "testreceiver@mail.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent!!!!";
// redirect to thank you page here
}
else {
echo "Message failed";
}
} else {
echo "Malfunction alert!";
}
?>