2014-03-25 35 views
0

因此,我在codeigniter中發送郵件來自數據庫的電子郵件。插入到數據庫的電子郵件中的變量

我想要做的是將發佈的變量放入數據庫中的html格式的電子郵件中。

對於發送我在我的控制器以下電子郵件:

$this->load->library('email'); 

    $this->load->model('cms'); 

    $message = $this->cms->Order_Email(); 

    $this->email->from('[email protected]', 'Candy Kingdom'); 
    $this->email->to($this->input->post('billingEmail')); 

    $this->email->subject('Order Confirmation'); 
    $this->email->message($message->content); 

    $this->email->send(); 

現在,來自數據庫我的電子郵件的部分是:

<td> 
    <p>Hi</p> 
    <p>Sometimes all you want is to send a simple HTML email with a basic design.</p> 
    <h1>Really simple HTML email template</h1> 
... 

我試圖讓<p>Hi</p>行變成:<p>Hi John,</p>我試圖將該行更改爲以下內容:

<p>Hi <?php echo $this->input->post('billingFname'); ?>,</p> 

還有:

<p>Hi '.$this->input->post('billingFName").',</p> 

但它顯示就像在電子郵件上面的完成併發送電子郵件。沒有用實際變量替換php。

所以我問的是,我在存儲的電子郵件中輸入什麼信息才能使php代碼替換實際變量中的php?

爲例子,讓我們使用John$this->input->post('billingFName');

只是一個想法

也許這將與模板庫可以更好地實現?像這樣: https://github.com/philsturgeon/codeigniter-template

回答

0

一種常見的方法,我用你瓦爾通過str_replace函數代替看出:

電子郵件的HTML有一些你知道的替代,爲#USERNAME#瓦爾。在你的數據庫,你存儲

<td> 
    <p>Hi #USERNAME#</p> 
    <p>Sometimes all you want is to send a simple HTML email with a basic design.</p> 
    <h1>Really simple HTML email template</h1> 

然後,通過str_replace函數更改#USERNAME#當你從數據庫中得到它:

$message->content = str_replace('#USERNAME#', $var_with_username, $message->content); 

你甚至可以使用數組中的str_replace函數,以替補多許多如果你想要增值,請查詢http://www.php.net/manual/en/function.str-replace.php#refsect1-function.str-replace-examples瞭解更多信息。

+0

我會盡快批准,但可以提供一個陣列的例子嗎?即使只有兩個變種? – user3367639

+0

檢查鏈接的例子,它來自php.net,XD – Chococroc

相關問題