PHP email template

Contents

    When I create a PHP script that has to send an email to a user I tend to use a template file for the body of the email. I load the template file in to the script then search and replace the variables that I need to before sending it to the user.

    I use this template system for a few reasons.

    One of my favorite PHP template engines is http://www.smarty.net/ it just works. You can use it for both Email, Websites or what ever else you want. But it might be a little over kill when you want to just send one email using a template. Instead you could use something similar to this;

    SendEmail.php:
    <?php
    $name = 'Mr Waterbottom';
    $emailBody = file_get_contents( 'email_template.tpl' ) ;
    $emailBody = str_replace( '__NAME__', $name, $emailBody );
    // SentEmail( $email, "Email Subject", $emailBody );
    echo "<pre>";
    echo $emailBody ;
    echo "</pre>";
    ?>

    </code>

    email_template.tpl:
    Hello __NAME__
    How are you doing?