Search code examples
codeignitercodeigniter-3codeigniter-2

Having trouble about how to attach file in email using codeigniter


This is my view form. Here is where the user will send and email with attached file/s.(This is a shortcut for my view form. Problem is the attached file.)

<form action="<?php echo base_url(); ?>hr_mailer/create" method="POST">
                    <table class="table" style="margin-left: 15px; text-align: left; width: 870px;">
            <tr>
    <td style="font-weight: bold;">Attachment/s: </td>
    <td colspan="4"><input type="file" name="upload" id="file" /><label for="file">Choose a file</label>
                        </tr>
                        <tr>
                            <td colspan="4"><input type="submit" name="submit" value="Send Email" class="button1" style="float: right;" onclick="return confirm('You are about send the email, are you sure?')"> 
                                <button type="reset" value="Reset" class="button1" style="float: right; margin-right: 5px;">Clear</button></td>
                        </tr>
                    </table>            
                </form>

This is the function that will catch the form action

public function create() {
            if ($this->input->post('submit')) {

                  $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('upload');
                  $upload_data = $this->upload->data();



                  $form = array(
                    'sender' => $this->input->post('hr'),
                    'date' => $this->input->post('email_date')
                );


                $to = $this->input->post('to');
                $cc = $this->input->post('cc');
                $bcc = $this->input->post('bcc');
                $message = $this->input->post('message');
                $subj = $this->input->post('subject');            


                $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
                $this->mailer->insert($form);                  

                 $this->session->set_flashdata('msg', 'Email Sent!');
                    redirect(base_url().'hr_mailer/');


            } else {
                $data['uid'] = $this->user->get_uid();
                $content = $this->load->view("hr_mailer/create", $data, true);
                $this->render("", "", "", $content);
            }
        }

This is the other function in my controller.

private function notify($to, $cc, $bcc, $message, $subj,$upload_data) {
        $this->user = new Employee($this->session->userdata('username'));
        $var = "cebuhr@1and1.com";
        $name = "HR Announcement";

//        $config['charset'] = 'iso-8859-1';
//        $config['wordwrap'] = FALSE;
//        $config['mailtype'] = 'html';

        $this->load->library('email');
//        $this->email->initialize($config);

        //for testing purposes only. Must not be pushed to production
         $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'email',
            'smtp_pass' => 'password',
            'mailtype' => 'html',
            'charset' => 'utf-8'
        );  

        $this->email->initialize($config);
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");

        $this->email->from($var, $name);
        $this->email->to($to);
        $this->email->cc($cc);
        $this->email->bcc($bcc);

        $this->email->subject($subj);
        $this->email->message($message);
        $this->email->attach($upload_data['full_path']);

        if($this->email->send()){
            echo 'SUCCESS';
        } else {
            show_error($this->email->print_debugger());
        }
         return true;
    }

I'm fairly new to CI and has trouble with some of it. I came from other framework. In my research, the variable to be put in the attach() part is the full path name of the uploaded file and here is where i find it hard.


Solution

  • in your function create ...

    public function create() {
                if ($this->input->post('submit')) {
    
                      $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('upload');
                      $upload_data = $this->upload->data();
                      $file_path = $config['upload_path'].$upload_data[file_name]; // get path
    
    
                      $form = array(
                        'sender' => $this->input->post('hr'),
                        'date' => $this->input->post('email_date')
                    );
    
    
                    $to = $this->input->post('to');
                    $cc = $this->input->post('cc');
                    $bcc = $this->input->post('bcc');
                    $message = $this->input->post('message');
                    $subj = $this->input->post('subject');            
    
    
                    // $this->notify($to, $cc, $bcc, $message, $subj,$upload_data );
                    $this->notify($to, $cc, $bcc, $message, $subj,$file_path );
                    // pass the generated path.. 
                    $this->mailer->insert($form);                  
    
                     $this->session->set_flashdata('msg', 'Email Sent!');
                        redirect(base_url().'hr_mailer/');
    
    
                } else {
                    $data['uid'] = $this->user->get_uid();
                    $content = $this->load->view("hr_mailer/create", $data, true);
                    $this->render("", "", "", $content);
                }
            }
    

    and then change the last parameter and attach var in notify function

    private function notify($to, $cc, $bcc, $message, $subj,$file_path) {
            $this->user = new Employee($this->session->userdata('username'));
            $var = "cebuhr@1and1.com";
            $name = "HR Announcement";
    
    //        $config['charset'] = 'iso-8859-1';
    //        $config['wordwrap'] = FALSE;
    //        $config['mailtype'] = 'html';
    
            $this->load->library('email');
    //        $this->email->initialize($config);
    
            //for testing purposes only. Must not be pushed to production
             $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => 'email',
                'smtp_pass' => 'password',
                'mailtype' => 'html',
                'charset' => 'utf-8'
            );  
    
            $this->email->initialize($config);
            $this->email->set_mailtype("html");
            $this->email->set_newline("\r\n");
    
            $this->email->from($var, $name);
            $this->email->to($to);
            $this->email->cc($cc);
            $this->email->bcc($bcc);
    
            $this->email->subject($subj);
            $this->email->message($message);
            $this->email->attach($file_path);
    
            if($this->email->send()){
                echo 'SUCCESS';
            } else {
                echo "<pre>";
                print_r($this->email->print_debugger());
                EXIT;
            }
             return true;
        }
    

    replace your code with this ... you are not capturing the path properly and you didnt capture debugger in any variable.. it went out before it prints.. at create function ... your location get redirect .. you dont have any condition for notify function ..