Search code examples
phpcodeigniteremailemail-attachments

How to send Attachment through email library using own email library in CodeIgniter


If I code below method in my own library, it is sending email successfully but attachments are not shown in email i.e., attachments are not attached with email, also on enabling bcc, it throws Internal Server Error but if i use same code in my controller its working properly.

public function sendEmail($arrData=array()){
        if($arrData['TO_EMAIL']=='') return FALSE;
        $status = FALSE;
        $config = array(
            'protocol' => 'sendmail',
            'charset'   => 'utf-8',
            'wordwrap'  => TRUE,
            'mailtype'  => 'html'
        );
        $to = 'ra@gmail.com';
        $cci =& get_instance();
        $cci->load->library('email', $config);

        $cci->email->from($arrData['FROM_EMAIL'],           $arrData['FROM_NAME']);
        $cci->email->to($arrData['TO_EMAIL'], $arrData['TO_NAME']);
        if($arrData['CC_EMAIL'])
            $cci->email->cc($arrData['CC_EMAIL']);
        //$this->email->bcc('ravr@rediffmail.com');
        $cci->email->subject($arrData['SUBJECT']);
        $cci->email->message($arrData['MESSAGE']);
        if($arrData['FILES'])
            $cci->email->attach($arrData['FILES']);
        $status = $cci->email->send();
        return $status;
    }

Solution

  • Use the path helper

    $this->load->helper('path');
    $path = set_realpath('pathtouploads'); //your uploads folder
    

    then

    $this->email->attach($path . $file);