doushang1778 2019-07-23 06:05
浏览 413

如何在将json数组转换为csv后访问csv文件

I needed to convert a json array to .csv file, so I looked it up and managed to come up with some code. Now, I supposedly have a csv file, however I don't know how to send it as an attachment to a particular email address.

$arrayData = json_decode($jsonData, true);
$csvFileName = 'reports.csv';
$fp = fopen($csvFileName, 'w');

foreach($arrayData as $row){
    fputcsv($fp, $row);
}

fclose($fp);

$to = 'petkoutekal@gmail.com';                                             
$subject = 'IronWifi reports';
$message = 'reports';
$file = $fp;           //probably wrong

mailto()

My final goal is to send this csv file as an attachment to a particular email address. Any help will me appreciated.

  • 写回答

1条回答 默认 最新

  • dongshushen4392 2019-07-23 06:34
    关注

    I have updated following example as per your requirement

    function create_csvfileFromString($data) { // Function for convert string to csv file
    
      // 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_mailWithCSV ($csvData, $body, $to = 'ToEmailAddress@example.com', $subject = 'Email with attachment', $from = 'FromEmail@example.com') {
    
      $multipartSep = '-----'.md5(time()).'-----';
    
      // Arrays are much more readable
      $headers = array(
        "From: $from",
        "Reply-To: $from",
        "Content-Type: multipart/mixed; boundary=\"$multipartSep\""
      );
    
      // Add JSON file as attachment Make the attachment
      $attachment = chunk_split(base64_encode(create_csvfileFromString($csvData))); 
    
      // Make the body of the MAIL message
      $body = "--$multipartSep
    "
        . "Content-Type: text/plain; charset=ISO-8859-1; format=flowed
    "
        . "Content-Transfer-Encoding: 7bit
    "
        . "
    "
        . $body."
    "
        . "--$multipartSep
    "
        . "Content-Type: text/csv
    "
        . "Content-Transfer-Encoding: base64
    "
        . "Content-Disposition: attachment; filename=\"file.csv\"
    "
        . "
    "
        . $attachment."
    "
        . "--$multipartSep--";
    
       // Send the email, return the result
       return @mail($to, $subject, $body, implode("
    ", $headers)); 
    
    }
    
    $array = array(array("data1","data2","data3","data4"), array("array1","array2","array3","array4"), array("array6","array7","array8","array9"));
    
    send_mailWithCSV($array, "Hello User!!! 
     Email Message");
    
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline