这里是我的尝试,你能让我知道我在做什么错了吗?如何用字符串发送附件中的CSV附件的电子邮件
allputcsv将数组转换为csv格式的字符串。这是base64编码和块拆分,这似乎适用于正常的文件,但有什么不同,我应该做的,因为我使用的是字符串?
<?php
function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = "\n")
{
static $fp = false;
if ($fp === false)
{
$fp = fopen('php://temp', 'r+');
}
else
{
rewind($fp);
}
if (fputcsv($fp, $row, $delimiter, $enclosure) === false)
{
return false;
}
rewind($fp);
$csv = fgets($fp);
if ($eol != PHP_EOL)
{
$csv = substr($csv, 0, (0 - strlen(PHP_EOL))) . $eol;
}
return $csv;
}
function allputcsv($arr) {
$str = "";
foreach($arr as $val) {
$str .= sputcsv($val);
}
return $str;
}
function send_mail($arr) {
$to = '[email protected]';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"--".$random_hash."\"";
$attachment = chunk_split(base64_encode(allputcsv($arr)));
ob_start();
?>
--<?php echo $random_hash; ?>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--<?php echo $random_hash; ?>
Content-Type: application/vnd.ms-excel;
name="test.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="test.csv"
<?php echo $attachment; ?>
--<?php echo $random_hash; ?>--
<?php
$message = ob_get_clean();
$mail_sent = @mail($to, $subject, $message, $headers);
}
$array = array(array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7));
send_mail($array);
?>
注:我必须使用邮件功能我没有选择(我可以用一个包装,但我宁愿不要),我不能保存在服务器端的文件。
非常感谢你我得到了这一切,现在完美的工作。 (最后一行send_mail应该是send_csv_mail虽然) – 2012-04-27 06:29:19
非常感谢!我以这种完全错误的方式解决这个问题,并且解决了所有问题! – 2013-10-25 18:16:37