PHP邮件功能html内容在电子邮件中不可见
Posted
技术标签:
【中文标题】PHP邮件功能html内容在电子邮件中不可见【英文标题】:PHP mail function html content not visible in email 【发布时间】:2015-12-30 16:47:30 【问题描述】:我正在尝试使用 php mail() 函数发送带有附件的电子邮件。带有附件的电子邮件正确发送,但 html 内容(邮件正文)在我的电子邮件中不可见。它只显示附件。
<?php
$htmlbody="
<strong>Name:</strong> $_POST['name']<br>
<strong>Position:</strong> $_POST['position']<br>
<strong>Email:</strong> $_POST['email']<br>
<strong>Phone:</strong> $_POST['phone']<br>
<strong>Date Of Birth:</strong> $_POST['dob']<br>
<strong>Qualification:</strong> $_POST['qual']<br>
<strong>Experiance:</strong> $_POST['experiance']<br>
<strong>Remarks:</strong> $_POST['remarks']";
$strSid = md5(date('r', time()));
$strHeader = "";
$strHeader .= "From: $_POST['email']\nReply-To: $_POST['email']\n";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n";
$strHeader .= "Content: ".$htmlbody."\n";
//*** Attachment ***//
if(isset($_FILES['resume']))
$filename = $_FILES['resume']['name'];
$ext = pathinfo($filename,PATHINFO_EXTENSION);
if(!in_array($ext,array('doc','docx'))) die('Invalid File');
move_uploaded_file($_FILES['resume']['tmp_name'],"temp/$filename");
$strContent = chunk_split(base64_encode(file_get_contents("temp/$filename")));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$filename."\"\n";
$to = "name@domain.com";
$subject = "Test email with attachment";
$mail = mail($to, $subject, $htmlbody, $strHeader);
echo $mail ? "Mail sent" : "Mail failed";
?>
【问题讨论】:
试过用<html><body></body></html>
标签包裹它吗?
@Epodax:什么都没发生,伙计……
【参考方案1】:
只需删除“$strHeader .= "This is a multi-part message in MIME format.\n";
”并将“$strHeader .= "Content: ".$htmlbody."\n";
”替换为“$strHeader .= "$htmlbody."\n";
”
【讨论】:
以上是关于PHP邮件功能html内容在电子邮件中不可见的主要内容,如果未能解决你的问题,请参考以下文章
图像/svg+xml 格式的 Mandrill base64 内联图像在电子邮件中不可见?
如何强制电子邮件程序显示使用 php 在 html 电子邮件中发送的图像?