虽然代码显然是正确的,但php电子邮件表单仍无法正常

时间:2017-02-28 13:08:35

标签: php email

我对sendemail.php文件进行了建议的更改,因此整个文件现在包含...

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'mail@URLREMOVED.co.uk';

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";

$sent = mail($to, $subject, $message, $headers);
die;

在尝试其他测试消息后,我的电子邮件收件箱中没有收到任何内容。

1 个答案:

答案 0 :(得分:1)

尝试替换:

$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers = "From: {$name} <{$from}>\r\n";
$headers = "Reply-To: <{$from}>\r\n";
$headers = "Subject: {$subject}\r\n";
$headers = "X-Mailer: PHP/".phpversion()."\r\n";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";

失败是因为您没有正确设置$ headers,而是在您分配了值之后在每一行上覆盖了标题,下一行覆盖了之前的值。