PHP code example of utoronto / email-merge

1. Go to this page and download the library: Download utoronto/email-merge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

utoronto / email-merge example snippets


// Define allowed token names
$tokens = new TokenSet(array("USERNAME", "FOO", "BAR"));

// Email subject line may contain tokens also
$subject = "Hello %USERNAME%";

// HTML email body
$htmlBody = <<<HTML_BODY
<p>Here is a list of interpolated values
  <ul>
    <li>Foo: %FOO%</li>
    <li>Bar: %BAR%</li>
  </ul>
</p>	
HTML_BODY;

// Create a template in which all tokens must be "allowed"
$tpl = new Template($subject, $htmlBody, $tokens);

$parser = new Parser($tpl);

$data = array(
    "USERNAME" => "qq12345",
    "FOO" => "my foo value",
    "BAR" => "my bar value"
);

// A Parser instance will return Template objects containing interpolated values
$result = $parser->getResult($data);

// Define allowed token names
$tokens = new TokenSet(array("USERNAME", "FOO", "BAR"));

// token name not in allowed set
$subject = "Hello %NOT_A_LEGAL_TOKEN%";

// throws UnrecognizedTokenException
$tpl = new Template($subject, "", $tokens);