douke6881 2014-04-29 11:47
浏览 56
已采纳

如何将原始PHP代码保存到数据库或文本文件以发送电子邮件?

Well, In admin panel I'm going to create a custom Email Template. For example... 1) Registration Confirmation 2) Forgot Password etc...

Now let's talk about Registration Confirmation.

After successfully register I'm (admin) sending a confirmation email which I want to edit from admin panel. So my confirmation text is look like this which I save it as .tpt file :

Confirmation text with php code:

<p>Hello $fname $lname, Thanks for your registration in our site Following is your login info.<p/>
<p>Username : <strong>$email</strong> <br/> Password : <strong>$password</strong> </p> <br/> Please click on the link to active your acccount. <a 
href='www.//mysite.com/activeAccount.php?code=$key&gonona=$userid'>Active My Account</a> </p>
<p>Thank You.<br/>Support Team <br/>www.//mysite.com/</p>";

In Register.php file I just call this .tpt file when I sending a email message using...

$message = file_get_contents("templates/email_verification.tpt", true);

In admin panel when I want to edit it I just call it with $file_content = file_get_contents($file); function in html textarea field.

So what I want is... When I send the confirmation message from Register.php page I got text with php code(variable e.g. $fname, $lname) in my email addresss which I don't want. It's should be all plain text. It's should be get the php variable (e. g. $fname, $lname, $email, $password, $key, $userid etc..) value from Register.php page and send it to email address...

How can I do this ? any solution or Idea are Most Welcome :)

Note: Sorry for my bad English language and lack of Php knowledge.

  • 写回答

3条回答 默认 最新

  • dongzhi1851 2014-04-29 12:01
    关注

    You don't necessarily have to save php variables in your template.

    You could also save a template like this:

    Hello {{NAME}},
    This is your password: {{PASSWORD}}
    This is your email: {{EMAIL}}
    

    This way your admin doesn't see weird PHP but a little bit more understandable template.

    And when you load the template you can replace the words with your variables at the moment of loading templates.

    $message = file_get_contents("templates/email_verification.tpt", true);
    $search = array(
        '{{NAME}}',
        '{{EMAIL}}',
        '{{PASSWORD}}',
    );
    $replace = array(
        $fname,
        $femail,
        $fpassword,
    );
    $message = str_replace($search, $replace, $message);
    

    Personally I also like to use strtr()

    $trans = array(
        "{{NAME}}" => $fname,
        "{{EMAIL}}" => $femail,
        "{{PASSWORD}}" =>  $fpassword
    );
    $message = strtr($message, $trans);
    

    The reason why strtr() could be better is if i would enter the following data in form:

    • name : {{PASSWORD}}
    • email:test@test.com
    • password:foobar

    With str_replace() If I would enter my name as {{PASSWORD}} the incorrect result would be this:

    Hello foobar,
    This is your password: foobar
    This is your email: test@test.com
    

    This is because str_replace replaces the earlier replaced {{NAME}} with {{PASSWORD}} and again with the foobar!!

    With strtr() te resul would be the foollowing (because it only replaces 1 time):

    Hello {{PASSWORD}},
    This is your password: foobar
    This is your email: test@test.com
    

    Which is correct in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?