繁体   English   中英

使用表单在Codeigniter中发送电子邮件类

[英]Email Class in Codeigniter Using Form

免责声明:我是Web开发新手。

场景:我已经创建了一个联系表单,并且试图将输入传递给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(); ?>

好吧,您几乎没有在这里定义任何变量:

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{}部分中(因为您忽略了它,所以我无法告知)

暂无
暂无

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

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