title: How to download and attach file when sending email in Codeigniter?
body: <p><a href="https://i.stack.imgur.com/cJCnr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cJCnr.png" alt="enter image description here"></a></p>
  
  <p>-I want to click the download button and download this bill as pdf as well as I want to attach this pdf in an email.</p>
  
  <p>I use this code- </p>
  
  <pre><code>public function details_pdf($id) {
      $this-&gt;load-&gt;model('user_model');
      $this-&gt;load-&gt;helper('download');
  
      $data['invoice_detls'] = $this-&gt;user_model-&gt;front_get_invoice_details(['front_orders_id'=&gt;$id]);
      $data['items'] = $this-&gt;user_model-&gt;front_get_ordered_items(['front_dish_invoice_id' =&gt; $id]);
  
      //$data['abstract_message'] = $this-&gt;user_model-&gt;get_abstract_message(['abstract_id' =&gt; $id]);
      //$data['user_details'] = $this-&gt;user_model-&gt;get_user(['id' =&gt; $data['abstract_message'][0]['user_id']]);
  
      //$data['user_details_member'] = $this-&gt;user_model-&gt;get_profile_details(['members.userid' =&gt; $data['abstract_message'][0]['user_id']]);
      //print_r($data['user_details']);die();
  
      $customer_name =  $data['invoice_detls'][0]['customer_name'];
      $customer_phone =  $data['invoice_detls'][0]['customer_phone'];
  
      //load the view and saved it into $html variable
      $html = $this-&gt;load-&gt;view('front_billing_view', $data, true);
      $time = time();
  
  
      //this the the PDF filename that user will get to download
      $pdfFilePath = "$customer_name.'_'.$customer_phone.'_'.$time.pdf";
  
      $this-&gt;user_model-&gt;update_order(['front_orders_id' =&gt; $id],['bill_pdf'=&gt;$pdfFilePath]);
  
      //load mPDF library
      $this-&gt;load-&gt;library('m_pdf');
  
      //generate the PDF from the given html
      $this-&gt;m_pdf-&gt;pdf-&gt;WriteHTML($html);
  
      //download it.
      $this-&gt;m_pdf-&gt;pdf-&gt;Output($pdfFilePath, "D");
  
      /*--------------------Mail function Start-----------------------*/
          $order_fetch = $this-&gt;user_model-&gt;front_get_invoice_details(['front_orders_id' =&gt; $id]);
  
          $email = $order_fetch[0]['customer_email'];
          $pdf_name = $order_fetch[0]['bill_pdf'];
              $config = Array(
                 'protocol' =&gt; 'smtp',
                 'smtp_host' =&gt; 'ssl://smtp.googlemail.com',
                 'smtp_port' =&gt; 465,
                 'smtp_user' =&gt; 'noreply@wsdev.in',
                 'smtp_pass' =&gt; '%KidFlash!2#4%',
                 'mailtype' =&gt; 'text/html',
                 'charset' =&gt; 'iso-8859-1',
                 'charset' =&gt; 'utf-8'
              );
              $this-&gt;load-&gt;library('email');
              $this-&gt;email-&gt;set_mailtype("html");
              $this-&gt;email-&gt;set_newline("\r\n");
              $from_email = 'noreply@wsdev.in';
              $this-&gt;email-&gt;from($from_email);
              $this-&gt;email-&gt;to($email);
              $this-&gt;email-&gt;subject('Order Delivered');
              $this-&gt;email-&gt;message('Your Order Successfully Delivered.');                       
              $this-&gt;email-&gt;attach($pdfFilePath);
              $this-&gt;email-&gt;send();
  
      /*--------------------Mail Function End-----------------------*/ 
  
  
  }
  </code></pre>
  
  <p>This code only downloads the pdf but I can not send the download file as an email for clicking the download button because of this file store into the desktop not in the project folder. how to solve this problem?</p>