How to insert php variables into html email message

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
rlinsurf
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2008 11:00 am

How to insert php variables into html email message

Post by rlinsurf »

Hi--

I'm really having a hard time with this code. I think I've set this up according to the manual, but the following things are happening:

1. My variables aren't appearing in the message. I just get blanks.
2. I'm getting all these error messages -- or something? -- at the top of the message body (Please see screenshots).
3. The Bcc: being sent is unformatted html -- just a bunch of tags.

Obviously, I'm missing the correct syntax here.

My workflow here is from my index.html page, which is the form the customer submits. The form is sent to process.php which processes the form. On that page, there' s an include to receipt.php -- the code for which is below. This prints the receipt, and sends the customer a receipt email. The variables are all working in all the pages, so I didn't think they needed to be re-assigned. For example, the upper portion of the code, which prints the receipt in the browser, works fine using the same variables.

Here's the full code for receipt.php:

Code: Select all

<html>
<head>
    <title>Groundlevel Store Receipt</title>
    <meta name="generator" content="BBEdit 6.5.1">
        <style type="text/css" media="all"><!--
.dsR7 /*agl rulekind: base;*/ { width: 467px; height: 90px; }
--></style>
    </head>
<body>
        <h2>Thank you for your order!</h2>
        <div align="left">
            <hr>
            Your transaction has been approved.
            <br><br>
            A confirmation email for this order has been sent to the email provided.<br>
        <br>
            <table class="dsR7" width="456" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td></td>
                    <td><b>Name: </b><?php echo $fullname; ?></td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Email: </b><?php echo $email; ?><br>
                        <br>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Product: </b><?php echo $item; ?><br>
                        <br>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Total: </b><?php echo '$'; ?><?php echo$total; ?></td>
                </tr>
            </table>
        </div>
        <p>
<i><?php echo $quantity; ?> 1 Groundlevel Store order(s) processed <?php echo $orderdate; ?>.</i>
    </body>
</html>
 
 
 
<?php
 
// The next line has the subject of the message. The remaining lines before the ending tag
// are the body of the message.
  
$message .= '<html>
 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css" media="all"><!--
.dsR5 /*agl rulekind: base;*/ { width: 645px; height: 75px; }
.dsR6 /*agl rulekind: base;*/ { width: 645px; height: 158px; }
.emailtext { color: gray; font-size: 10pt; font-family: Verdana, Arial, Helvetica, sans-serif; }
.headertext { color: gray; font-size: 14pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; }
.ds4 /*agl rulekind: base;*/ { font-size: 15px; }
.ds10 /*agl rulekind: base;*/ { font-size: 12px; }
a:link {
    text-decoration: none;
    color: #3399FF;
}
a:visited {
    text-decoration: none;
}
a:hover {
    text-decoration: none;
}
a:active {
    text-decoration: none;
}
--></style>
    </head>
 
<body>
        <p><img class="dsR5" src="https://ssl21.pair.com/grndlvl/commerce/gl/images/GRLogo.gif" alt="" border="0" /></p>
        <p class="headertext">Groundlevel Store Order</p>
        <table class="dsR6" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><span class="emailtext">Name:</span></td> 
                <td class="emailtext"><?php echo $fullname; ?></td> 
           </tr> 
            <tr> 
                <td><span class="emailtext">Email Address:</span></td> 
                <td class="emailtext"><?php echo $email; ?></td> 
            </tr> 
            <tr> 
                <td><span class="emailtext">Date:</span></td> 
                <td class="emailtext"><?php echo $orderdate; ?></td> 
            </tr> 
            <tr> 
                <td><span class="emailtext">Product:</span></td> 
                <td class="emailtext"><?php echo $item;?></td> 
            </tr> 
           
            <tr> 
                <td><span class="emailtext">Total:</span></td> 
                <td class="emailtext"><?php echo$total; ?></td> 
            </tr>
            
        </table>
        <p class="headertext"><span class="ds4">Thank You for Your Order</span></p>
    <p class="headertext"><span class="ds10">You can expect your order to be delivered promptly. You will also receive notifcation of your shipment along with the tracking information from the carrier you selected for delivery, either USPS, UPS or Fedex.  If you have any questions about your order, please email us at: <a href="mailto:sales@groundlevel.net">sales@groundlevel.net.</a></span></p><br><br>
</body>
 
</html>';
$headers = "From: Groundlevel Store <sales@groundlevel.net>\r\n" .  
"Bcc: Groundlevel Store <grndlvl@gmail.com>\r\n" . 
"MIME-Version: 1.0\r\n" .  
"Content-type: text/html; charset=iso-8859-1";
 
 
mail($email,"Your Groundlevel Store Order",$message,$headers); // this line is the ending tag
 
?>
And here are two screenshots of my output. The first is the html formatted email to the customer:
Image

And here's the Bcc: as it's coming through to an email client:

Image

Thanks :)
rlinsurf
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2008 11:00 am

Re: How to insert php variables into html email message

Post by rlinsurf »

Ok. I figured out the right syntax to insert the variables :)

