PHP 邮件功能不工作

PHP Mail Function Isn't Working

我目前正在为我的一门网页设计课程开发一个网站作品集网站,希望它在完成后能成为我的实际作品集。部分作业需要站点的 "contact me" 部分,其中将包含一个表单,该表单可验证输入数据并在正确提交时重定向用户。我已经完成了这个,但我希望它在有人提交表格时给我发电子邮件。这是我的代码:

$admin_mail = example@mail.com
-code to validate form-
$headers = array("First Name: " => $_POST['first_name'],
                        "Last Name: " => $_POST['last_name'],
                        "Email: " => $_POST['email'],
                        "Company: " => $_POST['company']);
$message = wordwrap($_POST['message'],70);
mail($admin_mail,"Portfolio Message",$message,$headers);
header("location: success.html");

我用 print_r 函数检查了 $headers$message 变量,它们工作正常。所有输入的数据都被添加并且页面重定向正确,但电子邮件似乎根本没有发送。我检查过我的虚拟主机可以发送和接收电子邮件,而且似乎也能正常工作。

关于如何解决这个问题有什么想法吗?

只需检查..通过输入真实的电子邮件 ID

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>

$headers 错误。检查 php mail function docs

(假设您没有 step-debug 可用)

看起来您的位置 header 将被设置,无论邮件是否发送。 mail() returns 布尔值,所以使用类似:

if(mail($admin_mail,"Portfolio Message",$message,$headers)){
  header(...);
}
else{
  // it failed. run some debug here.
}

A jump-start:这是来自我网站上类似联系表单的 copy-paste 代码。它就像一个魅力 - 每天给我发邮件。 ...好吧,每天都有人使用它 :)

$to = 'my.mail@gmail.com';
$subject = 'The Site Contact Request';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$mail = mail($to, $subject, $m, $headers);
return $mail;

headers可以添加如下

            $headers =  'From: '.$from.''. "\r\n" .
                'Reply-To: '.$from.'' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
                $message = htmlspecialchars($body);
                mail($to, $subject, $message, $headers);