CodeIgniter Forums
Export data from table and email it. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Export data from table and email it. (/thread-36106.html)



Export data from table and email it. - El Forum - 11-21-2010

[eluser]Brad K Morse[/eluser]
I am unsure about how to make this happen, I do have it working to export the table to a csv and it forces the download, but that won't be needed when trying to attach it to an email.

I also have a function for sending an email.

I need help putting these two together to export, then email that csv file.

Code:
// controller: export
function export() {
    $this->load->dbutil();
    $this->load->model('data_model');
    $csv = $this->data_model->exportCsv();
    $file_name = "spreadsheet.csv";
    force_download($file_name,$csv);
}

// model: exportCsv
function exportCsv() {
    $this->load->dbutil();
    $q = $this->db->select('first_name, tshirt')->order_by('first_name','asc')->get('data');
    return $this->dbutil->csv_from_result($q, ",", "\n");
}

// controller: emailCsv
function emailCsv() {
    $this->email->from('[email protected]', 'Jane Doe');
    $this->email->to('[email protected]', 'John Doe Morse');

    $this->email->subject('Spreadsheet');
    $this->email->message('Here is the list');    
    $this->email->attach($file); // want to attach the csv file here

    $this->email->send();

    echo $this->email->print_debugger();
}

The emailCsv controller function I just wrote, but not being used anywhere right now.

I am not sure how I can attach the csv file (spreadsheet.csv) to $this->email->attach() within emailCsv

Any help is very appreciated.


Export data from table and email it. - El Forum - 11-21-2010

[eluser]jrtashjian[/eluser]
You need to pass the full server path to the file you want to attach, not just the filename.


Export data from table and email it. - El Forum - 11-21-2010

[eluser]Brad K Morse[/eluser]
Thanks, I understand that, but what I am unsure about is how to pass the result of export (the csv file), into emailCsv.


Export data from table and email it. - El Forum - 11-21-2010

[eluser]jrtashjian[/eluser]
You have to write the results as a file somewhere, then attach it. You cannot pass the results directly. The function $this->email->attach() looks for the physical file that you passed as the parameter.


Export data from table and email it. - El Forum - 11-21-2010

[eluser]Brad K Morse[/eluser]
Makes sense.

Too bad there was not a helper file that passed a string into an email as a csv file Smile