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
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?