imlouisrussell
imlouisrussell

Reputation: 946

Email Class in Codeigniter Using Form

Disclaimer: I'm new to web develoment.

Scenario: I've created a contact form, and I'm trying to pass the input into the email class functions used in CodeIgniter. I am receiving undefined variable errors when the form is submitted. Email library is autoloaded for reference. It's late, and I may be overlooking something easy, but it doesn't hurt to post. Thanks a lot for all of your help!

Controller:

public function index()
{   
    //form validation
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
    $this->form_validation->set_rules('subject', 'Subject', 'required');
    $this->form_validation->set_rules('message', 'Message', 'required');

    $this->data['email'] = array(
            'name' => 'email',
            'id' => 'email',
            'type' => 'text',
            'value' => $this->form_validation->set_value('email'),
    );
    $this->data['subject'] = array(
            'name' => 'subject',
            'id' => 'subject',
            'type' => 'text',
            'value' => $this->form_validation->set_value('subject'),
    );
    $this->data['message'] = array(
            'name' => 'message',
            'id' => 'message',
            'type' => 'text',
            'value' => $this->form_validation->set_value('message'),
    );

    if ($this->form_validation->run() == true)
    {   
        $this->email->from($email);
        $this->email->to('[email protected]'); 

        $this->email->subject($subject);
        $this->email->message($message);    

        $this->email->send();

        redirect('contact/success');
    }

    else
    {

        $this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message');

        $title['title'] = 'Contact';

        $this->load->view('public/head_view', $title);
        $this->load->view('public/header_view');
        $this->load->view('public/contact_view', $this->data);
        $this->load->view('public/footer_view');
    }

View:

<div id="infoMessage"><?php echo $error_message;?></div>

            <?php $attritubes = array('class' => 'nice'); ?>
            <?php echo form_open('contact'); ?>
                <p>Email Address:<br />
                    <?php echo form_input($email); ?>
                </p>
                <p>Subject:<br />
                    <?php echo form_input($subject); ?> 
                </p>
                <p>Message:<br />
                    <?php echo form_textarea($message); ?>
                </p>
                <p><?php echo form_submit('submit', 'Submit'); ?></p>
            <?php echo form_close(); ?>

Upvotes: 0

Views: 2324

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25445

Well, you barely define any variable here:

if ($this->form_validation->run() == true)
    {   
        $this->email->from($email);
        $this->email->to('[email protected]'); 

        $this->email->subject($subject);
        $this->email->message($message);    

        $this->email->send();

        redirect('contact/success');
    }

Where do $email, $subject, $message come from? You might want to use something like (but I suggest you do better :))

$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');

Also, make sure you've loaded the email library before calling it, and that you load the view in your else{} part (since you omitted it I cannot tell)

Upvotes: 1

Related Questions