简体   繁体   中英

Inject PHP Variable(s) into XML Email Template

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM