在飞行中创建CSV并通过电子邮件发送,未创建附件

时间:2013-04-05 11:55:58

标签: php csv

我从其他开发人员那里继承了一个PHP任务。基本上我需要动态制作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 = "root@localhost.com";
$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: root@localhost.com
From: root@localhost
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

1 个答案:

答案 0 :(得分:0)

我认为这与边界定义附近不必要的换行有关。所以让我们让边界更短一点,让事情变得更加明显。另请参阅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--
';