使用 phpmailer 发送邮件时,若邮件模板以 html 形式嵌入但收件端仅显示原始 html 代码而非渲染效果,根本原因是未正确声明邮件内容类型为 `text/html`,需在邮件头中显式添加 `content-type: text/html; charset=utf-8`。
您当前的代码存在两个关键问题:
✅ 正确做法(使用原生 mail() + HTML 模板):
function sendCOTP($REmail, $RVID) {
$to 
= $REmail;
$subject = 'Allloooooooooooo Bhindiiiiiii';
// 捕获模板输出(确保 mailtemplet.php 输出的是合法 HTML)
ob_start();
include './mailtemplet.php';
$body = ob_get_clean();
// 构建 HTML 邮件正文(注意:应将业务逻辑融入模板,而非拼接字符串)
$htmlMessage = '
Dear Sir,
Guess what ??? ' . htmlspecialchars($RVID) . ' venue that you checked earlier is now finally available!
Please check it out and book it before it gets taken.
Thank you,
Team Venueazy
';
// 关键:设置完整的 HTML 邮件头(含 Content-Type 和字符编码)
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n";
$headers .= 'Reply-To: [email protected]' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
// 发送 HTML 邮件(注意:第三个参数是正文,第四个参数才是 headers)
return mail($to, $subject, $htmlMessage, $headers);
}⚠️ 重要注意事项:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->setFrom('[email protected]');
$mail->addAddress($REmail);
$mail->isHTML(true); // ✅ 核心开关
$mail->Subject = $subject;
$mail->Body = $htmlMessage; // 直接赋值 HTML 字符串
$mail->send();总结:HTML 邮件不渲染的本质是 MIME 类型缺失。修复 headers 中的 Content-Type 是基础解法;长远来看,拥抱标准库(如 PHPMailer)能规避大量底层陷阱,提升可维护性与送达率。