0

How can I attach a PDF file in a mail using CodeIgniter?

I received mail. But I didn't find any attachment in that.

I am using the following code:

public function sendmail()
{
    $this->load->library('email');

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);
    $this->upload->do_upload('attachment');
    $upload_data = $this->upload->data();

    $this->email->set_newline("\r\n");
    $this->email->set_crlf("\r\n");
    $this->email->from('sskwebtech@gmail.com');
    $this->email->to($this->input->post('to'));
    $this->email->subject($this->input->post('subject'));
    $this->email->message($this->input->post('message'));


    if ($this->email->send()) {
        echo "Mail Send";
        return true;
    } else {
        show_error($this->email->print_debugger());
    }
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • You can find answer to your quesion here: https://stackoverflow.com/questions/25416585/codeigniter-send-email-with-attach-file – slon Jun 09 '18 at 12:37
  • 1
    Possible duplicate of [Codeigniter send email with attach file](https://stackoverflow.com/questions/25416585/codeigniter-send-email-with-attach-file) – Nigel Ren Jun 09 '18 at 12:56

2 Answers2

2

Use $this->email->attach(); this Enables you to send an attachment

$this->email->attach('path_to_file');

You can also use a URL like this :

$this->email->attach('http://example.com/filename.pdf');

Should be like this :

 $pdf_file_path = FCPATH.'your path';

 $this->email->set_newline("\r\n");
 $this->email->set_crlf("\r\n");
 $this->email->from('sskwebtech@gmail.com'); 
 $this->email->to($this->input->post('to')); 
 $this->email->subject($this->input->post('subject'));
 $this->email->message($this->input->post('message'));

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

For more : https://www.codeigniter.com/user_guide/libraries/email.html#CI_Email::attach

Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

100% working correct code, Lengthy but USEFUL. NO Time-Waste, Enjoy!

//View

<?php echo form_open_multipart('Jobs_portal/job_apply')?>
<input type="file" 
name="userfile" accept="application/msword,application/pdf" 
required="">
<button type="submit">Submit</button>
</form>
//end view


//Controller <Jobs_portal>

public function job_apply()
{

            $q=$this->file_upload(); //calling function file_upload

            if ($q) {
            $file_attribute=$this->upload->data();
            $file_name=$file_attribute['file_name'];
            $file_path=$file_attribute['file_path'];

            //calling function attachment_mail to send pdf
            $this->attachment_mail($file_path,$file_name);
            }
 }



public function file_upload()
{
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'doc|pdf';
            $config['max_size']             = 9000;
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                $this->upload->display_errors();
                echo "Sorry! Apply again. Check Your Resume Format";
                return false;
            }
            else
            {
                echo "i'll reply you after check your Resume. Thanks!";
                return true;    
            }
   } 

 public function attachment_mail($file_path,$file_name)
 {
            $this->load->library('email');   
            $this->load->helper('path');
            $this->email->from('No-Reply@domain.com', 'Resume Receive');
            $this->email->to('reciever@gmail.com'); 
            $this->email->subject('Apply for Job ');
            $this->email->message('Sir I have attached my Resume for Job'); 

            $this->email->attach($file_path.file_$name);
            $this->email->send();
            echo $this->email->print_debugger();
}

//end