简体   繁体   English

在php中发送额外的附件

[英]An extra attachement is being sent in php

I use the following code to create and send the csv file to a specific mail box in php. 我使用以下代码创建并将csv文件发送到php中的特定邮箱。 I can successfully receive the csv file, however, I do not understand why one more txt file called ATT00001.txt is attached also. 我可以成功接收csv文件,但是,我不明白为什么还要附加一个名为ATT00001.txt的txt文件。 Can anyone help me to take a look? 任何人都可以帮我看看吗?

Here is the part of code for sending the mail: 以下是发送邮件的代码部分:

// email fields: to, from, subject, and so on 
$to = "you@home.com"; 
$from = "me@home.com";  
$subject ="Test mail";  
$message = "please check the csv out!"; 
$headers = "From: $from"; 

$fileName = pathtocsv;

// boundary  
$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

// headers for attachment  
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";  

// multipart boundary  
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
$message .= "--{$mime_boundary}\n"; 

// preparing attachments 
$file = fopen($fileName,"rb"); 
$data = fread($file,filesize($fileName)); 
fclose($file); 
$data = chunk_split(base64_encode($data)); 
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"record.csv\"\n" .  
"Content-Disposition: attachment;\n" . " filename=\"test.csv\"\n" .  
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
$message .= "--{$mime_boundary}\n"; 

mail($to, $subject, $message, $headers);

再发送一个文本文件
And when I view it, it is just an empty txt file. 当我查看它时,它只是一个空的txt文件。 I open the mail in Outlook. 我在Outlook中打开邮件。

Changing the last boundary line: 更改最后一条边界线:

$message .= "--{$mime_boundary}\n";

to: 至:

$message .= "--{$mime_boundary}--\n";

Note the extra -- at the end. 注意额外--最后。

RFC 2046 ( § 5.1.1 - Common Syntax [page 19]) states that: RFC 2046 (第5.1.1节 - 通用语法[第19页])指出:

The boundary delimiter line following the last body part is a 最后一个身体部位后面的边界定界符是a
distinguished delimiter that indicates that no further body parts 区分分隔符,表示没有其他正文部分
will follow. 将遵循。 Such a delimiter line is identical to the previous 这样的分隔线与前一个相同
delimiter lines, with the addition of two more hyphens after the 分隔线,后加两个连字符
boundary parameter value. 边界参数值。

--gc0pJq0M:08jU534c0p-- --gc0pJq0M:08jU534c0p--

Since the last boundary was missing, it seems to be the default behaviour that Outlook creates an attachment containing some extra data or an empty attachment. 由于缺少最后一个边界,因此它似乎是Outlook创建包含一些额外数据或空附件的附件的默认行为。

The second answer here by James-Luo may also be valid. 第二个答案在这里由詹姆斯·洛也可能是有效的。 He states that adding an attachment to a MIME message prior to the message body can result in similar outcome with the attachments. 他指出,在邮件正文之前添加MIME邮件的附件可能会导致与附件类似的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM