我有一个通过PHP发送HTML格式的电子邮件的联系表单。完成此表单后,电子邮件将发送到我的电子邮件地址,并向填写表单的客户发送略有修改的版本。
这样可以正常工作,但是我按预期收到了电子邮件,但客户只接收原始HTML代码,而不是格式正确的电子邮件。
我已将我的代码包含在下方,但出于隐私原因替换了我的电子邮件地址。
PHP代码:
<?php
// Get the form fields, removes html tags and whitespace.
$first_name = strip_tags(trim($_POST["first_name"]));
$first_name = str_replace(array("\r","\n"),array(" "," "),$first_name);
$last_name = strip_tags(trim($_POST["last_name"]));
$last_name = str_replace(array("\r","\n"),array(" "," "),$last_name);
$company = strip_tags(trim($_POST["company"]));
$company = str_replace(array("\r","\n"),array(" "," "),$company);
$phone = strip_tags(trim($_POST["phone"]));
$phone = str_replace(array("\r","\n"),array(" "," "),$phone);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$checklist = trim($_POST["checklist"]);
$details = trim($_POST["details"]);
// Check the data.
if (empty($first_name) OR empty($last_name) OR empty($details) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: http://www.EXAMPLE.com/index-test.html");
exit;
}
// Set the recipient email address. Update this to YOUR desired email address.
$to = "contact@EXAMPLE.com";
$noreply = "noreply@EXAMPLE.com";
// Set the email subject.
$subject = "New contact from $first_name $last_name of $company";
$subject2 = "Thank you for contacting EXAMPLE!";
$message = "
<html>
<head>
</head>
<body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<tr>
<td><b>Name:</b></td>
<td>$first_name $last_name</td>
</tr>
<tr>
<td><b>Company Name: </b></td>
<td>$company</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$phone</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$email</td>
</tr>
<tr>
<td><b>Services:</b></td>
<td>$checklist</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><b>Details of Job:</b><br>$details</td>
</tr>
</table>
</body>
</html>
";
$message2 = "
<html>
<head>
</head>
<body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<p>Hi $first_name,<br><br>Thank you for contacting me. Once I have reviewed your job request, I will be in contact to discuss the project further.<br><br>You will find a copy of your message below. Please do not reply to this email as your reply will be sent to an unmonitored email address.</p>
<table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<tr>
<td><b>Name:</b></td>
<td>$first_name $last_name</td>
</tr>
<tr>
<td><b>Company Name: </b></td>
<td>$company</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$phone</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$email</td>
</tr>
<tr>
<td><b>Services:</b></td>
<td>$checklist</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><b>Details of Job:</b><br>$details</td>
</tr>
</table>
<br>
<br>
<span style=\"color: #3a3a3c;\"><b>EXAMPLE NAME</b></span>
<br>
<span style=\"font-size: 13px; color: #F04E4C;\">Freelance Cinematography & Post-Production</span>
<br>
<br>
<span style=\"font-size: 13px; color: #F04E4C;\">P</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>000 000 0000</b> </span>
<span style=\"font-size: 13px; color: #F04E4C;\">E</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>contact@EXAMPLE.com</b> </span>
<span style=\"font-size: 13px; color: #F04E4C;\">W</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>www.EXAMPLE.com</b></span>
<br>
<br>
<img alt=\"EXAMPLE Logo\" src=\"http://EXAMPLE.com/resources/img/logos/logo-black-orange-cropped.png\">
<br>
<div> </div>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers2 .= "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= "From: $first_name $last_name <$email>";
$headers2 = "From: EXAMPLE <$noreply>";
mail($to,$subject,$message,$headers);
mail($email,$subject2,$message2,$headers2);
?>
我是PHP的新手,所以我知道代码中可能会出现一个简单的错误,或者只是添加了一些没有意义的错误。如果有人能指出我哪里出错了,我真的很感激。
另外,我知道服务清单部分不起作用,我还没有找到那个部分。
答案 0 :(得分:0)
我认为问题在于您定义$headers2
的顺序 - 它是不正确的 - 因为您最初设置$headers2.=
表示要追加,然后通过在没有连接的情况下再次显式设置来覆盖它。
<?php
// Get the form fields, removes html tags and whitespace.
$first_name = strip_tags(trim($_POST["first_name"]));
$first_name = str_replace(array("\r","\n"),array(" "," "),$first_name);
$last_name = strip_tags(trim($_POST["last_name"]));
$last_name = str_replace(array("\r","\n"),array(" "," "),$last_name);
$company = strip_tags(trim($_POST["company"]));
$company = str_replace(array("\r","\n"),array(" "," "),$company);
$phone = strip_tags(trim($_POST["phone"]));
$phone = str_replace(array("\r","\n"),array(" "," "),$phone);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$checklist = trim($_POST["checklist"]);
$details = trim($_POST["details"]);
// Check the data.
if (empty($first_name) OR empty($last_name) OR empty($details) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: http://www.EXAMPLE.com/index-test.html");
exit;
}
// Set the recipient email address. Update this to YOUR desired email address.
$to = "contact@EXAMPLE.com";
$noreply = "noreply@EXAMPLE.com";
// Set the email subject.
$subject = "New contact from $first_name $last_name of $company";
$subject2 = "Thank you for contacting EXAMPLE!";
$message = "
<html>
<head>
</head>
<body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<tr>
<td><b>Name:</b></td>
<td>$first_name $last_name</td>
</tr>
<tr>
<td><b>Company Name: </b></td>
<td>$company</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$phone</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$email</td>
</tr>
<tr>
<td><b>Services:</b></td>
<td>$checklist</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><b>Details of Job:</b><br>$details</td>
</tr>
</table>
</body>
</html>
";
$message2 = "
<html>
<head>
</head>
<body style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<p>Hi $first_name,<br><br>Thank you for contacting me. Once I have reviewed your job request, I will be in contact to discuss the project further.<br><br>You will find a copy of your message below. Please do not reply to this email as your reply will be sent to an unmonitored email address.</p>
<table style=\"font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px;\">
<tr>
<td><b>Name:</b></td>
<td>$first_name $last_name</td>
</tr>
<tr>
<td><b>Company Name: </b></td>
<td>$company</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$phone</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$email</td>
</tr>
<tr>
<td><b>Services:</b></td>
<td>$checklist</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><b>Details of Job:</b><br>$details</td>
</tr>
</table>
<br>
<br>
<span style=\"color: #3a3a3c;\"><b>EXAMPLE NAME</b></span>
<br>
<span style=\"font-size: 13px; color: #F04E4C;\">Freelance Cinematography & Post-Production</span>
<br>
<br>
<span style=\"font-size: 13px; color: #F04E4C;\">P</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>000 000 0000</b> </span>
<span style=\"font-size: 13px; color: #F04E4C;\">E</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>contact@EXAMPLE.com</b> </span>
<span style=\"font-size: 13px; color: #F04E4C;\">W</span> <span style=\"font-size: 13px; color: #3a3a3c;\"><b>www.EXAMPLE.com</b></span>
<br>
<br>
<img alt=\"EXAMPLE Logo\" src=\"http://EXAMPLE.com/resources/img/logos/logo-black-orange-cropped.png\">
<br>
<div> </div>
</body>
</html>
";
// Always set content-type when sending HTML email
/*
Set the initial value
and then append additional headers to the string
*/
$headers = "From: $first_name $last_name <$email>\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers2 = "From: EXAMPLE <$noreply>\r\n";
$headers2 .= "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$subject,$message,$headers);
mail($email,$subject2,$message2,$headers2);
?>
另一种方法是在需要时使用implode
形成实际标题字符串的数组。
$headers_1=array();
$headers_2=array();
$headers_1[]="From: $first_name $last_name <$email>";
$headers_1[]="MIME-Version: 1.0";
$headers_1[]="Content-Type: text/html;charset=UTF-8";
$headers_2[]="From: EXAMPLE <$noreply>";
$headers_2[]="MIME-Version: 1.0";
$headers_2[]="Content-Type: text/html;charset=UTF-8";
mail( $to, $subject, $message, implode( "\r\n",$headers_1 ) );
mail($email,$subject2,$message2,implode( "\r\n",$headers_2 ));