php - 正在用 php 发送一个额外的附件

标签 php

我使用以下代码创建 csv 文件并将其发送到 php 中的特定邮箱。我可以成功收到 csv 文件,但是,我不明白为什么还要附加一个名为 ATT00001.txt 的 txt 文件。谁能帮我看看?

发送邮件的部分代码如下:

// 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);

One more text file is sent
当我查看它时,它只是一个空的 txt 文件。我在 Outlook 中打开邮件。

最佳答案

改变最后的边界线:

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

到:

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

注意最后的额外 --

RFC 2046 (§ 5.1.1 - 通用语法 [第 19 页]) 指出:

The boundary delimiter line following the last body part is 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--

由于缺少最后一个边界,这似乎是 Outlook 创建包含一些额外数据或空附件的附件的默认行为。

第二个答案here James-Luo 的说法也可能有效。他指出,在邮件正文之前向 MIME 邮件添加附件可能会导致与附件类似的结果。

关于php - 正在用 php 发送一个额外的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11928004/

相关文章:

javascript - PHP + Jquery 根据两个 Dropdown 更改 Textbox 的值

php - Symfony 2 - 使用子实体创建表单并上传文件

php - 使用 mySQL 查询仅获取第一行

php - 如何在yii框架网格的一列中放置两个imagelink?

php - 保护数据库内容不被 'sniffed' 删除(Ajax)

php - Facebook 的 l.php

PHP fopen() 内存效率和使用

php - 用php显示多个搜索结果

php - 读取和写入 mysql

php - 如何使用两个连接获取最后的数据