我试图使用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";
?>
答案 0 :(得分:0)
只需删除&#34; $strHeader .= "This is a multi-part message in MIME format.\n";
&#34;并替换&#34; $strHeader .= "Content: ".$htmlbody."\n";
&#34;到&#34; $strHeader .= "$htmlbody."\n";
&#34;