Codeigniter中的电子邮件类使用表单

时间:2011-11-22 08:51:28

标签: php forms email codeigniter

免责声明:我是网络开发的新手。

场景:我创建了一个联系表单,我正在尝试将输入传递给CodeIgniter中使用的电子邮件类函数。提交表单时,我收到未定义的变量错误。电子邮件库自动加载以供参考。现在已经很晚了,我可能会忽略一些简单的东西,但是发布它并没有什么坏处。非常感谢您的帮助!

控制器:

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('support@example.com'); 

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

查看:

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

1 个答案:

答案 0 :(得分:1)

嗯,你几乎没有在这里定义任何变量:

if ($this->form_validation->run() == true)
    {   
        $this->email->from($email);
        $this->email->to('support@example.com'); 

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

        $this->email->send();

        redirect('contact/success');
    }

$email$subject$message来自哪里? 你可能想要使用类似的东西(但我建议你做得更好:))

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

此外,请确保在调用之前已加载电子邮件库,并在else{}部分加载视图(因为您省略了我无法分辨的信息)