电子邮件CSV文件与PHP


Emailing CSV file with PHP

我已经从我的数据库中提取数据,并在表格格式的网页上显示它。我在这个页面上提供了一个链接,允许用户以CSV文件的形式下载这些数据。到目前为止,这是正确的,当用户跟随显示数据下面的链接时,它允许他们保存它。

它还发送一封带有CSV作为附件的电子邮件。然而,目前附件中的CSV是空白的,我不知道为什么。

所有我想要的是从数据库的数据,这是放置到一个可下载的CSV也进入附加的CSV,我不能这样做。

有人能帮帮我吗?

这是我到目前为止的代码:

// Create CSV file
fputcsv($output, array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5'));
$mysql_connection = db_connect_enhanced('*******','******','******','******');
$query='SELECT * FROM ****.****';
$surveys = db_query_into_array_enhanced($mysql_connection, $query);
$count = count($surveys);
$data = array();
    for($i=0; $i<=$count; $i++){
    $data[] = array($surveys[$i]['FeedbackName'], $surveys[$i]['BranchName'], $surveys[$i]['FeedbackWebsite'], $surveys[$i]['FeedbackCompany'], $surveys[$i]['Question1'], $surveys[$i]['Question2'], $surveys[$i]['Question3'], $surveys[$i]['Question4'], $surveys[$i]['Question5']);  
}
foreach( $data as $row )  
{  
    fputcsv($output, $row, ',', '"');  
}  
$encoded = chunk_split(base64_encode($data));
// create the email and send it off
$subject = "File you requested from RRWH.com";
$from = "***************";
$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
We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
as a zip file.
Regards
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream;  name="';
$message .= "surveys.csv";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "surveys.csv";
$message .= '"
';
$message .= "$encoded";
$message .= '
------=_NextPart_001_0011_1234ABCD.4321FDAC--
';
mail("*************", $subject, $message, $headers, "-f$from");
fclose($output);  

感觉我离答案如此之近,但我就是看不到答案。

改变这一行:

$message .= "$encoded";

和这个:

$message .= $encoded;

这只是一个"暗示性"的答案:

这是我用来附加文件的方法,它对我有用,它可能会帮助你"BASE"自己解决你遇到的问题。

$file_path = "/path_to_your/$file_name";
$file_path_name = "$file_name"; // this file name will be used at receiver end 
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file); 
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x"; 
$headers .= "'nMIME-Version: 1.0'n" .
"Content-Type: multipart/mixed;'n" .
" boundary='"{$mime_boundary}'""; 
$message .= "This is a multi-part message in MIME format.'n'n" .
"--{$mime_boundary}'n" .
"Content-Type:text/html; charset='"iso-8859-1'"'n" .
"Content-Transfer-Encoding: 7bit'n'n" .
$message .= "'n'n"; 
$data = chunk_split(base64_encode($data)); 
$message .= "--{$mime_boundary}'n" .
"Content-Type: {$file_path_type};'n" .
" name='"{$file_path_name}'"'n" .
"Content-Disposition: attachment;'n" .
" filename='"{$file_path_name}'"'n" .
"Content-Transfer-Encoding: base64'n'n" .
$data .= "'n'n" .
"--{$mime_boundary}--'n";