【问题标题】:Mail + attachment is not convert as a CSV file邮件 + 附件未转换为 CSV 文件
【发布时间】: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


    【解决方案1】:

    试试这个 ::

    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); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 2021-01-31
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多