So now, the problems are the garbage at the top of the html formatted email, and the fact the Bcc: isn't properly formatted at all.

Here's the code as it now stands:

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
    <title>Groundlevel Store Receipt</title>
    <meta name="generator" content="BBEdit 6.5.1">
        <style type="text/css" media="all"><!--
.dsR7 /*agl rulekind: base;*/ { width: 467px; height: 90px; }
--></style>
    </head>
<body>
        <h2>Thank you for your order!</h2>
        <div align="left">
            <hr>
            Your transaction has been approved.
            <br><br>
            A confirmation email for this order has been sent to the email provided.<br>
        <br>
            <table class="dsR7" width="456" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td></td>
                    <td><b>Name: </b><?php echo $fullname; ?></td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Email: </b><?php echo $email; ?><br>
                        <br>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Product: </b><?php echo $item; ?><br>
                        <br>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><b>Total: </b><?php echo '$'; ?><?php echo$total; ?></td>
                </tr>
            </table>
        </div>
        <p>
<i><?php echo $quantity; ?> 1 Groundlevel Store order(s) processed <?php echo $orderdate; ?>.</i>
    </body>
</html>
 
 
 
<?php
 
// The next line has the subject of the message. The remaining lines before the ending tag
// are the body of the message.
  
$message .= '<html>
 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css" media="all"><!--
.dsR5 /*agl rulekind: base;*/ { width: 645px; height: 75px; }
.dsR6 /*agl rulekind: base;*/ { width: 645px; height: 158px; }
.emailtext { color: gray; font-size: 10pt; font-family: Verdana, Arial, Helvetica, sans-serif; }
.headertext { color: gray; font-size: 14pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; }
.ds4 /*agl rulekind: base;*/ { font-size: 15px; }
.ds10 /*agl rulekind: base;*/ { font-size: 12px; }
a:link {
    text-decoration: none;
    color: #3399FF;
}
a:visited {
    text-decoration: none;
}
a:hover {
    text-decoration: none;
}
a:active {
    text-decoration: none;
}
--></style>
    </head>
 
<body>
        <p><img class="dsR5" src="https://ssl21.pair.com/grndlvl/commerce/gl/images/GRLogo.gif" alt="" border="0" /></p><br>
        <p class="headertext">Groundlevel Store Order</p>
        <table class="dsR6" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><span class="emailtext">Name:</span></td> 
                <td class="emailtext"><b>';
$message .= $fullname;
$message .= '</b></td> 
           </tr> 
            <tr> 
                <td><span class="emailtext">Email Address:</span></td> 
                <td class="emailtext"><b>';
$message .= $email;
$message .= '</b></td> 
            </tr> 
            <tr> 
                <td><span class="emailtext">Purchase Date:</span></td> 
                <td class="emailtext"><b>';
$message .= $orderdate;
$message .= '</b></td> 
            </tr> 
            <tr> 
                <td><span class="emailtext">Product:</span></td> 
                <td class="emailtext"><b>';
$message .= $item;
$message .= '</b></td> 
            </tr> 
           
            <tr> 
                <td><span class="emailtext">Total:</span></td> 
                <td class="emailtext"><b>$';
$message .= $total;
$message .= '</b></td> 
            </tr>
            
        </table><br>
        <p class="headertext"><span class="ds4">Thank You for Your Order</span></p>
    <p class="headertext"><span class="ds10">You can expect your order to be delivered promptly. You will also receive notifcation of your shipment along with the tracking information from the carrier you selected for delivery, either USPS, UPS or Fedex.  If you have any questions about your order, please email us at: <a href="mailto:sales@groundlevel.net">sales@groundlevel.net.</a></span></p><br><br><br>
</body>
 
</html>';
$headers = "From: Groundlevel Store <sales@groundlevel.net>\r\n" .  
"Bcc: Groundlevel Store <grndlvl@gmail.com>\r\n" . 
"MIME-Version: 1.0\r\n" .  
"Content-type: text/html; charset=iso-8859-1";
 
 
mail($email,"Your Groundlevel Store Order",$message,$headers); // this line is the ending tag
 
?>
rlinsurf
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2008 11:00 am

Re: How to insert php variables into html email message

Post by rlinsurf »

I have found out that these warnings are coming from URIBL. So there must be some way to suppress those warnings being sent with the message body, no?
rlinsurf
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2008 11:00 am

Re: How to insert php variables into html email message

Post by rlinsurf »

Ok. I tracked down the URIBL warnings to my ISP. They are now checking why their server is doing that.

I also found out the the reason the Bcc: was failing was that it was going to my gmail account. I didn't realize gmail doesn't parse html :roll:

So I'm left with one more weird problem. In Outlook, the test messages aren't going to my test POP account, even though the To: still says that's the account they should go to. And even stranger, an iPhone I test on isn't receiving them at all, even though that iPhone is setup to receive the test POP account's emails, and gets other text-based email messages regularly.

Maybe someone can look at my code and see if there's something I've missed which is causing this behavior?
Post Reply