1

I have check similar links on stack overflow about this. But i'm unable to find any solution for this.

Link's i have checked

  1. Codeigniter send email with attach file
  2. Code Igniter -> attach email
  3. codeigniter send pdf file as email attachment

Problem

When is send mail, I gets the mail. with all details. but attachment is not working.

Try

I echo the file name, its prints the file name from controller

Code

$upcon['upload_path'] = 'cv_uploads/';
$upcon['max_size'] = '5120';
$upcon['allowed_types'] = 'pdf|doc|txt|zip|rtf|docx';
$this->load->library('upload', $upcon);
if(!$this->upload->do_upload('file')){
    echo $this->upload->display_errors();
}
else{
    $up_data = $this->upload->data();

    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['mailtype']  = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;

    $this->load->library('email',$config);
    $this->email->set_newline("\r\n");

    $name = $this->input->post('name');
    $email = $this->input->post('email');
    $address = $this->input->post('address');
    $residence = $this->input->post('residence');
    $office = $this->input->post('office');
    $mobile = $this->input->post('mobile');
    $position = $this->input->post('position');
    $comment = $this->input->post('comment');

    $file_name= '/cv_uploads/'.$up_data['file_name'];

    $this->email->from('xx@xxx.com', 'site name'); 
    $this->email->to('yy@yyyy.com');
    $this->email->attach($file_name);
    $this->email->subject('Subject');
    $this->email->message("Message ");
    if(!$this->email->send())
    {
        $data['sent_mail'] = false;
    }
    else{
        $data['sent_mail'] = "true";
    }

    print_r($file_name);
}

I'm receiving mail without the attachment. Any idea??

Community
  • 1
  • 1
Stack User
  • 299
  • 1
  • 3
  • 12

1 Answers1

2

To send your attachment use absolute path instead of relative and make sure your attachment is accessable for public.

absolute path example

c:/test/test/cv_uploads/cv.doc

in your code try with

$file_name= FCPATH.'/cv_uploads/'.$up_data['file_name'];

or

$file_name= $_SERVER["DOCUMENT_ROOT"].'/cv_uploads/'.$up_data['file_name'];

and attach file with this function

$this->email->attach($file_name);
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31