即时创建CSV并发送电子邮件,未创建附件

问题描述:

我从一位开发人员继承了PHP任务。基本上我需要立即制作CSV和电子邮件。创建CSV内容正在运行,验证和罚款。但是,在测试电子邮件和CSV时,我成功地生成了电子邮件和附件,但是它们都是空白的,没有文本内容?我确保我的脚本清除了隐藏的字符(标签等),并且在重新测试电子邮件时有内容......但现在没有CSV联系!即时创建CSV并发送电子邮件,未创建附件

这是我的PHP代码:

// Make CSV & Email 
$csvString = chunk_split(base64_encode($csvString)); //CSV is produced earlier and is valid 

// create the email and send it off 

$mailSubject = "Daily CSV from the site"; 
$from = "[email protected]"; 
$headers = "From: " . $from ."\n"; 
$headers .= 'MIME-Version: 1.0' . "\n"; 
$headers .= 'Content-Type: multipart/mixed; 
boundary="----=_ 

NextPart_001_0011_1234ABCD.4321FDAC"' . "\n"; 

$message = 'This is a multi-part message in MIME format. 

------=_NextPart_001_0011_1234ABCD.4321FDAC 
Content-Type: text/plain; 
charset="us-ascii" 
Content-Transfer-Encoding: 7bit 
Hello 

Oh look! Attached a CSV! 

Regards 

------=_NextPart_001_0011_1234ABCD.4321FDAC 
Content-Type: application/octet-stream; name="'; 

$message .= "$cvsName"; 
$message .= '" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="'; 
$message .= "$cvsName"; 
$message .= '" 

'; 
$message .= "$csvString"; // was encoded 
$message .= ' 
------=_NextPart_001_0011_1234ABCD.4321FDAC-- 
'; 

    // now send the email 
    if (mail($email, $mailSubject, $message, $headers, "-f$from")) { 
     echo("Message successfully sent!"); 
    } else { 
     echo("Message delivery failed..."); 
    } 

这是产生的电子邮件,请注意有没有附加CSV但电子邮件确实包含编码CSV文本/值。任何人都可以帮忙吗?

enter image description here

这是产生电子邮件的源代码:

Date: Fri, 05 Apr 2013 14:39:52 +0200 
Subject: Daily CSV from the site 
To: [email protected] 
From: [email protected] 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
boundary="-----=_NextPart_001_0011_1234ABCD.4321FDAC" 



This is a multi-part message in MIME format. 

-----=_NextPart_001_0011_1234ABCD.4321FDAC 

Content-Type: text/plain; 
charset="us-ascii" 
Content-Transfer-Encoding: 7bit 

Hello 

Oh look! Attached a CSV! 

Regards 

-----=_NextPart_001_0011_1234ABCD.4321FDAC 
Content-Type: application/octet-stream; name="marcel.preukschat-Requested-19-03-2013.csv" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="marcel.preukschat-Requested-19-03-2013.csv" 

marcel.preukschat-Requested-19-03-2013.csv 

-----=_NextPart_001_0011_1234ABCD.4321FDAC 
+0

'boundary'似乎打破。在'---- = _'和'NextPart_001_0011_1234ABCD.4321FDAC'之间有一条新的线路 – Uby 2013-04-05 12:14:43

+0

您好,我已经修复了这个问题,但问题仍然存在:( – 2013-04-05 12:20:01

+0

我想你也应该去掉尾部的'--'最后边界的末端 – Uby 2013-04-05 12:21:55

我觉得这与边界定义附近的不必要的换行做。所以让我们缩短边界以使事情更加明显。另见this example of a multipart message

$headers .= 'Content-Type: multipart/mixed; boundary="NextPart_001"' . "\n"; 

及以下部分:

$message = 'This is a multi-part message in MIME format. 
--NextPart_001 
Content-Type: text/plain; charset="us-ascii" 
Content-Transfer-Encoding: 7bit 
Hello 

Oh look! Attached a CSV! 

Regards 

--NextPart_001 
Content-Type: application/octet-stream; name="'; 

$message .= ' 
--NextPart_001-- 
'; 
+0

谢谢你,我已经确定所有的边界是相同的“----- = _ NextPart_001_0011_1234ABCD.4321FDAC”,他们没有换行符,但是我有相同的问题? – 2013-04-05 12:34:40

+0

我刚刚尝试过你的代码,并且在使用Apple Mail查看时会产生正确的附件。你可以检查邮件的源代码,例如通过按Ctrl-U。它绝对看起来像MIME不是d的问题etected。 – akirk 2013-04-05 12:36:56

+0

我正在通过Outlook查看邮件,它不允许我查看源代码,但是我在他们的文件夹中找到了电子邮件(正如我在本地测试的那样)并在NotePad ++中打开它们。我已经将源添加到上面的代码中。 – 2013-04-05 12:42:43