简体   繁体   中英

Send Email Using input from form with CodeIgniter

Hello I'm having difficulties using the "email" library for CodeIgniter. I'm creating a contact form and I want to grab the information the user inputs and use it for the email but I am having no success. Yes I've searched through the site and have tried the various implementations provided but I've had no success in incorporating them into my code. I should mention that I'm new to XodeIgniter and web development so maybe it's a matter of making it easier to understand for me. Here is my controller and view.

Controller:

public function index()
{
    $data['title'] = 'Contact Us';
    $this->load->view('templates/header', $data);
    $this->load->view('templates/navbar', $data);
    $this->load->view('contact_us', $data);
    $this->load->view('templates/footer', $data);
}

public function email()
{
    $this->load->library('email');

    $this->email->from($this->input->post('email'),$this->input->post('name'));
    $this->email->to('orlandoe91@gmail.com');
    //$this->email->cc('another@example.com');
    //$this->email->bcc('them@example.com');
    $this->email->subject($this->input->post('subject'));
    $this->email->message($this->input->post('comments'));

    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;

    $this->email->initialize($config);
    $this->email->send();

    if ( ! $this->email->send())
    {
        $this->email->print_debugger();
    }
    else
    {
        $data['title'] = 'Contact confirmation';

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

View:

<?php
echo form_open('index.php/contact_us/submit', 'id=contact_us');
echo form_fieldset('Contact Us');
echo form_label('Name', 'name');
echo form_input('name', '');
echo br(1);
echo form_label('Email', 'email');
echo form_input('email', '');
echo br(1);
echo form_label('Subject', 'subject');
echo form_input('subject', '');
echo br(1);
echo form_label('Comments', 'comments');
echo form_textarea('comments','','id=comments');
echo br(1);
echo form_fieldset_close();
echo form_submit('submit_comment', 'Submit Comment');
echo form_close();
?>

Any help would be appreciated or maybe at stated earlier a clearer explanation of the other implementations. Thanks.

Load the library in the constructor :

$this->load->library('email');

follow the below code for sending email through EMAIL CLASS

    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';

    $this->email->initialize($config);
    $this->email->from($_POST['user_email_id'], $_POST['user_name']);
    $this->email->to('enter_your_email_address');

    $this->email->subject($_POST['your_custom_subject_subject']);
    $this->email->message("Enter_your_custom_message_here"); 

    if($this->email->send()){
        //email send successfully do something
    }else{
        //email not sent do something else
    }

For more details regarding email class in codeigniter please visit the below link :

http://ellislab.com/codeigniter/user-guide/libraries/email.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM