Forum Moderators: coopster

Message Too Old, No Replies

sendmail templates and variables - confused?

sendmail templates and variables

         

railboy

3:14 pm on Oct 26, 2004 (gmt 0)

10+ Year Member



Hi,

I'm going round in circles looking for a half decent solution, I was wondering if anyone out here could lend me a hand.

Depending on several data selections from a user submitted html form, I would like to send off an automatic email reply.

Their are approximately 20 different replies and I would like to be able to customize the response e.g.

"Dear $member,

Your dossier $reference has been acepted...."

For easy edits, I would like to store the template replies in seperate files. Using a Switch statement for the different cases, I would assign content to a "$content" variable which I would then use as below:

<?
$to = $member_email ;
$subject = "$topic" ;
$msg = $content ;
mail($to, $subject, $msg) ;
?>

I have tried "includes" and "requires" and even reading text files using:

$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

But I just can't get this to work, the variables in the templates never appear correctly.

Any ideas?
thanks.

Birdman

3:32 pm on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what you need to use is str_replace. See, I think PHP is not recognizing your variables because they are just part of the string.

I suggest marking the editable sections like so:

[u]email_template.txt[/u]

Dear ##MEMBER##,

Your dossier ##REFERENCE## has been acepted....

Then, set up your replacement arrays:

$find = array('##MEMBER##', '##REFERENCE##');
$replace = array($member, $reference);

// Now get the template into a string

$message = implode('', file('/path/email_template.txt'));

// Now do the replacements

$message = str_replace($find, $replace, $message);

// Now $message should be ready to send!

Regards,
Birdman

railboy

3:51 pm on Oct 26, 2004 (gmt 0)

10+ Year Member



WOW thanks bird(miracle)man... worked straight off first time! thanks.

:)

I'm not quite sure why PHP would consider the file as a string and not as a string with variables in it. Would be nice to know but am already super stoked that it was this easy to solve... got a lot to learn.