title: How to save mysql result as csv in specific folder in codeigniter
body: <p>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.</p>
  
  <pre><code>public function retrieveEmail($s){
          //print_r($s);
          $config = Array(
                       'protocol' =&gt; 'smtp',
                       'smtp_host' =&gt; getenv('SMTP_HOST_'.getenv('ENV_TYPE')),
                      'smtp_port' =&gt; getenv('SMTP_PORT_'.getenv('ENV_TYPE')),
                      'smtp_user' =&gt; getenv('SMTP_USER_'.getenv('ENV_TYPE')),
                      'smtp_pass' =&gt; getenv('SMTP_PASS_'.getenv('ENV_TYPE')),
                      'smtp_timeout' =&gt; 5,
                       'mailtype' =&gt; 'html',
                       'charset' =&gt; 'iso-8859-1',
                       'wordwrap'  =&gt; TRUE,
                       'newline'   =&gt; "\r\n"
                  );
          $this-&gt;load-&gt;library('email', $config);
          //echo "Hi";
          $this-&gt;email-&gt;initialize($config);
          $this-&gt;email-&gt;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=&gt;$line){ 
               fputcsv($file,$line); 
          }
          // force_download($file, $header);
          //fclose($file); 
          //exit; 
          //print_r($s);
          //print_r($r);
          $this-&gt;email-&gt;from(getenv('SMTP_FROM_'.getenv('ENV_TYPE')), 'Serial Block Statement');
          $this-&gt;email-&gt;to('pacepdtpro@gmail.com');
          $this-&gt;email-&gt;bcc('pacepdtpro1@gmail.com');       
          $this-&gt;email-&gt;subject($subject);
          $this-&gt;email-&gt;message($econtent);
          $this-&gt;email-&gt;attach($file);       
              if($this-&gt;email-&gt;send()) {
  
              } else {
                  show_error($this-&gt;email-&gt;print_debugger());
              }
      }
  </code></pre>