如何在PHP中存储电子邮件模板?

时间:2012-08-06 02:57:55

标签: php sendmail

我有以下代码向用户发送验证电子邮件,其中包含一些变量,例如用户名和令牌,以便进行验证。该变量显然在个人基础上发生变化。但是,我想将此消息单独存储在PHP include_path中,以便我可以在其他地方重新使用它(如果需要)。我不想像现在这样硬编码:

$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);

所以有三个问题:

  1. 如何将此消息存储为模板,以便变量仍可访问?
  2. 如何将此消息转换为带有HTML标记的HTML格式的消息?
  3. 我可以这样做:

    $ to = $ username; $ subject ='帐户验证'; $ now = date('Y-m -d H:i:s',time()); $ message = include“verification_template.php”; 邮件($ to,$ subject,$ message);

2 个答案:

答案 0 :(得分:2)

我解决了这样的包含:

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

答案 1 :(得分:0)

除了php之外,您还可以使用Smarty之类的模板引擎来生成电子邮件所需的html /文本。

使用模板引擎的优点是模板可以包含简单的逻辑(例如条件,处理动态数据的循环。