Forum Moderators: coopster

Message Too Old, No Replies

Email an included page with mail()

         

andrewheiss

11:23 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



I'm making a confirmation page for an event registration system and would like to e-mail it to the person registering as a reminder. I'm trying to e-mail it in HTML format since I already have the page dynamically generated in an included file, as seen below:


<?php include('includes/confirmation.php'); // This parses and displays the confirmation page in the web browser
$to = $email;
$subject = "Conference Confirmation";
$message = "<html><body>" . include('includes/confirmation.php') . "</body></html>";
$headers = "From: no-reply@example.com" . "\r\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1" . "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message1, $headers);
?>

An e-mail gets sent, but it has no content. Is it possible to e-mail an included snippet of HTML/PHP like I'm trying? Is there something wrong in my headers?

Thanks!

mipapage

11:37 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...but it has no content

When you view source on the 'empty' email, do the html and body tags show up?

Is it possible to e-mail an included snippet of HTML/PHP like I'm trying?

It can be done, but perhaps not the way you are trying.

The way I have done this in the past, was to file_get_contents the page/html in question (your includes/confirmation.php, but to get it from an URL, not an include). This way was the easiest for the implementation I was using.

So $message = file_get_contents('http://www.somepage.com/confirmation.php');

This requires that fopen wrappers have been enabled. See the entry on file_get_contents in php.net for more info.

andrewheiss

12:31 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Using file_get_contents worked...kind of.

My includes/confirmation page uses variables that I get from the current page--I insert a whole bunch of information from a form into MySQL at the top of the page and in the actual HTML I show what they just inserted, using variables like $name, $email, etc.

When the confirmation page gets e-mailed, it is not parsed by PHP until getting read by the browser/e-mail viewer, meaning all my variables are dead (since it's a new page).

Is there any way to render/parse the PHP page before e-mailing it out?

Thanks!

mipapage

6:54 am on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey andrewheiss,

These are the same reasons why I did mine this way. It was easiest for us to have the page load at an URL, as this invoked the CMS to run and therefore we had access to the DB and could fill in the necessary vars.

So I guess that is your answer. Find a way to get the necessary data and to fill in the vars of your page before file_get_contents. Sorry I cannot be of more help, but w/out knowing your exact system the answer becomes a little abstract.

andrewheiss

3:30 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



Thanks for all the help and advice!

I finally got something to work. Rather than use HTML e-mail, I did it all in plain text (although I could have done it in HTML now that I think of it)

Before my confirmation include, I started a $message variable with some initial e-mail text. In the confirmation page where the confirmation was automatically generated, I inserted $message .= "whatever\n\n" in each of the sections that got rendered. For example:


<?php
if ($volunteer=="yes"){
?>
<tr>
<th></th>
<td>I would like to volunteer at this event</td>
<tr>
<?php
$message .= "I would like to volunteer at this event\n\n";
}
?>

By the end of going through all the confirmation logic, $message was big and long, but e-mailable in with mail(). Roundabout solution, yes, but it worked.

mipapage

5:22 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Roundabout solution, yes, but it worked.

Ours was a bit roundabout too, as the client hadn't spec'd the resulting e-mail to be html. Glad you got it working!