Forum Moderators: coopster

Message Too Old, No Replies

server-side include in mail() function

         

hypersound

12:52 am on Feb 10, 2003 (gmt 0)

10+ Year Member



Is it possible to place a server-side include into the message section of the mail() function?

That way, when the email is sent, the message body consists of the executed php code from another file.

<?php
mail("$email", "$subject", "require_once('email2.php');",
?>

The above code is obviously not right. Can anyone suggest anything?

Thanks.

lorax

2:43 am on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would think you'd need to use one of the file open commands (fopen or fread) and parse the file into a variable that you could then use as your msg.

hypersound

3:26 am on Feb 10, 2003 (gmt 0)

10+ Year Member



seems like fopen or fread are mainly used for accessing it text files. im trying to parse a php file which has a do loop within it into the message string of the mail function.

crypto

5:43 am on Feb 10, 2003 (gmt 0)

10+ Year Member



see if eval() function is of any help.

I've written a small piece of code to get
the contents of file text.php.

Contents of text.php are:
--------------------------
<?
$a="XYZ";
$b="ABC";
for($i=0;$i<5;$i++)
{
echo $a.$b;
}
?>

File parsing the above code:
----------------------------
<?
$code = implode ('', file ('text.php'));

ob_start(); //turn output buffering on

eval(str_replace("?>","",str_replace("<?","",$code)));
$message=ob_get_contents();

ob_end_clean(); //turn off output buffering

mail("to@xyz.com","the subject",$message,"From:me@xyz.com";
?>

Personally I feel this is a complex way of doing things.
Better way would be to build your message variable inside
the file "xyz.php" and use the variable after the
include "xyz.php" statement directly.

andreasfriedrich

9:25 am on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do


<?php
mail($email, $subject, require('email2.php'));
?>

if you made sure that email2.php does not use any output functions but returns the text you want to use as your email using the return() [php.net] construct.

Alternatively you could do something like crypto describes: Start a new output buffer with ob_start() [php.net], do your require() [php.net] (there is no need to eval() [php.net]ue the code, [url=http://www.php.net/require]require() [php.net][/url] or [url=http://www.php.net/include]include() [php.net][/url] will work just fine) and then get the output of the require() [php.net] by calling ob_get_contents() [php.net]. To stop this output buffer call ob_end_clean() [php.net].

Andreas

crypto

3:28 am on Feb 11, 2003 (gmt 0)

10+ Year Member



oh silly me... yes there is no need of eval(). only
an include() or a require would do. thanx Andreas for that.