June 28th, 2006

Send emails from a template with PHP

When you're designing your web application, you'll find that various actions require that an email be sent off. It's easy to hard-code the email into the script. For example, in a tech support web application, you may want to send an email when a user submits a ticket, and another email alerting them of any replies.

But what happens when you have many different emails to send, and they deliver a lot of different, complex information. Then it gets too difficult to maintain many different lines of code for different emails. It's easier to create a generic email template class, and then as many templates as you want. Here's how!

Here's the template class:


class Template{
    var $template;
    var $variables;

    function Template($template){
        $this->template = @file_get_contents($template);
        $this->resetVars();
    } 

    function resetVars(){
        $this->variables = array();
    } 

    function Add($var_name, $var_data){
        $this->variables[$var_name] = $var_data;
    } 

    function AddArray($array){
        if ( !is_array($array) ){
            return;
        }
        foreach ( $array as $name => $data ){
            $this->Add($name, $data);
        }
        return;
    }

    function Evaluate($direct_output = false){
        $template = addslashes($this->template);
        foreach ( $this->variables as $variable => $data ){
            $$variable = $data;
        }
        eval("\$template = \"$template\";");
        if ( $direct_output ) {
            echo stripslashes($template);
        } else {
            return stripslashes($template);
        }
    }
}

Here's the code where you'll choose the template file, get whatever variables (username, order, etc) from the database, and merge it with the template file to send in the email.


    // build the logic to choose which template to use
    $template_file = 'templates/confirm_order.tpl';

    $template =& new Template($template_file);

    // here you need to get the user's specific data from a database, and dump it into a resultset called $result

    $from = "admin@domain.com";
    $to = "user's email address";

    foreach ( $result as $data ){
        $template->resetVars();
        $template->AddArray($data);
        $output = $template->Evaluate();
        mail( $from, $to, $output );
    }

And here's an example template file:


Your order has been confirmed, and will ship within 24 hours.

Product	     : {$product}
Url		: {$url}
Price		: {$price}
Description	: {$description}
Category	: {$category}
Contact name: {$contact_name}
Email   	: {$email}

Regards,
Company Name
  • Share/Bookmark

One Response to ' Send emails from a template with PHP '

Subscribe to comments with RSS or TrackBack to ' Send emails from a template with PHP '.

  1. Deepa said,

    on November 10th, 2009 at 6:48 pm

    I am getting error,
    Class 'Template' not found
    can u help me.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word