2013-07-22 89 views
0

我有一个联系表单,当它被引用时,它发送一封电子邮件并将数据作为备份添加到数据库中。Codeigniter,html电子邮件发送但变量是'未定义'

当测试我的电子邮件时,我实际上通过(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

你缺少解析器。这里是你如何做到这一点:

在你的控制器,你处理电子邮件:

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> 

让我知道,如果有任何问题。

+0

工作就像一个魅力! Wow会记下从现在开始加载解析器库 - 谢谢! – user2212564

+0

如果你在你的代码中做了很多这样的事情,一次加载到你的autoload.php中,并且它用于所有文件。 – SasaT

+0

很高兴我能帮到你 – SasaT

相关问题