Page 1 of 1
Display php page in email body?
Posted: Mon Feb 22, 2010 8:57 am
by fatman
I have a page on my site that sends a mail to my client containing a copy of their statement in the body of the mail. The statement is a PHP page rendering from my database and I used iframe till now. It works fine as long as they click "Load Pictures" in their email client. Problem is that anti-virus programs don't like this so I need to change? I am playing with the code below now but it shows the code of the page in the body of the mail instead of a rendered version of the page.
Code: Select all
<?php
$handle = fopen("http://www.myserver.com/statement.php", "rb");
$contents = ' ';
while (!feof($handle)) {
$fdisplay .= fread($handle, 8192);
// Pass $fdisplay to the body section of the mail
}
fclose($handle);
// Send the mail
$recipient ="client@hismail.com";
$from ="me@myserver.com";
$bod="$fdisplay";
$to = "$recipient";
$from = "$email";
$subject = "Your current statement";
$message = "$bod";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $header);
?>
I thought to use "print" somewhere in there?
Re: Display php page in email body?
Posted: Mon Feb 22, 2010 9:36 am
by AbraCadaver
You either have <? in statement.php instead of <?php, or you are not accessing it by URL.
Re: Display php page in email body?
Posted: Mon Feb 22, 2010 10:16 am
by fatman
Actually, I have experimented by using a plain .htm file from somewhere in my site in the place of statement.php and same thing? Displays the page code instead of the page.
Re: Display php page in email body?
Posted: Mon Feb 22, 2010 11:31 am
by jefffan24
1. does your host support php?
2. .htm will never work with php unless you edit your php.ini file. You need to keep the extension .php .
Re: Display php page in email body?
Posted: Mon Feb 22, 2010 1:35 pm
by fatman
I might not have been clear before.
I am on a PHP server and my page 'statement.php renders perfectly on my site. I have .php and .htm files in my site. I used the code shown to call or open the file and input what the code gets into the body of the mail that the page sends. This did not matter whether I was using statement.php or conditions.htm.
The problem is that the script shown delivers the source code of the page in the body of the mail sent. ie. the recipient of the mail views the source code of the page I am trying to show in the body of the mail.
I am now trying with:
file_get_contents('
http://www.sunrise-lmg.com/statement.php');
or
readfile('
http://www.sunrise-lmg.com/statement.php');
Anybody got any ideas on whether this can work?
Re: Display php page in email body?
Posted: Mon Feb 22, 2010 9:20 pm
by Benjamin
Using fopen bypasses apache and the php engine. You will need to use file_get_contents using the website address.
Re: Display php page in email body?
Posted: Tue Feb 23, 2010 8:14 am
by fatman
The code below is what I used now but it still gives me the source code of the page in the email body? I am getting desperate, please, any idea's welcome.
Thanks
Code: Select all
<?php
$recipient ="gerard@sunrise-lmg.com";
$from ="info@sunrise-lmg.com";
$to = "$recipient";
$from = "$email";
$subject = "Message forwarded from me";
$message = file_get_contents('http://www.mysite.com/statement.php');
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $header);
?>
I am wondering about the "Content header"? Should it be something other than text/html?
Re: Display php page in email body?
Posted: Tue Feb 23, 2010 9:17 am
by AbraCadaver
astions wrote:Using fopen bypasses apache and the php engine. You will need to use file_get_contents using the website address.
fopen() will perform the same as file_get_contents() on a URL.
fatman
If you are saying that
http://www.myserver.com/statement.php in a browser window works as expected, then I'm at a loss.
Re: Display php page in email body?
Posted: Tue Feb 23, 2010 9:26 am
by meekamoo
I've done a similar thing myself.
What you could maybe do is incorporate the email code into the statement.php file and then use PHP's output buffering.
You'd then initate the mail with calling statement.php?sendemail and then in the statement.php it would look like so:
Code: Select all
<?php
if ($_REQUEST['sendemail'])
{
ob_start();
}
(code to generate the statement)
if ($_REQUEST['sendemail'])
{
$email = ob_get_contents();
mail($to, $subject, $email)
ob_end_clean();
}
?>
Alternatively, look at the php cURL functions. (
http://php.net/manual/en/book.curl.php)
Code: Select all
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/statement.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$email = curl_exec($ch);
curl_close($ch);
?>
(
EDIT: Forgot the return transfer setting.)