Codeigniter send email with attach file

前端 未结 8 1540
走了就别回头了
走了就别回头了 2020-11-30 05:15

I am trying to send email on codeigniter with attach file.

I always receive email successfully. However , I never receive with attach file. Below is code and highly

8条回答
  •  既然无缘
    2020-11-30 05:48

    Here Is Full Source Code

        $validation_rules = array(
            array('field' => 'name', 'rules' => COMMON_RULES),
            array('field' => 'email', 'rules' => COMMON_RULES),
            array('field' => 'message', 'rules' => COMMON_RULES),
        );
    
        $this->validation_errors($validation_rules);
    
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $message = $this->input->post('message');
    
        $this->load->library('email');
    
        //upload file
        $attachment_file = "";
        if (!empty($_FILES) && isset($_FILES["attachment_file"])) {
    
            $image_name = $_FILES["attachment_file"]['name'];
    
            $ext = pathinfo($image_name, PATHINFO_EXTENSION);
    
            $new_name = time() . '_' . $this->get_random_string();
    
            $config['file_name'] = $new_name . $ext;
            $config['upload_path'] = "uploads/email/";
            $config['allowed_types'] = "*";
    
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
    
            if ($this->upload->do_upload('attachment_file')) {
    
                $finfo = $this->upload->data();
                $attachment_file = base_url() . 'uploads/email/' . $finfo['file_name'];
            } else {
    
                $error = $this->upload->display_errors();
                $this->msg = $error;
                $this->_sendResponse(5);
            }
        }
    
        $this->email->from($email, "$name")
                ->to("example@gmail.com")
                ->subject("FeedBack From $name")
                ->message($message)
                ->attach($attachment_file);
    
        if ($this->email->send()) {
    
            // temp pass updated.
            $this->msg = "Email send successfully.";
            $this->_sendResponse(1);
        } else {
    
            $this->msg = "Internal server error/Something went wrong.";
            $this->_sendResponse(0);
        }
    

提交回复
热议问题