php - PHP邮件没有格式化HTML
问题描述
我试图在基于表单提交的电子邮件收据中包含一些 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
?>
有人可以解释我做错了什么以及如何纠正我的错误吗?
解决方案
您的消息 #2 不使用您定义的 MIME/ContentType 标头。
$headers = "From:" . $from;
也会覆盖那些。将您的 MIME/ContentType 标头命名为不同的名称,并将其添加到您的消息 #2 标头中:
// Always set content-type when sending HTML email
$htmlHeaders = "MIME-Version: 1.0" . "\r\n";
$htmlHeaders .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From:" . $from;
$headers2 = $htmlHeaders . "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
推荐阅读
- docker - ROS 安装后 Dockerfile RUN 命令停止工作
- android - 使用 exoplayer 的 Android kotlin 应用程序崩溃而没有错误指示
- amazon-aurora - 无法连接到 Aurora MySQL 数据库
- windows - PowerShell:客户端无法连接到请求中指定的目标
- javascript - 找不到变量:token
- python - 从 Internet 复制整个文件夹
- python - 如何使用 python 从 Outlook 中的 MailItem 获取发件人 SMTP 电子邮件地址?
- apache - htaccess:可以匹配一个斜杠,但不能匹配双斜杠
- php - Woocommerce set_price 功能不一致
- reactjs - 将 ReactJS Okta 身份验证从 OAuth 2.0 更改为 SAML 2.0