0
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="file.csv"');

    echo "blabla";

    $Email = new CakeEmail();
    $Email->from(array('no-reply@something.com' => 'Something'));
    $Email->to($email);
    $Email->subject('Something');
    $Email->send($textEmail);

How can I output and give the file to download before sending an email? Is it possible? Or I have to create a temporary file for that myself? It works if I don't send an email.

Cœur
  • 37,241
  • 25
  • 195
  • 267
good_evening
  • 21,085
  • 65
  • 193
  • 298

1 Answers1

1

One solution could be:

  1. Create the file and save it to a tempoary location
  2. Send the email with the attachment (using the code you posted in your question)
  3. Use low-level PHP methods to write file contents to HTTP stream (see this answer)
  4. Finally, delete the tempoary file

I think by the looks of the code you've posted, you're nearly there, just got to sort the order out.

I hope this helps.

Community
  • 1
  • 1
Sam Delaney
  • 1,305
  • 11
  • 10