Send exported csv via email with PHPMailer

Send exported csv via email with PHPMailer

culterculter Posts: 102Questions: 24Answers: 0

Hi, it's almost a year since I discovered and working with datatables and now I'm in love with it. Great work, really, especially your kind advices here in forum are very helpful.

So, here is my recent task. I have working DataTables (server-side processing) with exporting to csv. Now I need not to download the csv file, but attach it to an email and send it to the use's email address which is stored in the variable. I'm using PHPMailer which I hope is enough for this task.

Of course I don't want to program it for me, I just need some advice how to manage this. Is it better to save the file on the server and then grab the file, attach it to the email and send or is it possible to attach it without storing on the server? The second solution seems better for me, but I'm not sure it's easier to do. I'm not very experienced in PHP. Or you know some better way how to do it?

Thank you.

Answers

  • rf1234rf1234 Posts: 2,939Questions: 87Answers: 415

    You could use "customize" to get hold of the csv-content or even the file itself and send it to the server with an ajax call. Then use PHP Mailer to send it.

    Please take a look at this and the example at the button highlighting what you can do with "customize" (zebral's comment):
    https://datatables.net/reference/button/csv

    I have no code example for this; I personally use a different approach. I generate the documents with PHP Spreadsheet on the server side and send them from there using something similar to PHP Mailer.

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin

    The only trouble with using csv here is that server-side processing is being used. So only the current display would be saved.

    I'd suggest forgoing using DataTables' export buttons at all when using server-side processing and instead doing what is suggested in your post @culter:

    Is it better to save the file on the server and then grab the file,

    Yes. Have the export button send an Ajax request to the server which will generate the CSV file from the database, save it to a temporary file on the server, attach it to an e-mail, send and then delete the temp. file.

    Allan

This discussion has been closed.