使用CodeIgniter从表单输入发送电子邮件


Send Email Using input from form with CodeIgniter

你好,我在使用CodeIgniter的"email"库时遇到了困难。我正在创建一个联系表单,我想抓住用户输入的信息,并将其用于电子邮件,但我没有成功。是的,我已经搜索了整个网站,并尝试了各种实现提供,但我没有成功地将它们纳入我的代码。我应该提到的是,我是XodeIgniter和网页开发的新手,所以也许这是一个让我更容易理解的问题。这是控制器和视图

控制器:

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);
    }
}

视图:

<?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();
?>

任何帮助都将是感激的,或者可能在前面陈述的其他实现的更清晰的解释。谢谢。

在构造函数中加载库:

$ this ->加载>图书馆(电子邮件);

按照下面的代码通过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
    }

有关codeigniter中email类的更多详细信息,请访问以下链接:

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