如何将原始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.

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.

it should be like this:

Hello ' . $fname . ', ' . $lname . ',

You can replace text form $message with the values of your variables.

Try something like this (use | as special character at your template:

$variables = array(
    "|fname" => $fname,
    "|lname" => $lname,
    "|email" => $email,
    "|password" => $password
);

$message  = str_replace(array_keys($variables), array_values($variables), $message );

Edit: thanks @Valerij for your comment.