Pratirup Mukherjee
Pratirup Mukherjee

Reputation: 39

Codeigniter 3 email attachment from form

I have a consultancy controller where users can upload their resume

public function consultancy($page = 'consultancy') {
  if (! file_exists(APPPATH.'views/pages'.$page.'.php'))
  {
    show_404();
  }
  $data['title'] = ucfirst($page);


  $this->load->view('templates/header', $data);
  $this->load->view('pages/'.$page, $data);
  $this->load->view('templates/footer', $data);
}

the view

<form enctype="multipart/form-data" style="text-align:left;font-size:12px;" action="<?php echo base_url(); ?>postconEmail/"method="POST">

               Name <input class="form-control" id="id_name" name="name" type="text" required />
               Phone <input class="form-control" id="id_phone" name="phone" type="text" required />
               From email <input class="form-control" id="id_from_email" name="from_email" type="email" required />
               Subject <input class="form-control" id="id_subject" name="subject" type="text" required />
               Message <textarea class="form-control" cols="40" id="id_message" name="message" rows="10" required></textarea>
            <br>
            <label for="id_resume" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Resume Upload</label>
            <input class="custom-file-upload" id="filename" type="text" size="35" placeholder="Upload Your Resume"/>
            <input class="btn btn-primary form-control test" id="id_resume" name="resume" type="file" />
               <div class="form-group">
            <button style="float:right;display: inline;" type="submit" class="btn btn-primary">
                <span class="glyphicon glyphicon-star"></span> Submit
              </button> </div>
        </form>

and a post email controller form where the send mail is done

public function postconEmail(){

  $data = $this->input->post();
  $this->load->library('email');
  $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'mail.example.com';
    $config['smtp_user'] = '[email protected]';
    $config['smtp_pass'] = 'password';
    $config['smtp_port'] = 'xxx';
    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $this->email->from($data['from_email']);
    $this->email->to('[email protected]');
    $this->email->subject($data['subject']);
    $this->email->message($data['message']);
    $this->email->attach($data['resume']);
    if ($this->email->send()) {
      $this->session->set_flashdata('success','Email Sent');
      redirect(base_url());
    } else{
      show_error($this->email->print_debugger());
    }
  }

The mail is going through but there is no file attached to the mail i am receiving.

I looked up google but am not able to get my hands on any posts regarding this matter.

And over that I am new to php and Codeigniter 3 any kind of help is appreciated

Upvotes: 0

Views: 3773

Answers (1)

ImBhavin95
ImBhavin95

Reputation: 1527

You have to add attachment file path in attachment argument

Replace Your post email controller as per below

public function postconEmail(){
    $data = $this->input->post();
    $this->load->library('email');
    $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'mail.example.com';
    $config['smtp_user'] = '[email protected]';
    $config['smtp_pass'] = 'password';
    $config['smtp_port'] = 'xxx';
    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $this->email->from($data['from_email']);
    $this->email->to('[email protected]');
    $this->email->subject($data['subject']);
    $this->email->message($data['message']);

    $resume_tmp_path = $_FILES['resume']['tmp_name'].'/'.$_FILES['resume']['name'];

    $this->email->attach($resume_tmp_path);
    if ($this->email->send()) {
      $this->session->set_flashdata('success','Email Sent');
      redirect(base_url());
    } else{
      show_error($this->email->print_debugger());
    }
  }

If This is not working then you refer this question it says You cannot attach file without upload your Server so first you have to upload file in your server and then pass $this->email->attach(youy file path); so your code definetely work.

Refer This Question : https://stackoverflow.com/a/3628203/3377733

Upvotes: 1

Related Questions