【发布时间】:2021-04-12 09:37:27
【问题描述】:
我试图在基于表单提交的电子邮件收据中包含一些 HTML,但是 $message2 中的所有 html 都以纯文本形式发送。
代码:
$to = "no-reply@test.com"; // this is your Email address
$from = $_POST['EMAIL']; // this is the sender's Email address
$first_name = $_POST['FIRSTNAME'];
$last_name = $_POST['SURNAME'];
$subject = "subject";
$subject2 = "subject";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['PRODUCTNAME'];
$message2 =
"<html>" .
"<head>" .
"<title>HTML email</title>" .
"</head>" .
" <table cellspacing='0' cellpadding='0' border='0' align='center' width='600' style='margin: auto;' class='email-container'>
<tr>
<td style='padding: 20px 0; text-align: center'>
<img src='http://placehold.it/200x50' width='200' height='50' alt='alt_text' border='0'>
</td>
</tr>" .
" <tr> " .
"<td style='padding: 20px 0; text-align: center'>" .
"Hi " . $first_name . "," . "\n\n" . "You have sucessfully registered " . $_POST['PRODUCTNAME'] . " (" . $_POST['PRODUCTCODE'] . ")" . "\n\n" .
"Your registration code is " . $unixNow . "\n\n" . "If you have any queries or require assistance, please contact test@test.com" . "\n\n" .
"Please retain a copy of your email confirmation. " .
" </td> " .
" </tr> " .
"<tr>
<td style='padding: 20px 0; text-align: center'>
<img src='http://placehold.it/200x50' width='200' height='50' alt='alt_text' border='0'>
</td>
</tr>" .
"</table>" .
"</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";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
?>
谁能解释我做错了什么以及如何纠正我的错误?
【问题讨论】:
-
$headers = "From:" . $from;会覆盖您之前的$headers,因此没有 ContentType -
谢谢!如何解决这个问题?我是否只需在一个 '$headers =' 中添加所有语句
-
与您将
Content-type:text/html;charset=UTF-8添加到$headers的方式相同:使用.=连接:$headers .= "From:" . $from; -
这似乎没有任何影响
-
"$message2 中的所有 html" 因为您在消息 #2 中使用
$headers2- 它没有设置 MIME 和 ContentType。