Codeigniter,html电子邮件发送但变量“未定义”

时间:2013-07-22 16:26:58

标签: codeigniter email controllers

我有一个联系表单,当提交时,它会发送一封电子邮件并将数据作为备份添加到数据库中。

在测试我的电子邮件时,我实际上是通过(woo)获取html电子邮件,但是正在填写的变量不应该在视图中定义 - 我缺少什么?

控制器

表格验证----

如果错误这样做......

如果成功......

// set the response as successful
                $respond['result'] = 'true';
                $respond['success_message'] = $success_message;

// set field labels
                $data['message'] = $message;
                $data['first_name'] = $first_name;
                $data['email'] = $email;

                $html_email = $this->load->view('html_email', $data, true);

                // send email notification
                    $this->load->library('email');
                    $this->email->from('email-address', 'Email-address');
                    $this->email->to('email-address-here');                     
                    $this->email->subject('subject');
                    $this->email->message($html_email);         
                    $this->email->send();

                echo $this->email->print_debugger();

                // add contact message to the database
                $this->contact_model->insert_contact_message($curr_lang, $this->input->post('first_name'), $this->input->post('email'), $this->input->post('message'));

在我的html电子邮件中使用表格设置并将变量声明为:

<?=$first_name?>
<?=$email?>
<?=$message?>

我知道它正在通过,因为样式正在运作,但只是变量没有通过。

当我查看我的错误时,这是​​我从html电子邮件中得到的:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: message    

1 个答案:

答案 0 :(得分:1)

你缺少解析器。这是你如何做到的:

在您处理电子邮件的控制器中:

function send_email()
   $this->load->library('parser');
   $this->load->library('email');

   $data = array(
      'name'  =>  $this->input->post('name'),
      'email' =>  $this->input->post('email'),
      'message' => $this->input->post('message)
   );

   $body = $this->parser->parse('path_to/email_view_template', $data, true);

   //set from, to etc.
   $this->email->message($body);
   $this->email->send();
}

确保您的电子邮件配置文件设置为发送HTML电子邮件而不是纯文本。

然后在您的电子邮件模板中调用如下变量:

<p>You have just received email from {name}. You can contact {name} on {email}. {name} left a message saying: {message}</p>

如果有任何问题,请告诉我。