chaser
chaser

Reputation: 3195

How to store email template in PHP?

I have the following code to send users a verification email, which contains some variables such as username and token for verification purpose. The variable obviously changes on an individual basis. However, I would like to store this message separately in the PHP include_path so that I can re-use it somewhere else if need be. I don't want to hardcode it like it is now:

$to = $username;
$subject = 'Account Verification';
$now = date('Y-m-d H:i:s',time());
$message = "
Date sent:$now

Thanks for signing up! 

Your account has been created, however we need to verify that this is your email address. Here is your verification token which you should copy and paste on the verification page:

<strong>$token</strong>

Alternatively, click the below link to activate your account:

http://localhost/login/verification.php?username=$username&token=$token 

If you did not register with us. You <a href='http://localhost/login/'>can click</a> here to report this to us, or ignore this email.

Thanks,

The LocalHost Team
";
mail($to, $subject, $message);

So three questions:

  1. How do I store this message to be like a template in such a way the variables are still accessible?
  2. How do I turn this message into a nicely HTML formatted message with html markups?
  3. Can I do this:

    $to = $username; $subject = 'Account Verification'; $now = date('Y-m-d H:i:s',time()); $message = include "verification_template.php"; mail($to, $subject, $message);

Upvotes: 1

Views: 915

Answers (2)

Ben Rowe
Ben Rowe

Reputation: 28711

Alternatively to php, you can use template engines like Smarty to generate the html/text needed for your emails.

The advantage of using a templating engine is that your template can contain simple logic (such as conditionals, loops for handling dynamic data.

Upvotes: 0

mariusnn
mariusnn

Reputation: 1897

I solve the inclusion like this:

ob_start();
include $filename;
$message = ob_get_clean(); 

Upvotes: 2

Related Questions