1

我有以下代码向用户发送验证电子邮件,其中包含一些变量,例如用于验证的用户名和令牌。变量显然会因人而异。但是,我想将此消息单独存储在 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. 如何将此消息转换为带有 h​​tml 标记的精美 HTML 格式的消息?
  3. 我可以这样做:

    $to = $用户名;$subject = '账户验证'; $now = date('Ymd H:i:s',time()); $message = 包括“verification_template.php”;邮件($to,$subject,$message);

4

2 回答 2

2

我像这样解决包含:

ob_start();
include $filename;
$message = ob_get_clean(); 
于 2012-08-06T03:01:30.007 回答
0

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

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

于 2012-08-06T03:21:23.217 回答