【发布时间】:2016-01-11 06:34:45
【问题描述】:
我想以 CSV 文件的邮件附件形式发送数组数据。
邮件正在发送,但附件不起作用
In Mail inbox My output data is :
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hello Manager!!!
Please find coupon code file.
Content-Type: text/csv
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.csv"
IlNyIG5vIiwiQ291cG9uIENvZGUiLEZlZQoxLEBUSFYxM0dRLDEwMAo=
我的代码部分是:
function create_csv_string($data = array()) {
// Open temp file pointer
if (!$fp = fopen('php://temp', 'w+')) return FALSE;
// Loop data and write to file pointer
foreach ($data as $line) fputcsv($fp, $line);
// Place stream pointer at beginning
rewind($fp);
// Return the data
return stream_get_contents($fp);
}
function send_csv_mail ($csvData, $body, $to, $subject, $from) {
// This will provide plenty adequate entropy
$multipartSep = '-----'.md5(time()).'-----';
// Arrays are much more readable
$headers = array(
"From: $from",
"Reply-To: $from",
"Content-Transfer-Encoding: UTF-8",
"Content-Type: multipart/mixed; boundary=\"$multipartSep\"",
"Pragma: no-cache",
"Expires: 0"
);
// Make the attachment
$attachment = chunk_split(base64_encode($this->create_csv_string($csvData)));
// Make the body of the message
$body = "--$multipartSep\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$body\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"file.csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
return @mail($to, $subject, $body, implode("\r\n", $headers));
}
附件在实时模式下不工作。在本地它工作正常。
请帮我看看我想念什么?
谢谢
【问题讨论】:
标签: php codeigniter csv