dongtang7347 2014-04-24 22:03
浏览 45
已采纳

将PHP变量注入XML电子邮件模板

How can I inject PHP variables into my XML email templates?

My templates are structured like this:

<Template id="1" name="Registration">
    <![CDATA[
        <p>Dear $FullName,</p>
        <p>We would like to welcome you to oursite.  Please find your activation link below, <br />
        if you cannot click the link, please copy and paste the link into your browser.</p>
        <ul>
            <li><strong>Activation Link:</strong> <a href="http://example.com/activate/$actCode/$UserName">http://example.com/activate/$actCode/$UserName</a></li>
        </ul>
        <p>&nbsp;</p>
        <p>Thank you,<br />
        ~Kevin - our site</p>
    ]]>
</Template>

and the code I am pulling it in with is:

public function PullEmailTemplate($which, $id){
    $file = $which . '.config';
    $xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . '/assets/templates/' . $file, NULL, LIBXML_NOCDATA);
    $res = $xml->xpath("//Template[@id='" . $id . "']");
    return (string)$res[0]; 
}

and using it like:

$msg = PullEmailTemplate('user', 1);

Now, the email sends out successfully, but I get $FullName, $ActCode, and $UserName exactly as they are... in otherwords, the aren't outputting what they should be... for instance they should be (in order): Kevin, 12345, myuser@name.com

How can I do this?

  • 写回答

1条回答 默认 最新

  • dongpu4141 2014-04-24 22:32
    关注

    You have got a matryoshka here. The outer layer is an XML document. You already have source that reads the inner element from it. The inner element (inside the CDATA) is an HTML snippet with $name placeholders. You now need some source that replaces that placeholders without destroying the HTML.

    If $name is the extend of that template system, you could use preg_replace_callback() to match them. The pattern (\$[\w\d]+) should do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接