您的“已发布”代码中缺少一些内容。
末尾缺少分号$message = "hello"
(除非这是拼写错误/粘贴错误?)和第一个点$headers
此外,没有From:
标头属性肯定会导致电子邮件被发送到并被视为垃圾邮件。
修复了这些问题并添加了额外的标头信息后,以下代码可以正常工作并且没有出现在我的垃圾邮件中,但收件箱成功了。
<?php
$to = "xxxx@xxxx.com";
$email = "email@example.com";
$subject = 'xxxx';
$message = "hello";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
?>
或成功回显消息:
<?php
$to = "xxxx@xxxx.com";
$email = "email@example.com";
$subject = 'xxxx';
$message = "hello";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(mail($to, $subject, $message, $headers))
{
echo "Message sent.";
}
else{
echo "Something went wrong.";
}
?>
访问 PHP.net 网站以获取有关mail()
和header()
函数的更多信息。