problem sending mail to customers upon cart checkout

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
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

problem sending mail to customers upon cart checkout

Post by adsegzy »

Hello there,
I have an online foods store where members can pick what they want to buy and submit the cart content when done. below is the script

Code: Select all


$cartOutput =="";
$cartTotal =="";

if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1){
	$cartOutput = "<p>&nbsp;</p><p><h2 align='center'>Your Shopping Cart is empty.</p>&nbsp;<p></p></h2>";
}else{
		
		$i = 0;
		foreach($_SESSION["cart_array"] as $each_item){
			$number++;
			$item_id = $each_item["item_id"];
			$sql = mysql_query("SELECT * FROM products WHERE hydee='$item_id' LIMIT 1");
			$listing = 1;
			while($roll=mysql_fetch_array($sql)){
			$id=stripslashes($roll[hydee]);
			$pid=stripslashes($roll[phydee]);
			$product=stripslashes($roll[product]);
			$type=stripslashes($roll[type]);
			$name=stripslashes($roll[name]);
			$facts=stripslashes($roll[facts]);
			$detail=stripslashes($roll[detail]);
			$ingredients=stripslashes($roll[ingredients]);
			$how=stripslashes($roll[how]);
			$price=stripslashes($roll[price]);
			$qty=stripslashes($roll[qty]);
			}
			$totalPrice2 = $price * $each_item['quantity']; //price times quantity.
			
			$cartTotal = $totalPrice2 + $cartTotal; //sum up all the prices in the cart to give grand amount.

			//putting all prices into naira format.
			$price = "#".number_format($price, '00','.',',').".00";
			$totalPrice = "#".number_format($totalPrice2, '00','.',',').".00";
			
			$get_imgs = mysql_query("SELECT * FROM product_imgs WHERE phydee='$pid' ORDER BY hydee asc LIMIT 1");
			$rows = mysql_fetch_array($get_imgs);{
			$spix = $rows[spix];
			$bpix = $rows[bpix];
			
			if($bpix!=""){
			$pix = "adminpanel/imgs/products/$spix";
			list($width, $height, $type, $attr) = getimagesize($pix); 
			
			if($width > $height) $size='100px';
			else{
			$new = 100 * $width; 
			$size = $new / $height; 
			$size = $size."px";}}}
			
			if($type == 'Equipment') $img = "<img src='adminpanel/imgs/products/$facts' width='90' class='img_border' />";
			elseif($spix!="") $img = "<img src='adminpanel/imgs/products/$spix' width='$size' class='img_border' />";
			else $img = "<img src='images/product.png' width='80' class='img_border' />";
			
			//location of product/equipment detail by segun
			if($type == 'Equipment') $location = "equipment_detail.php?id=$id";
			else $location = "product_detail.php?id=$id";
			
			//dynamic rows assembly
			$cartOutput.="<tr>";
			$cartOutput.="<td align='center' valign='top' style='border-bottom:1px solid #ccc'>".$number."</td>";
			$cartOutput.="<td align='center' valign='top' style='border-bottom:1px solid #ccc'><a href='$location'>$img<br/>".$name."</a></td>";
			$cartOutput.="<td align='centre' valign='middle' style='border-bottom:1px solid #ccc'>".$price."</td>";
			$cartOutput.="<td align='center' valign='middle' style='border-bottom:1px solid #ccc'>
			<form action='cart.php' method='post'>
			<input name='quantity' type='text' size='1' maxlength='2' value='".$each_item['quantity']."' />
			<input name='adjustBtn".$item_id."' type='submit' value='Adjust' />
			<input name='item_to_adjust' type='hidden' value='".$item_id."' />
			</form></td>";
			//$cartOutput.="<td align='center' valign='middle' style='border-bottom:1px solid #ccc'>".$each_item['quantity']."</td>";
			$cartOutput.="<td align='centre' valign='middle' style='border-bottom:1px solid #ccc'>".$totalPrice."</td>";
			$cartOutput.="<td align='right' valign='middle' style='border-bottom:1px solid #ccc'><form action='cart.php' method='post'><input name='deleteBtn".$item_id."' type='submit' value='Remove' /><input name='index_to_remove' type='hidden' value='".$i."' /></form></td>";
			$cartOutput.="</tr>";
			$i++;
			
					//submit order into the database if the member click on SUBMIT MY ORDER in the Cart Page
					$order = $_GET[order];
					if($order!=""){
					$order_id = $mid.$newunix;
					$quantity = $each_item['quantity'];
					$submit_order = mysql_query("INSERT INTO purchases (member_id, order_id, product, qty, price, totalamount, date, time, ip) VALUES ('$mid', '$order_id', '$id', '$quantity', '$totalPrice2', '$cartTotal', '$date', '$time', '$ip')");
					}
					
		//MAIL POSITION 1 (FOR SENDING MAIL TO THE MEMBER ON SUBMITTING THE CART)
		}

                           //MAIL POSITION 2 (FOR SENDING MAIL TO THE MEMBER ON SUBMITTING THE CART)
		
}
mail to be sent upon submitting the cart.

Code: Select all

			// To send HTML mail, the Content-type header must be set
					if($order!=""){
					
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= 'From: Food & Beverages Ltd <info@foods.com>' . "\r\n";
					
					$to = $memail;
					$subject = "Your Order is received (Orider ID: $order_id)";
					
					//message to be sent begins
					$message = "Hello ".$firstname."<br/><br/>Thank you for the interest in our Products. Your Order ID is $order_id and your order is as below<br/><br/>".$mailCart."<br/><br/>Thank you";
					
					//message to be sent ends
					
					mail($to, $subject, $message, $headers);
					
					header("location:submit_order.php");
					}

The problem is that, assuming the customer purchased 3 items, if the mail code is at MAIL POSITION 1, it will send the mail 3 times to the customer's email. the 1st mail with 1 item, the 2nd mail with 2 items and the 3rd with 3 item and so on. But if the mail code is placed at MAIL POSITION 2, it will send 1 email to the customers but with only 1 item and not all items submitted.

Please what is the right thing for me to do.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: problem sending mail to customers upon cart checkout

Post by social_experiment »

adsegzy wrote:Please what is the right thing for me to do.
What should happen? :)

Position 1 is inside the foreach loop which is why mails is sent for each item.
Position 2 is outside this. Hence the one email.

You already have all items in $_SESSION['cart_array'] so use those values to generate the items you want to display in the mail. Inside the foreach loop you could create an array ($boughtItems) and then create a msg with the array and send it.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply