I have a simple Email() class. It\'s used to send out emails from my website.
Email::send($to, $subj, $msg, $options);
?>
I also h
One solution is to globalize the variables needed not inside the template but inside the function send
.
public static function send($to, $subj, $msg, $options = array()) {
global $SITE_NAME, $SITE_URL, $name;
/* ... */
ob_start();
include '/inc/email/templates/account_created.php';
$msg = ob_get_clean();
/* ... */
}
Another solution is to pass those extra variables as parameters. This maybe ugly as the number of parameters in the set
function may grow a lot depending on how many of them you need in your template. To solve this issue, another way of implementing this solution is by passing those extra variables as a hash and create those variables on-the-fly (using the function eval
). Here's an example:
public static function send($to, $extra_vars = array()) {
foreach ($extra_vars as $key => $value) {
eval("\$$key = '$value';");
}
/* ... */
ob_start();
include '/inc/email/templates/account_created.php';
$msg = ob_get_clean();
/* ... */
}
Then when you should call send like this:
$SITE_NAME = "www.somewebsite.com";
Email::send("recipient", array('SITE_NAME' => $SITE_NAME));