Welcome Guest, Not a member yet? Register   Sign In
file upload correctly, but unable to attach that file to an email, using CI's email library
#1

[eluser]Brad K Morse[/eluser]
Using form, file, and email libraries:

controller

Code:
<?php
class Work_check extends CI_Controller {
  function index() {
    $this->form_validation->set_rules('userfile', '', '');
    $this->form_validation->set_error_delimiters('<em>','</em>');
    
    if ($this->form_validation->run()) {
      
      $config['upload_path'] = 'uploads/';
          $config['allowed_types'] = 'gif|jpg|png|doc|docx|zip|xls|xlsx|ppt|pptx|pdf';
          $config['max_size']    = '100';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';

          $this->load->library('upload', $config);
          
          if($this->upload->do_upload()) {
            $data = array('upload_data' => $this->upload->data());
          }
      
      // validates, lets send some stuff
      $this->_emailWork(
              $this->input->post('email'),
              $this->input->post('name'),
              $this->input->post('description'),
              $this->input->post('userfile')
            );
    }
    $this->load->view('library/work-check-form-view');
  }
  
  function _emailWork($email, $name, $description, $userfile) {
    $this->email->from($email, $name);
    $this->email->to('[email protected]', 'Larry Jones');
    $this->email->subject('Work Check');
    $this->email->message($description);    
    $this->email->attach('uploads/'.$userfile);
    $this->email->send();

    echo $this->email->print_debugger();
    
  }
}
?&gt;

first line of debugger prints: Your message has been successfully sent using the following protocol: mail

I receive the email, but it does not attach the file, screenshot of what it attaches: http://cl.ly/5AVF

The file uploads properly to the uploads directory, but I am doing something wrong with capturing the filename of the file uploaded.

If I hardcode the file-path & file-name into the $this->email->attach('uploads/image.png'); - I receive the file correctly in the email.

Any help is appreciated.
#2

[eluser]erikstraub[/eluser]
It should be something like this:

[quote author="bkmorse" date="1299893744"]
Code:
&lt;?php
class Work_check extends CI_Controller {
  function index() {
    $this->form_validation->set_rules('userfile', '', '');
    $this->form_validation->set_error_delimiters('<em>','</em>');
    
    if ($this->form_validation->run()) {
      
      $config['upload_path'] = 'uploads/';
          $config['allowed_types'] = 'gif|jpg|png|doc|docx|zip|xls|xlsx|ppt|pptx|pdf';
          $config['max_size']    = '100';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';

          $this->load->library('upload', $config);
          
          if($this->upload->do_upload()) {
            $data = array('upload_data' => $this->upload->data());
          }
      
      // validates, lets send some stuff
      $this->_emailWork(
              $this->input->post('email'),
              $this->input->post('name'),
              $this->input->post('description'),
              $data['upload_data']['full_path']
            );
    }
    $this->load->view('library/work-check-form-view');
  }
  
  function _emailWork($email, $name, $description, $userfile) {
    $this->email->from($email, $name);
    $this->email->to('[email protected]', 'Larry Jones');
    $this->email->subject('Work Check');
    $this->email->message($description);    
    $this->email->attach($userfile);
    $this->email->send();

    echo $this->email->print_debugger();
    
  }
}
?&gt;
[/quote]

I would only use it this way if you were always going to be adding an attachment. Otherwise, there should be a different way of sending the email without an attachment if there is no upload.
#3

[eluser]Brad K Morse[/eluser]
Resolved:

had to grab from the upload array $this->upload->data();

Code:
$file_uploaded = $this->upload->data();
$this->email->attach($file_uploaded['full_path']);




Theme © iAndrew 2016 - Forum software by © MyBB