我从一位开发人员那里继承了一个 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 文本/值。任何人都可以帮忙吗?
这是生成的电子邮件的源代码:
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