有人建议我在php代码中添加标题,因为我的电子邮件以空白/空的表单字段进入收件箱,即使用户在点击提交之前已经在网页上提交了这些字段。如果有人能告诉我我做得对吗?我将不胜感激?
<?php
$EmailFrom = "Quote@mydomian.co.uk";
$EmailTo = "me@mydomian.co.uk";
$Subject = "Online contact form";
$fullName = Trim(stripslashes($_POST['fullName']));
$contactNo = Trim(stripslashes($_POST['contactNo']));
$message = Trim(stripslashes($_POST['message']));
// prepare email body text
$Body = "";
$Body .= "fullName: ";
$Body .= $fullName;
$Body .= "'n";
$Body .= "contactNo: ";
$Body .= $contactNo;
$Body .= "'n";
$Body .= "message: ";
$Body .= $message;
$Body .= "'n";
$headers = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$success = mail($EmailTo, $Subject, $Body, $headers, $EmailFrom");
// redirect to success page
if ($success){
print "<meta http-equiv='"refresh'" content='"0;URL=Thankyou.html'">";
}
else{
print "<meta http-equiv='"refresh'" content='"0;URL=error.html'">";
}
?>
您应该使用php-header()函数:
标题('Content-type:application/html/html');
因为这将首先发送到浏览器。。。
以下是如何在电子邮件中设置发件人/回复邮件头的示例:
$headers = array(
'From: sender name <senderemail@example.com>',
'Sender: senderemail@example.com',
'To: '.$to,
'Reply-To: senderemail@example.com',
'MIME-Version: 1.0',
'Content-Type: text/html; charset=utf-8',
'Content-Transfer-Encoding: 8bit',
);
if ($cc)
$headers[] = 'CC: ' . $cc;
if ($bcc)
$headers[] = 'BCC: ' . $bcc;
$response = @mail($to, $subject, $content, implode("'n", $headers), '-f senderemail@example.com');
注意:正如您所看到的,发件人的电子邮件地址有很多重复,但我注意到邮件客户端和邮件发件人(如sendmail)的行为不同,因此需要这样来处理可能的配置