Jump to content

How to approach problem with email


Lassie

Recommended Posts

I need to email a reciept of purchase in a cart application.

I store the product id and qty in a session variable $cart and then use 2 functions to generate the item, qty, price ,value.

This works fine for the display on screen, but I cant figure how to include this in an email to the customer.

ideally I would like the same information to display in the email.

Any ideas how to approach this would be helpful.

 

email

$to = "$email";
$subj = "test";
$mess = "<html>\n"
."<head>\n"
."<title>Test Mail</title>\n"
."</head>\n"
."<body>\n"
."This is an html email test<br />\n"
."<p><b>Your order has been processed. Please print this email as your reference</b></p>\n"
."<p>Your purchases were:-</p>\n" [u][b](Need to insert reciept here)[/b][/u]
."</body>\n</html>\n";
$mess = wordwrap($mess,70);

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 mail($to,$subj,$mess,$headers);

 

display cart function


function display_cart($cart, $change = true, $images = 1)
{
  // display items in shopping cart
  // optionally allow changes (true or false)
  // optionally include images (1 - yes, 0 - no)
  
  echo '<table border = 0 width = "700" cellspacing = 0 align="center">
        <form action = "show_cart.php" method = "post">
        <tr><th colspan = '. (4+$images) .' bgcolor="#cccccc">Item</th>
        <th bgcolor="#cccccc">Price</th><th bgcolor="#cccccc">Quantity</th>
        <th bgcolor="#cccccc">Total</th></tr>';

  //display each item as a table row
  foreach ($cart as $product_id => $qty)
  {
    $book = get_book_details($product_id);
    echo '<tr>';
    if($images ==true)
    {
      echo '<td align = left>';
      if (file_exists('images/$pix'))
      {
         /* display picture  */
    echo "<td><a href='./images/{$book['pix']}' border='0'>
        	<img src= './images/{$book['pix']}' border='0'
              width='100' height='80'></a></td>\n";
      }
      else
         echo ' ';
      echo '</td>';
    }
    echo '<td colspan= "3" align = "right">';
    
     echo '<a href = "show_book.php?product_id='.$product_id.'">'.$book['title'].'</a> by '.$book['author'];
    echo '</td><td align = "center">£'.number_format($book['price'], 2);
    echo '</td><td align = "center">';
    // if we allow changes, quantities are in text boxes
    if ($change == true)
      echo "<input type = 'text' name = \"$product_id\" value = \"$qty\" size = 3>";
    else
      echo $qty;
    echo '</td><td align = "center">£'.number_format($book['price']*$qty,2)."</td></tr>\n";
  }
  // display total row
  echo "<tr>
          <th colspan = ". (5+$images) ." bgcolor=\"#cccccc\"> 
          <th align = \"center\" bgcolor=\"#cccccc\"> 
              ".$_SESSION['items']."
          </th>
          <th align = \"center\" bgcolor=\"#cccccc\">
              £".number_format($_SESSION['total_price'], 2).
          '</th>
        </tr>';
  // display save change button
  if($change == true)
  {
    echo '<tr>
            <td colspan = '. (4+$images) .'> </td>
            <td align = "center">
              <input type = "hidden" name = "save" value = true>  
              <input type = "image" src = "images/save-changes.gif" 
                     border = 0 alt = "Save Changes">
            </td>
            <td> </td>
        </tr>';
  }
  echo '</form></table>';
}

 

get product detail function

function get_book_details($product_id)
{
  // query database for all details for a particular book
  if (!$product_id || $product_id=='')
     return false;
$connection = db_connect();    
   $query = "select * from products where product_id='$product_id'";
   $result = mysql_query($query);
   if (!$result)
     return false;
   $result = mysql_fetch_assoc($result);
   return $result;
}	    

Link to comment
https://forums.phpfreaks.com/topic/46724-how-to-approach-problem-with-email/
Share on other sites

in $mess just add those variables..

 

$mess = "<html>\n"

."<head>\n"

."<title>Test Mail</title>\n"

."</head>\n"

."<body>\n"

."This is an html email test<br />\n"

."<p><b>Your order has been processed. Please print this email as your reference</b></p>\n"

."<p>Your purchases were:-</p>\n" your variables go here..

."</body>\n</html>\n";

 

I have modified the email and it now works to a limited degree.

What it does not do is show multiples, only the last purchase.

Does my loop need to be in the email body?

Any help appreciated.

 

 foreach ($cart as $product_id => $qty)
  {
    $book = get_book_details($product_id);
    
  }
  	extract($book);
$to = "$email";
$subj = "test";
$mess = "<html>\n"
."<head>\n"
."<title>Test Mail</title>\n"
."</head>\n"
."<body>\n"
."This is an html email test<br />\n"
."<p><b>Your order has been processed. Please print this email as your reference</b></p>\n"
."<p>Your purchases were:-$title . by . $author </p>\n"
."</body>\n</html>\n";
$mess = wordwrap($mess,70);

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 mail($to,$subj,$mess,$headers);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.