【问题标题】:Variables inserted into email message from database从数据库插入电子邮件的变量
【发布时间】:2014-03-25 18:14:25
【问题描述】:

所以我在 codeigniter 中发送一封电子邮件,其中消息来自数据库。

我想要做的是将发布的变量放入数据库中的 html 格式的电子邮件中。

为了发送电子邮件,我的控制器中有以下内容:

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

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

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

    $this->email->from('info@candykingdom.org', '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>
...

我正在尝试将 &lt;p&gt;Hi&lt;/p&gt; 行变为:&lt;p&gt;Hi John,&lt;/p&gt; 我已尝试将该行更改为以下内容:

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

还有:

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

但在已完成并已发送的电子邮件中,它的显示方式与上面的电子邮件一样。无需用实际变量替换 php。

所以我要问的是,我在存储的电子邮件中输入什么来使 php 代码用实际变量替换 php?

例如,让我们将John 用作$this-&gt;input-&gt;post('billingFName');

只是一个想法

也许使用模板库会更好地实现这一点?像这样: https://github.com/philsturgeon/codeigniter-template

【问题讨论】:

    标签: php codeigniter email


    【解决方案1】:

    我见过的一种常见方法是使用您通过 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>
    

    然后,当您从数据库中获取 #USERNAME# 时,您可以通过 str_replace 更改它:

    $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 了解更多信息。

    【讨论】:

    • 我会尽快批准,但你能提供一个数组的例子吗?即使只有两个变量?
    • 查看示例链接,来自php.net,XD
    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多