PHP inside an HTML 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

larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

PHP inside an HTML Email

Post by larrytech »

Hi,

I am having a problem with putting PHP code inside an HTML email.

With:

Code: Select all

<?php 
mail('info@larrytech.biz', 'Subject', 
    $message, 
    "MIME-Version: 1.0\n" . 
    "Content-type: text/html; charset=iso-8859-1"); 
?>
I am trying to have the email body as the output of a shopping cart.

Code: Select all

<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="15%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						&nbsp;&nbsp;<b>Qty</b>
					</font>
				</td>
				<td width="55%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Product</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Price Each</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Total</b>
					</font>
				</td>

			</tr>' .
			<?php

			while($row = mysql_fetch_array($result))
			&#123;
				// Increment the total cost of all items
				$totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
			$unitCost = ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
				?> . '
					<tr>
						<td width="15%" height="25">
							<font face="verdana" size="1" color="black">
								
								' . <?
								echo $row&#1111;"qty"];
								?> . '
								
							</font>
						</td>
						<td width="55%" height="25">
							<font face="verdana" size="1" color="black">
								' . <?php echo $row&#1111;"itemName"]; ?>
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								£<?php echo number_format($row&#1111;"itemPrice"], 2, ".", ","); ?>
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								£<?php echo number_format($unitCost, 2, ".", ","); ?>
							</font>
						</td>

					</tr>
				<?php
			&#125;
			
			// Display the total
			?>

					<tr>
						<td width="100%" colspan="3">

						</td>
						<td width="30%" colspan="2">
							<font face="verdana" size="2" color="black">
								<b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b>
							</font>
						</td>
					</tr>
				</table>
As you can see, it contains all sorts of code snippets. These don't seem to be parsed when it is emailed, so is there any way of getting the output into an email?

Many thanks,

Lawrence
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Are escaping the " characters. Maybe there is an easier way, but for the little bit I know, it is the way I would do it.

Code: Select all

$message = "<table width="100%" cellspacing="0" cellpadding="0" border="0"> 
         <tr> 
            <td width="15%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                    <b>Qty</b> 
               </font> 
            </td> 
            <td width="55%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Product</b> 
               </font> 
            </td> 
            <td width="20%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Price Each</b> 
               </font> 
            </td> 
            <td width="20%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Total</b> 
               </font> 
            </td>";
 
 while($row = mysql_fetch_array($result)) 
         { 
            // Increment the total cost of all items 
            $totalCost += ($row["qty"] * $row["itemPrice"]); 
         $unitCost = ($row["qty"] * $row["itemPrice"]); 
           //and the rest
you may have to run the queries first and then add the results to the message.
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Thanks for this lloydie-t. I have tried this and am now using:

Code: Select all

<?php

	include("headers.php");
		
	switch($_GET&#1111;"action"])
	&#123;
		case "add_item":
		&#123;
			AddItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "update_item":
		&#123;
			UpdateItem($_GET&#1111;"id"], $_GET&#1111;"qty"]);
			ShowCart();
			break;
		&#125;
		case "remove_item":
		&#123;
			RemoveItem($_GET&#1111;"id"]);
			ShowCart();
			break;
		&#125;
		default:
		&#123;
			ShowCart();
		&#125;
	&#125;
	
	function ShowCart()
	&#123;
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		global $dbServer, $dbUser, $dbPass, $dbName;

		// Get a connection to the database
		$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
		
		$totalCost = 0;
		$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
		?>
		<html>
		<head>
		<title> Your Shopping Cart </title>
		<script language="JavaScript">
		
			function UpdateQty(item)
			&#123;
				itemId = item.name;
				newQty = item.options&#1111;item.selectedIndex].text;
				
				document.location.href = 'basket.php?action=update_item&id='+itemId+'&qty='+newQty;
			&#125;
		
		</script>
		</head>
<?
include("top.php");
?>
		<h1>Your Basket</h1>
		<form name="frmCart" method="get">

		
<?
while($row = mysql_fetch_array($result))
			&#123;
				// Increment the total cost of all items
				$totalCost += ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
			$unitCost = ($row&#1111;"qty"] * $row&#1111;"itemPrice"]);
$qty = $row&#1111;"qty"];
$itemName = $row&#1111;"itemName"];
$price = number_format($row&#1111;"itemPrice"], 2, ".", ",");
$unitc = number_format($unitCost, 2, ".", ",");
$totalc = number_format($totalCost, 2, ".", ",");
&#125;

$message = "<table width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="15%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						&nbsp;&nbsp;<b>Qty</b>
					</font>
				</td>
				<td width="55%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Product</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Price Each</b>
					</font>
				</td>
				<td width="20%" height="25" bgcolor="#99cc99">
					<font face="verdana" size="1" color="#000000">
						<b>Total</b>
					</font>
				</td>

			</tr>
			
					<tr>
						<td width="15%" height="25">
							<font face="verdana" size="1" color="black">
								
						
								$qty
								
								
							</font>
						</td>
						<td width="55%" height="25">
							<font face="verdana" size="1" color="black">
								$itemName
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								£$price
							</font>
						</td>
						<td width="20%" height="25">
							<font face="verdana" size="1" color="black">
								£$unitc
							</font>
						</td>

					</tr>


					<tr>
						<td width="100%" colspan="3">

						</td>
						<td width="30%" colspan="2">
							<font face="verdana" size="2" color="black">
								<b>Total: £$totalc</b>
							</font>
						</td>
					</tr>
				</table>";
				
		
 
mail('info@larrytech.biz', 'Subject', 
    $message, 
    "To: The Receiver <recipient@some.net>\n" . 
    "From: The Sender <sender@some.net>\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-type: text/html; charset=iso-8859-1"); 
?>
		
		
				</form>
<a href="sendorder.php">Submit</a>
			<?php
	&#125;
	include("bottom.php");
?>
Now, this works (sort of) and I get an email, but only the last item selected is shown. I have a feeling that this is to do with the while statement and only doing one loop, but I am not sure how to get it to work total.

Thanks again!

Lawrence
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

You may have to put the queries back in $message or find some way of getting the array results to print out.
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Yes, I think so. Does anybody know how I would do this? I have tried having the query in $message, but it prints as text. Is there a special way of doing it?

Thanks a lot for your help!

Lawrence
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

  • just some things you should think about
  • include("headers.php");
    where is this file and therefor where can this file be included?
  • $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
    that implies that everyone could connect from everywhere to your database and you provided the login/password to everyone.
  • does your neighbour have php installed? If not what executes the script if he/she receives a mail from you?
client-side javascript e.g. works because many people have it installed on their box (shipping with their browser). But if they use lynx or disable javascript or ... it does not.
There is a client-side php-plugin called ActivePHP but who has it? ;)
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

I think that I need to get the query to output as HTML before being placed in the email. This would remove the need for a local parser. Is this what you meant? Ideally, the email should jst contain HTML, no PHP, but I don't know how this can be achieved with the query.
Lawrence
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

something like

Code: Select all

print "<td width="55%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        $itemName 
                     </font> 
                  </td>"; 
                  print "<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$price 
                     </font> 
                  </td>"; 
                  print"<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$unitc 
                     </font> 
                  </td>";
might work, might not
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Where would this go? Is this a possible solution to the query?

I can't find a way of incorporating the query into the $message, so I was wondering if the query would work if integrated into the mail() section...
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

You could try:

Code: Select all

<?php 

   include("headers.php"); 
       
   switch($_GET["action"]) 
   { 
      case "add_item": 
      { 
         AddItem($_GET["id"], $_GET["qty"]); 
         ShowCart(); 
         break; 
      } 
      case "update_item": 
      { 
         UpdateItem($_GET["id"], $_GET["qty"]); 
         ShowCart(); 
         break; 
      } 
      case "remove_item": 
      { 
         RemoveItem($_GET["id"]); 
         ShowCart(); 
         break; 
      } 
      default: 
      { 
         ShowCart(); 
      } 
   } 
    
   function ShowCart() 
   { 
      // Gets each item from the cart table and display them in 
      // a tabulated format, as well as a final total for the cart 
       
      global $dbServer, $dbUser, $dbPass, $dbName; 

      // Get a connection to the database 
      $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); 
       
      $totalCost = 0; 
      $result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc"); 
      ?> 
      <html> 
      <head> 
      <title> Your Shopping Cart </title> 
      <script language="JavaScript"> 
       
         function UpdateQty(item) 
         { 
            itemId = item.name; 
            newQty = item.options[item.selectedIndex].text; 
             
            document.location.href = 'basket.php?action=update_item&id='+itemId+'&qty='+newQty; 
         } 
       
      </script> 
      </head> 
<? 
include("top.php"); 
?> 
      <h1>Your Basket</h1> 
      <form name="frmCart" method="get"> 

       
<? 
while($row = mysql_fetch_array($result)) 
         { 
            // Increment the total cost of all items 
            $totalCost += ($row["qty"] * $row["itemPrice"]); 
         $unitCost = ($row["qty"] * $row["itemPrice"]); 
$qty = $row["qty"]; 
$itemName = $row["itemName"]; 
$price = number_format($row["itemPrice"], 2, ".", ","); 
$unitc = number_format($unitCost, 2, ".", ","); 
$totalc = number_format($totalCost, 2, ".", ","); 
} 

$message = "<table width="100%" cellspacing="0" cellpadding="0" border="0"> 
         <tr> 
            <td width="15%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                    <b>Qty</b> 
               </font> 
            </td> 
            <td width="55%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Product</b> 
               </font> 
            </td> 
            <td width="20%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Price Each</b> 
               </font> 
            </td> 
            <td width="20%" height="25" bgcolor="#99cc99"> 
               <font face="verdana" size="1" color="#000000"> 
                  <b>Total</b> 
               </font> 
            </td> 

         </tr> 
          
               <tr> 
                  <td width="15%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                         
                   
                        $qty 
                         
                         
                     </font> 
                  </td> 
                    print "<td width="55%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        $itemName 
                     </font> 
                  </td>"; 
                  print "<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$price 
                     </font> 
                  </td>"; 
                  print"<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$unitc 
                     </font> 
                  </td>"; 

               </tr> 


               <tr> 
                  <td width="100%" colspan="3"> 

                  </td> 
                  <td width="30%" colspan="2"> 
                     <font face="verdana" size="2" color="black"> 
                        <b>Total: £$totalc</b> 
                     </font> 
                  </td> 
               </tr> 
            </table>"; 
             
       

mail('info@larrytech.biz', 'Subject', 
    $message, 
    "To: The Receiver <recipient@some.net>\n" . 
    "From: The Sender <sender@some.net>\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-type: text/html; charset=iso-8859-1"); 
?> 
       
       
            </form> 
<a href="sendorder.php">Submit</a> 
         <?php 
   } 
   include("bottom.php");
this is getting a bit tricky for me as I have never done anything like this, but give it a try. As I said might work, might not, might not be far off.
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

check the syntax, as phpdn has taken on itself to remove some of the "\"
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

I get

Code: Select all

Parse error: parse error in /home/h/e/herbsandspices/public_html/dev/sendorder.php on line 114
Which did have a \ removed but which now reads

Code: Select all

print "<td width="55%" height="25">
So I don't know what it doesn't like about that...
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

what is on line 114
ignore, did'nt read last message properly
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

you cant put a print inside a string:

Code: Select all

$message = "<table width="100%" cellspacing="0" cellpadding="0" border="0">
         <tr>
            <td width="15%" height="25" bgcolor="#99cc99">
               <font face="verdana" size="1" color="#000000">
                    <b>Qty</b>
               </font>
            </td>
            <td width="55%" height="25" bgcolor="#99cc99">
               <font face="verdana" size="1" color="#000000">
                  <b>Product</b>
               </font>
            </td>
            <td width="20%" height="25" bgcolor="#99cc99">
               <font face="verdana" size="1" color="#000000">
                  <b>Price Each</b>
               </font>
            </td>
            <td width="20%" height="25" bgcolor="#99cc99">
               <font face="verdana" size="1" color="#000000">
                  <b>Total</b>
               </font>
            </td>

         </tr>
         
               <tr>
                  <td width="15%" height="25">
                     <font face="verdana" size="1" color="black">
                         
                   
                        $qty
                         
                         
                     </font>
                  </td>
                    print "<td width="55%" height="25">
                     <font face="verdana" size="1" color="black">
                        $itemName
                     </font>
                  </td>";
if you want to do something like that use eval
or if you want to use print's then use output buffering
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

forhot to print the <tr> try:

Code: Select all

$message. =  print "<tr>"; 
                  print "<td width="15%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                          $qty 
                   </font> 
                  </td>"; 
                    print "<td width="55%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        $itemName 
                     </font> 
                  </td>"; 
                  print "<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$price 
                     </font> 
                  </td>"; 
                  print"<td width="20%" height="25"> 
                     <font face="verdana" size="1" color="black"> 
                        £$unitc 
                     </font> 
                  </td>"; 

               print "</tr>";
not sure what else to do. check "\". You will also have to take this outside the initial $message string and then continue. Thanks for the pointer on that.
Post Reply