'Codeigniter Email Attachment: mail is sent without attachment

I am in a little bit of trouble sending mail with attachments in CodeIgniter. Here is my code:

        $to="[email protected]"
        $fileUrl=array(base_url()."assets/pdf/sample.pdf", base_url()."assets/pdf/sample1.pdf");
        $subject="TEST MAIL";
        $message="TEST MESSAGE";
        $this->load->library('email');
        $from = "[email protected];
        $config['wordwrap'] = TRUE;
        $config['mailtype']='html';
        $config['charset'] = 'utf-8';
        $config['priority'] = '2';
        $config['protocol'] = 'sendmail';
        $config['mailpath'] = '/usr/sbin/sendmail';
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");
        $this->email->from($from, 'Little Bloom');
        //$this->email->to();
        if($cc!=''){
            $this->email->cc('[email protected]');    
        }
        $this->email->bcc($to);

        $this->email->subject($subject);
        $this->email->message($message);
        if(!empty($fileUrl))
        {
            foreach($fileUrl as $key)
            {  
                $this->email->attach($key);
            }
        }
        return $this->email->send();

There are multiple pdf files so I am using an array to save the URL but when I send this to my email function these files are not attached to the mail.



Solution 1:[1]

if you print you array $fileUrl, you'll se at once where the problem is. Using e.g.: echo '<pre>'; print_r($fileUrl);die;

the urls are missing a forward slash between your_site and assets: http://your_siteassets/pdf/sample.pdf

you can fix this either with:

$fileUrl=array(base_url("assets/pdf/sample.pdf"), base_url("assets/pdf/sample1.pdf"));

or with

$fileUrl= array(base_url()."/assets/pdf/sample.pdf", base_url()."/assets/pdf/sample1.pdf");

docs url helper base_url()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1