Skip to content

Instantly share code, notes, and snippets.

@eichgi
Last active June 12, 2020 16:47
Show Gist options
  • Save eichgi/bfd20e5b886a111e1e70b85a380acc9c to your computer and use it in GitHub Desktop.
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
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@domain.com']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment