Search code examples
codeigniteremailcodeigniter-3email-attachments

send email with attachment in CodeIgniter


Pdf attachment through email in CodeIgniter Mysource code here

  $this->load->library('email', $config);
  $this->email->set_newline("\r\n");
  $this->email->from('test@gmail.com'); 
  $this->email->to($useremail);
  $this->email->cc('');
  $this->email->subject('pdf');
  $this->email->message($message);
  $this->email->attach('public_html/invoicepdf/171.pdf');
  $mailsucc =   $this->email->send();

I tried with this but didnt work

$this->email->attach('public_html/invoicepdf/171.pdf');

And i also replace path with URL.


Solution

  • You can send attach file using

    $this->email->attach($atch);

    method in codeigniter. in this below code i'm sending mail using SMTP with Attached File.

    its Working perfectly. You only need to specify base_url to define attachment file path

    Controller

    $email_config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'yourmail@gmail.com', // change it to yours
        'smtp_pass' => 'mypasswords', // change it to yours
        'mailtype'  => 'html',
        'charset'   => 'iso-8859-1'
    );
    
    // Defined Attachment file using variable
    $atch=base_url().'html/images/logo.png';
    
    $this->load->library('email', $email_config);
    
    $this->email->set_newline("\r\n");
    $this->email->from('test@gmail.com', 'Rudra');  // email From
    $this->email->to('mailtosend@gmail.com');       // eMail to send
    $this->email->subject('Mail with Attachment');  // Subject
    $this->email->message("This is mail with Attachment file using CodeIgniter.");  // Message
    $this->email->attach($atch);  // Attachment file defined using variable
    
    $maill=$this->email->send(); // send mail
    
    if($maill>0)
     {
      echo 'Email sent.';  // success
     }
     else
    {
     show_error($this->email->print_debugger());
    }