邮件+附件未转换为CSV文件

时间:2016-01-11 06:34:45

标签: php codeigniter csv

我想将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)); 
}

附件无法在实时模式下使用。在当地它工作正常。

请帮助我,我想念的是什么?

由于

1 个答案:

答案 0 :(得分:0)

试试这个::

function create_csv_string($data = array()) {
  $str = "";

  foreach ($data as $line) $str .= $line."\n";

  return $str;

}

function send_csv_mail ($csvData, $body, $to, $subject, $from) {
     $uid = md5(uniqid(time()));

     $myfile = "Output file name.csv";
     $message = "";

     $attachment = chunk_split(base64_encode($this->create_csv_string($csvData)));

     $header = "From: ".$from."\r\n";
     $header .= "MIME-Version: 1.0\r\n";
     $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
     $header .= "This is a multi-part message in MIME format.\r\n";
     $header .= "--".$uid."\r\n";
     $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
     $header .= $message."\r\n\r\n";
     $header .= "--".$uid."\r\n";
     $header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
     $header .= "Content-Transfer-Encoding: base64\r\n";
     $header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
     $header .= $attachment."\r\n\r\n";
     $header .= "--".$uid."--";



   return mail($to, $subject, $body, $headers); 
}