2013-07-22 85 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

相關問題