简体   繁体   English

使用来自CodeIgniter的表单中的输入来发送电子邮件

[英]Send Email Using input from form with CodeIgniter

Hello I'm having difficulties using the "email" library for CodeIgniter. 您好,我在使用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. 我应该提到,我是XodeIgniter和Web开发的新手,所以也许这是使我更容易理解的问题。 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'); $ this-> load-> library('email');

follow the below code for sending email through EMAIL CLASS 请按照以下代码通过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 : 有关codeigniter中电子邮件类别的更多详细信息,请访问以下链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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