Last active
June 12, 2020 16:47
-
-
Save eichgi/bfd20e5b886a111e1e70b85a380acc9c to your computer and use it in GitHub Desktop.
Make and zip a csv file from an array then send it by email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function makeCSV($data, $group) { | |
$file_name = storage_path('app/tmp/' . uniqid($group . '_') . '.csv');; | |
$file = fopen($file_name, 'w'); | |
$headers = array_keys(reset($data)); | |
fputcsv($file, $headers); | |
foreach ($data as $fields) { | |
fputcsv($file, $fields); | |
} | |
fclose($file); | |
$gz_file = $file_name . '.gz'; | |
$gz = gzopen($gz_file, 'w9'); | |
gzwrite($gz, file_get_contents($file_name)); | |
gzclose($gz); | |
unlink($file_name); | |
return $gz_file; | |
} | |
$file_name = $this->makeCSV($data, $group); | |
$message = 'Sent by X'; | |
Mail::raw($message, function ($message) use ($file_name) { | |
$message->subject('Subject') | |
->attach($file_name) | |
->to(['[email protected]']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment