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.
- Easier to update in the future. I don't have to search thou all my scripts looking for the body of an email instead I can just update the template.
- I can use the same template for multiple emails
- It follows the MVC pattern
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>"; ?>
email_template.tpl: Hello __NAME__ How are you doing?