Welcome Guest, Not a member yet? Register   Sign In
Attaching a file, sending with Email class
#1

[eluser]Unknown[/eluser]
Hey all. I'm pretty frustrated at this point. I have spent way too long trying to figure something out, that I know must be simple!

I simply want to attach and send a file in an email, through a form. I am sure my code isn't the best, but here is the controller.

Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('email');
        $this->load->library('validation');
        
        $rules['name']    = "required";
        $rules['phone']    = "required|alpha_dash";
        $rules['email']    = "required|valid_email";        
        $this->validation->set_rules($rules);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        
        $fields['name'] = 'Name';
        $fields['organization'] = 'Organization';
        $fields['phone'] = 'Phone';
        $fields['email'] = 'Email';
        $fields['quantity'] = 'Quantity';
        $fields['message'] = 'Message';

        $this->validation->set_fields($fields);
        
        $service = "";
            
            $services = $this->input->post('services');
            for($i=0;$i<(count($services));$i++) {
                $service .= ($services[$i].", ");
                //echo $services[$i].", ";
            }
        
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $organization = $this->input->post('organization');
        $phone = $this->input->post('phone');
        $quantity = $this->input->post('quantity');
        $artwork = $this->input->post('artwork');
        $message = $this->input->post('message');
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('getQuoteForm');
        }
        else
        {
            $this->email->from($email, 'SoWhachaWant.com');
            $this->email->to('[email protected], [email protected]');
            $this->email->subject('Quote Request');
            $this->email->message("Name: ".$name."\r\nOrganization: ".$organization."\r\nPhone #: ".$phone."\r\nEmail Address: ".$email."\r\nQuantity Nedded: ".$quantity."\r\nServices Needed: ".$service."\r\nMessage: ".$message);
            if($artwork) {
                           $this->email->attach($artwork);
                           echo $artwork;
                        }

            $this->email->send();
            
            $this->load->view('formsuccess');
        }
    }
}
?&gt;

Before I added the attachment code, the form properly displayed validation error, and sent correctly.

Now, it will take a second, like it's loading the attachment, and most of the time displays a success message. If I don't attach a file, it actually shoots the email. If I attach a file it does not send. AND, I only got an error message once. It said I had improper parameters.

Any help? I would be most grateful.
#2

[eluser]Christopher Blankenship[/eluser]
Here goes: first is the form a multitype set and is the artwork field a type of file if you are attempting to have people upload the image to the server.

If yes then here is why you are having a problem. First you have to upload the file to the server, currently in your code you are treating it as a text field. Use the upload class to help with that then artwork would need to be the full path to the file on the server to attach it.

Hope this helps you plan your course of action.




Theme © iAndrew 2016 - Forum software by © MyBB