无法正确添加附件-部分标头已添加到文件中


Unable to properly add an attachment - part of headers are added to file

我想用PHP的mail()发送一封带有pdf附件的电子邮件。问题是,最后我的附件文件是不可读的,因为它不是从base64解码,而是附加了一部分头。我的猜测是我用错误的方式连接了头,但我试图用各种方式更改它,结果总是一样的。

这是我收到的最后一封纯文本邮件内容:

MIME-Version: 1.0
From: XXX - xxxxx <xxxx.xxxx@xxxxx.com>
Content-Type: multipart/mixed; boundary=_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x
This is a MIME encoded message.
--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x
Content-type: text/plain;charset=utf-8
Verification Email
To activate the account click on the following link or copy-paste it in your browser:
http://10.24.35.92:443/?ctrl=users&act=verify&id=174&hash=4e4b5fbbbb602b6d35bea8460aa8f8e5
--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x
Content-type: text/html;charset=utf-8
<h4>Verification Email</h4>
<table>
<tr><td>To activate the account click on the following link or copy-paste it in your browser:</td></tr>
<tr><td>http://10.24.35.92:443/?ctrl=users&act=verify&id=174&hash=4e4b5fbbbb602b6d35bea8460aa8f8e5</td></tr>
--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x
Content-Type: application/pdf; name="xxxx.pdf"; 
Content-Transfer-Encoding: base64; 
Content-Disposition: attachment; filename="xxxx.pdf";
(here goes the encoded attachment)
JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
MDAwMCBuDQowMDAwMTUzODUwIDAwMDAwIG4NCjAwMDAxNzM4NzUgMDAwMDAgbg0KMDAwMDE3NDEw
MSAwMDAwMCBuDQp0cmFpbGVyDQo8PC9TaXplIDIxOS9Sb290IDEgMCBSL0luZm8gMzEgMCBSL0lE
(...)
--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x--

这就是pdf附件在文本编辑器中的样子

Content-Transfer-Encoding: base64;
Content-Disposition: attachment; filename="xxxx.pdf"
JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDMyIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4+
(...)

您需要使用多部分/混合MIME类型。像这样的东西应该有效:

<form name="form1" enctype="multipart/form-data" method="post" action="">
 <label for="name">Your Name</label>
 <input type="text" id="name" name="name" />  
 <label for="email">Your Email</label> 
 <input type="email" id="email" name="email" />
 <label for="myfile">Attachment</label>
 <input type="file" id="myfile" name="my_file" />
 <input type="submit" name="button" value="Submit" />
</form>

PHP代码

if($_POST && isset($_FILES['my_file'])){
$from_email = 'example@senderMail.com'; 
$to = 'to_mail@example.com'; 
$subject = 'Test mail with attachment'; 
$message = 'This is body of the message'; 
//get file details we need
$file_tmp_name    = $_FILES['my_file']['tmp_name'];
$file_name        = $_FILES['my_file']['name'];
$file_size        = $_FILES['my_file']['size'];
$file_type        = $_FILES['my_file']['type'];
$file_error       = $_FILES['my_file']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
    die('upload error');
}
//read from the uploaded file
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
//encode it with MIME base64, and split it into smaller chunks
$encoded_content = chunk_split(base64_encode($content));
//create a boundary string. It must be unique, you can use the MD5 algorithm to generate a random hash 
$boundary = md5(date('r', time()));  
//header
$headers = "MIME-Version: 1.0'r'n"; 
$headers .= "From:".$from_email."'r'n"; 
$headers .= "Reply-To: ".$user_email."" . "'r'n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary'r'n'r'n"; 
//plain text 
$body = "--$boundary'r'n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1'r'n";
$body .= "Content-Transfer-Encoding: base64'r'n'r'n"; 
$body .= chunk_split(base64_encode($message)); 
//attachment
$body .= "--$boundary'r'n";
$body .="Content-Type: $file_type; name="$file_name"'r'n";
$body .="Content-Disposition: attachment; filename="$file_name"'r'n";
$body .="Content-Transfer-Encoding: base64'r'n";
$body .="X-Attachment-Id: ".rand(1000,99999)."'r'n'r'n"; 
$body .= $encoded_content; 
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{       
    die('Thank you for your email');
}else{
    die('Could not send mail! Please check your PHP mail configuration.');  
}
}

希望能有所帮助。

干杯,文斯。