Forum Moderators: coopster

Message Too Old, No Replies

replacing values in saved templates

         

glennnall

5:29 pm on Feb 28, 2010 (gmt 0)

10+ Year Member


ok.

i'm sending a thanks email in html via this method (and i don't have to do it this way):
[code]
$template = 'welcome.php';
$fd = fopen($template,"r"[smilestopper]);
$message = fread($fd, filesize($template));

mail($to,$message, etc...)
[/code]

this is so my client can edit the html.

BUT.

what i really need to do is stick a value (registrant's name) in the variable that will be in the welcome.php that will be sent, "Hello, $newuser...".

have i made that difficult/impossible using fread() since the welcome.php is really not being opened?

can someone help me with this?

thanks much

oh - i tried $template = 'welcome.php?name=name'; but that didn't work...

astupidname

6:14 pm on Feb 28, 2010 (gmt 0)

10+ Year Member



have i made that difficult/impossible using fread() since the welcom.php is really not being opened?

Yeah, why do an fopen on a .php page? You could just have welcome.php execute and give you its message after you include it after defining $newuser. Or there are many ways of course.. but something along the lines of:
$newuser = 'John Doe';
include('welcome.php'); //import $message from welcome.php
mail($to, $message, etc...);

<?php
//welcome.php
if (!isset($newuser)) { //if the $newuser variable is not set, use a generic text (optional):
$newuser = 'new member';
}
$message = "Hello $newuser, thank you $newuser for joining our site!";
?>

astupidname

6:30 pm on Feb 28, 2010 (gmt 0)

10+ Year Member



Oh, and by the way, welcome to webmasterworld!

glennnall

6:30 pm on Feb 28, 2010 (gmt 0)

10+ Year Member



well, exactly - i did an include() first, and it opened it of course - i wasn't sure whether my client wanted it that way, but i suppose that can be done.

am i asking too much to try to stick a value somewhere in the php file via fwrite...? (i'm a bit of a noob with this)

astupidname

6:49 pm on Feb 28, 2010 (gmt 0)

10+ Year Member



You won't want to do fwrite's on the welcome.php page, not on a regular basis I would think. What is your intention of wanting to do that? Another alternative if you want to use fopen and all that jazz, treating welcome.php as pure text, is you could use placeholders and then do either a preg_replace or str_replace of the contents after fread'ing it. Though this would probably be better served if welcome.php were instead a .txt or .html file. Such as:
//welcome.html file
<h3>Welcome {registrantName} to our website</h3>
....
Then in the .php file where email is sent:
//do the fopen and the fread (having $newuser and $message defined), then do a replace on $message:
$message = str_replace('{registrantName}', $newuser, $message);
//send email

Getting any closer?

glennnall

7:24 pm on Feb 28, 2010 (gmt 0)

10+ Year Member



got it - thanks so much - string_replace... awesome