2012-04-26 94 views
2

這裏是我的嘗試,你能讓我知道我在做什麼錯了嗎?如何用字符串發送附件中的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); 
?> 

注:我必須使用郵件功能我沒有選擇(我可以用一個包裝,但我寧願不要),我不能保存在服務器端的文件。

回答

19

您可以將CSV創建代碼以更簡短更高效的方式輕鬆包裝到單個功能中。

CSV文件的正確MIME類型是text/csv。此外,你應該使用字符串連接和明確\r\n序列,因爲RFC的要求CRLF行分離和使用定界符/ nowdoc /有文字換行輸出緩衝,你將最終獲得2個問題:

  1. 您的壓痕可能弄糟消息格式
  2. 經由FTP ASCII模式複製該文件可能打破行結尾

嘗試此代替:

function create_csv_string($data) { 

    // 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 = '[email protected]', $subject = 'Test email with attachment', $from = '[email protected]') { 

    // This will provide plenty adequate entropy 
    $multipartSep = '-----'.md5(time()).'-----'; 

    // Arrays are much more readable 
    $headers = array(
    "From: $from", 
    "Reply-To: $from", 
    "Content-Type: multipart/mixed; boundary=\"$multipartSep\"" 
); 

    // Make the attachment 
    $attachment = chunk_split(base64_encode(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--"; 

    // Send the email, return the result 
    return @mail($to, $subject, $body, implode("\r\n", $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_csv_mail($array, "Hello World!!!\r\n This is simple text email message."); 
+0

非常感謝你我得到了這一切,現在完美的工作。 (最後一行send_mail應該是send_csv_mail雖然) – 2012-04-27 06:29:19

+0

非常感謝!我以這種完全錯誤的方式解決這個問題,並且解決了所有問題! – 2013-10-25 18:16:37

1

這是一個更簡單的解決方案,用於創建用於通過phpmailer向附件發送電子郵件的csv。

$csvinfo .= "$row[0],$row[1],$row[2],$row[3]\n"; 
$attachment = "details.csv"; 
file_put_contents($attachment, $csvinfo, LOCK_EX); 

及相關PHPMailer的線路發送一封電子郵件,附件

$mail = new PHPMailer; 
$mail->isSMTP();    // Set mailer to use SMTP 
$mail->Host = 'x';   // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;  // Enable SMTP  authentication 
$mail->Username = 'xx';  // SMTP username 
$mail->Password = 'xx';   // SMTP password 
$mail->SMTPSecure = 'tls';  // Enable TLS or SSL also accepted 
$mail->Port = 587;    // TCP port to connect to 
$mail->setFrom('[email protected]', 'Lily'); 
$mail->AddAddress("[email protected]"); 
$mail->addAttachment($attachment);   // Add attachments 
$mail->isHTML(true);    // Set email format to HTML 
$mail->Subject = "Report"; 
$mail->Body = $message; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
}