使用PHP发送带有(HTML,纯文本和附件)的Multipart电子邮件

时间:2015-07-07 07:28:31

标签: php html email

我发送了一个包含html格式和附件的php的多部分电子邮件... 一切正常,但是当用户从我们的网站收到该电子邮件时,它在最后的HTML部分中也显示了边界。 但当我删除边界线时,电子邮件显示文件内容为base64(无附件)。

 if ( isset($_POST['submit']) )
 {
 $to = 'admin@abc.com';
 $FromSubmitting = $_POST['submitting'];
$FromTitle = $_POST['title'];
$FromName = $_POST['name'];
$FromDesignation = $_POST['designation'];
$FromQualification = $_POST['qualification'];
$FromInstitute = $_POST['institute_address'];
$FromHomeAddress = $_POST['home_address'];
$FromEmail = $_POST['email'];
$FromWorkPhone = $_POST['work_phone_number'];
$FromHomePhone = $_POST['home_phone_number'];
$FromMobilePhone = $_POST['mobile_phone_number'];
$FromPresentingAuthor = $_POST['presenting_author'];
$FromConferenceTheme = $_POST['conference_theme'];
$FromTypeOfSubmission = $_POST['type_of_submission'];

$subject = "New ".$FromSubmitting." Submitted From: ".$FromName;


/* GET File Variables */
$tmpName = $_FILES['uploaded_file']['tmp_name'];
$fileType = $_FILES['uploaded_file']['type'];
$fileName = $_FILES['uploaded_file']['name'];
/* Start of headers */
$headers = "From: $FromEmail";

if (file($tmpName)) {  
/* Reading file ('rb' = read binary)  */
$file = fopen($tmpName,'rb'); 
$data = fread($file,filesize($tmpName));
fclose($file);   /* a boundary string */  
$randomVal = md5(time());  
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";   

/* Header for File Attachment */  
$headers .= "\nMIME-Version: 1.0\n";  
$headers .= "Content-Type: multipart/mixed;\n" ;  
$headers .= " boundary=\"{$mimeBoundary}\"";   

/* Multipart Boundary above message */  

 $message .= "--{$mimeBoundary}\n"
. "Content-Type: text/html; charset=UTF-8\n"
. "Content-Transfer-Encoding: 8bit\n\n"  
. "<table>
<tr><td>Submission Type:</td><td>$FromSubmitting</td></tr>
<tr><td>Name:</td><td>$FromTitle $FromName</td></tr>
<tr><td>Designation:</td><td>$FromDesignation</td></tr>
<tr><td>Qualification:</td><td>$FromQualification</td></tr>
<tr><td>Institute:</td><td>$FromInstitute</td></tr>
<tr><td>Home Address:</td><td>$FromHomeAddress</td></tr>
<tr><td>Work Phone:</td><td>$FromWorkPhone</td>
<td>Home Phone:</td>
<td>$FromHomePhone</td><td>Mobile Phone:</td>
<td>$FromMobilePhone</td></tr>
<tr><td>Presenting Author:</td><td>$FromPresentingAuthor</td></tr>
<tr><td>Conference Theme:</td><td>$FromConferenceTheme</td></tr>
<tr><td>Type of Submission:</td><td>$FromTypeOfSubmission</td></tr>
</table>"
.  "--{$mimeBoundary}\n"; // when removing this line it shows Image-2


/* Encoding file data */  
$data = chunk_split(base64_encode($data));   

/* Adding attchment-file to message*/  
$message .= "--{$mimeBoundary}\n" 
.  "Content-Type: {$fileType};\n" 
.  " name=\"{$fileName}\"\n" 
.  "Content-Transfer-Encoding: base64\n\n" 
.  $data . "\n\n" 
.  "--{$mimeBoundary}--\n";
}

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

}

电子邮件屏幕截图 Image-1 enter image description here

电子邮件屏幕截图 Image-2 enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定,但你可以尝试一下。

/ *将attchment-file添加到消息* /

$message .= "--{$mimeBoundary}\n"
."Content-Type: application/octet-stream;\n" 
.  " name=\"{$fileName}\"\n" 
.  "Content-Transfer-Encoding: base64\n\n" 
.  $data . "\n\n" 
.  "--{$mimeBoundary}--\n";