0

I want to send an email with csv attachment which comes from MySQL result. My code was directly downloading the file when I call the URL. But no attachment was included in the email. I want to send as an attachment in an email.

public function retrieveEmail($s){
        //print_r($s);
        $config = Array(
                     'protocol' => 'smtp',
                     'smtp_host' => getenv('SMTP_HOST_'.getenv('ENV_TYPE')),
                    'smtp_port' => getenv('SMTP_PORT_'.getenv('ENV_TYPE')),
                    'smtp_user' => getenv('SMTP_USER_'.getenv('ENV_TYPE')),
                    'smtp_pass' => getenv('SMTP_PASS_'.getenv('ENV_TYPE')),
                    'smtp_timeout' => 5,
                     'mailtype' => 'html',
                     'charset' => 'iso-8859-1',
                     'wordwrap'  => TRUE,
                     'newline'   => "\r\n"
                );
        $this->load->library('email', $config);
        //echo "Hi";
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");
        $subject = "Serials blocked report";
        $econtent = "Testing purpose only";
        $filename = "serialblockingreport_".date('Y-m-d').".csv";
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Pragma: no-cache");
        header("Expires: 0");
        $file = fopen("php://output","r");
        $header = array("id","serial","itemname","expire_date","isactive","blockedat"); 
        fputcsv($file, $header);
        foreach ($s as $key=>$line){ 
             fputcsv($file,$line); 
        }
        // force_download($file, $header);
        //fclose($file); 
        //exit; 
        //print_r($s);
        //print_r($r);
        $this->email->from(getenv('SMTP_FROM_'.getenv('ENV_TYPE')), 'Serial Block Statement');
        $this->email->to('pacepdtpro@gmail.com');
        $this->email->bcc('pacepdtpro1@gmail.com');       
        $this->email->subject($subject);
        $this->email->message($econtent);
        $this->email->attach($file);       
            if($this->email->send()) {

            } else {
                show_error($this->email->print_debugger());
            }
    }
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • use csv library function to load file – Jacky Aug 03 '19 at 10:25
  • https://stackoverflow.com/questions/57217875/how-to-use-csv-import-library-in-codeigniter see this link for reference – Jacky Aug 03 '19 at 10:26
  • Hi Jack iam using all loaders. the code is working. but as per my research i need to store/save the generated csv in some folder. I have strucked at storing the generated csv file in system – Nazeer Shaik Aug 03 '19 at 11:08

1 Answers1

0

I suggest to save your file in temporary folder, send it and destroy it if the mail was sent successfully: example:

$csv_content = array_to_csv($output);
$file = '/tmp/'.time().".csv";
if ( ! write_file($filename, $csv))
{
  //log the details of failed
} else {
  //sent the email with attachement
}