0

I have a script that currently takes some data from a wordpress DB and then loops over the returned rows and uses fputcsv(). The file handle was setup with:

$fh = fopen('php://output', 'w');

The script is linked to a link of a webpage and when you click the link it downloads the CSV (using the content-disposition header).

Is it possible to write to php://output this CSV file and then use PHP's mail() function to send it in an attachment?

I have a mail function I've written that will set the MIME type to multipart/mixed, I'm just not sure how to create the actual attachment to be emailed.

2
  • Careful you don't make this an XY Problem. The solution you come up with isn't always the best way to reach your goal. Commented Oct 15, 2012 at 1:09
  • Just to make your life easier, take a look at Pear Mail or PHPMailer Commented Oct 15, 2012 at 1:21

1 Answer 1

1

Use tmpfile() instead. Write your CSV data to that temp file, then attach it to your email.

Plus, don't write your own mime handling/generating functions. Use a library like PHPMailer or Swiftmailer to do it for you. Far easier and far more reliable. Part of their attachment handling code allows you specify the filename the user sees, so even though it might be "/tmp/abc123def", it'll show up as "data.csv" (or whatever you specify) in the actual email.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.