Forum Moderators: coopster

Message Too Old, No Replies

attaching page for email script

         

amikin

5:09 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



I'm wondering if there is a way to attach an external page to a variable to use with an email script.

Here's the scenario, previously how I did this was set the variable as follows
$htm = "some text for the email " . $variable . " some text";

The $html variable was then used as the email body when it was sent out.

What I would like to know if there is an easy way to attach an external file to this variable so that I can set up external template files for the emails.

eg.

$html = some way to attach my external file to this variable;

external file would be something like

<table width="540" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td colspan="2">Below are the results of the New Member Survey submitted by: <strong><?php echo $first_name;?> <?php echo $last_name;?></strong></td>
</tr></table>

quixote

6:03 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



This is not EXACTLY answering your question, but might be a way to do what you're shooting for.

You can define a string with HTML by doing something like this:

$html = <<<html

<table width="540" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td colspan="2">Below are the results of the New Member Survey submitted by: <strong>$first_name $last_name</strong></td>
</tr></table>

html;

Basically you enclose all your HTML markup inside the <<<html and html; parts. Notice also that you don't have to use <?php echo $x;?> and can just call the strings because it's all parsed through PHP. Naturally, your variable doesn't have to be called $html, it can be called anything you like.

Anyway, I hope this is useful to you.

cameraman

6:25 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What quixote did is called the heredoc syntax and it's invaluable.
To get an external file into a variable, you can do this:

$html = implode('',file('server_path/to/file.txt'));

on edit:
What you would do in the template file (which doesn't have to have a txt extension as above) is 'embed' your variable information using the delimiters of your choice - I generally use the curly braces:
...New member...{first_name}...other text

then after you read the file into $html, use str_replace:
$html = str_replace('{first_name}',$first_name,$html);

amikin

6:50 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



I'll give both ways a try.

Even the second option is better that how I have been doing it as you don't have to worry about conflicting double/single quotes with that method

I'll let you know how it goes

amikin

7:43 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



I there any way i can automatically loop through all my post data do do the string replace so that i'm not having to do a replace for every single variable?

cameraman

8:33 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



foreach($_POST as $key => $val)
$html = str_replace("\{$key}",$val,$html);

Keep in mind that mischievous people can make odd things go into a POST variable - the above line isn't checking for any of that.