Welcome Guest, Not a member yet? Register   Sign In
help with multipart forms, attachments, and emails
#1

[eluser]intoitdaily[/eluser]
Hey all. I was wondering if this was possible.

I have a form that has a couple of input fields, an upload field, and a textbox. When someone submits the form, I would like to format an email and attach their uploaded file, then send the email. I'm having trouble figuring out how to do this in one step. I have a form that only uploads a file (i found it in the docs), and then another contact form that functions properly. but how would i get the multipart form input within the same contact form?
#2

[eluser]darkhouse[/eluser]
Have you looked at the email library?
#3

[eluser]intoitdaily[/eluser]
you don't understand. I can email and attach no problem... it's having a form that has input fields AND a file upload field, then when submitted, uploads the file and attaches it to an email, then sends it, all in the same step.

I know how to have a form with only input fields, then when submitted, sends an email

I know how to upload a single file to the server

I know how to attach a file to an email and send it.

But i don't know how to put all those together in one form.
#4

[eluser]gtech[/eluser]
load a view with a form and add as many inputs as you desire:

Code:
<form action="<?=site_url()?>/uploader/upload" enctype="multipart/form-data" method="post">
  <p>
    &lt;input type="text" name="emsg" size="40"&gt;
    Please specify a file:<br>
    &lt;input type="file" name="userfile" size="40"&gt;
  </p>
  <div>&lt;input type="submit" value="Send"&gt;&lt;/div>
&lt;/form&gt;

then in your uploader controller
Code:
function upload()
   {
     $config['upload_path'] = './uploads/';
     $config['allowed_types'] = 'txt|csv';
     $config['max_size']    = '16777215';

     $this->load->library('upload', $config);

     // call do_upload
     if ( ! $this->upload->do_upload())
     {
         // if not uploaded ok
         echo($this->upload->display_errors());
     } else {
         // ah ha its uploaded ok, lets get the upload data
         // so we can find the pathname
         $dat = $this->upload->data();
         $emsg = $this->input->post('emsg');
         $this->email->clear();

         $this->email->to('[email protected]');
         $this->email->from('[email protected]');
         $this->email->subject('Here is your info:');
         $this->email->message($emsg);
         $this->email->attach($dat['full_path']);
         $this->email->send();
     }  
   }

I havn't tested it.. but you get the idea.
#5

[eluser]gtech[/eluser]
IN addition to the code above.. when calling do_upload() with no paramter your file input in the form has to be named 'userfile'.

if you are using my example you need to create a directory called uploads in the root of your CI install, and dont forget to configure the email prefs.
#6

[eluser]intoitdaily[/eluser]
perfect buddy! Thanks! I'll test that soon and reply with the results.




Theme © iAndrew 2016 - Forum software by © MyBB