电子邮件模板上的preg_replace

时间:2015-01-20 19:17:17

标签: php email swiftmailer

我使用swift邮件程序,当我从服务器收到电子邮件时,我得到了这个:

您通过忘记密码

申请密码

您的用户名是:{myusername}

您的密码是:{mypassword}

with {},send的功能是:

function format_emailforget($info, $format){

    //set the root
    $root = $_SERVER['DOCUMENT_ROOT'].'/email';

    //grab the template content
    $template = file_get_contents($root.'/forget_template.'.$format);

    //replace all the tags
    $template = preg_replace('{USERNAME}', $info['username'], $template);
    $template = preg_replace('{EMAIL}', $info['email'], $template);
    $template = preg_replace('{PASSWORD}', $info['password'], $template);
    $template = preg_replace('{SITEPATH}','http://smracer.com', $template);

    //return the html of the template
    return $template;

}

这个代码的错误想法在哪里?

1 个答案:

答案 0 :(得分:0)

更优雅的方式,易于维护:)

$pattern     = array('/\{USERNAME\}/' , '/\{EMAIL\}/' , '/\{PASSWORD\}/' , '/\{SITEPATH\}/');
$replacement = array($info['username'], $info['email'], $info['password'], 'http://smracer.com');

$template = preg_replace($pattern, $replacement, $template);