Welcome Guest, Not a member yet? Register   Sign In
Export data from table and email it.

[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.

// controller: export
function export() {
    $csv = $this->data_model->exportCsv();
    $file_name = "spreadsheet.csv";

// model: exportCsv
function exportCsv() {
    $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->message('Here is the list');    
    $this->email->attach($file); // want to attach the csv file here


    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.

You need to pass the full server path to the file you want to attach, not just the filename.

[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.

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.

[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

Theme © iAndrew 2016 - Forum software by © MyBB