Display php page in email body?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Display php page in email body?

Post 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?
Last edited by Benjamin on Mon Feb 22, 2010 9:17 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Display php page in email body?

Post by AbraCadaver »

You either have <? in statement.php instead of <?php, or you are not accessing it by URL.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Re: Display php page in email body?

Post 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.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Display php page in email body?

Post 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 .
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Re: Display php page in email body?

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Display php page in email body?

Post by Benjamin »

Using fopen bypasses apache and the php engine. You will need to use file_get_contents using the website address.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Re: Display php page in email body?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Display php page in email body?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
meekamoo
Forum Newbie
Posts: 1
Joined: Mon Apr 10, 2006 2:54 pm

Re: Display php page in email body?

Post 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.)
Post Reply