PHP not showing in my email

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
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

PHP not showing in my email

Post by Addos »

I’m trying to add some htlm and php in a separate file using this for the mail content so that when an email is sent to a customer they get a formatted email:
<snip>

Code: Select all

    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $message = file_get_contents('mail.php');
<snip>

In my mail.php file I have:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Blah.</title>
</head>
<body>
    <table>
      <tr>
        <td ><p>Thanks <?PHP echo $Name; ?>etc</p>
        </td>
      </tr>
    </table>
</body>
</html>
I receive the email ok with the text details above but the <?PHP echo $Name; ?> is not parsed.

If I was to use instead:

Code: Select all

$message .= 'Thanks'. ' ' ."$Name".' '  ."\r\n";
Then this would be parsed okay and $Name would hold the value and come through in the email.

So why when I try to add the external php file is the PHP being ignored. Do I need to add PHP tags to the HTML of my page and echo all of these?

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP not showing in my email

Post by requinix »

file_get_contents doesn't try to execute PHP code. Just reads files, that's all.

Use output buffering and include().

Code: Select all

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
 
ob_start();
include "mail.php";
$message = ob_get_contents();
ob_end_clean();
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: PHP not showing in my email

Post by Addos »

Thanks for this reply. I managed to make this work to some extent but I'm getting only HTMl code showing in Outlook Express when I get a reply email and I'm wondering what this is signifying?

This is what I'm using to send all the mail and I have done this with a different form before and it was ok but using ob_start() is new to me and I'm not sure if this is causing problems.
Thanks

Code: Select all

$from = 'text here' . "\r\n";
    $subject = 'text here' . "\r\n";
    $to = " Admin<address>, Customer email<$email> \r\n";
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    ob_start();
    include('mail.php');
    $message = ob_get_contents();
    ob_end_clean();
 
  mail($to,$subject,$message,$headers);
Post Reply