Jump to content

mail() sending 13 copies of the message...


peter_anderson

Recommended Posts

Hi there,

 

I'm working on a shopping script, and IPN now works (they should really document it better), but after using the sandbox, I've noticed that the system is sending 13 copies of the email.

 

I've stripped unnecessary code from below:

<?php
// stip
case "ipn":
	// read the post from PayPal system and add 'cmd'
	$req = 'cmd=_notify-validate';
	foreach ($_POST as $key => $value) {
		$value = urlencode(stripslashes($value));
		$req .= "&$key=$value";
	}
	// post back to PayPal system to validate
	$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
	$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
	// assign posted variables to local variables
	$item_name = $_POST['item_name'];
	$item_number = $_POST['item_number'];
	$payment_status = $_POST['payment_status'];
	$payment_amount = $_POST['mc_gross'];
	$payment_currency = $_POST['mc_currency'];
	$txn_id = $_POST['txn_id'];
	$receiver_email = $_POST['receiver_email'];
	$payer_email = $_POST['payer_email'];
	if(!isset($_POST['item_number'])){
		$item_number = $_POST['item_number1'];
	}
	if(!isset($_POST['mc_gross'])){
		$payment_amount = $_POST['mc_gross1'];
	}
	if(!$fp){
		// could not open the connection.  If loggin is on, the error message
		// will be in the log.
		$last_error = "fsockopen error no. $errnum: $errstr";
		exit($last_error);
	}else{
		fputs ($fp, $header . $req);
		while (!feof($fp)) {
			$res = fgets ($fp, 1024);
			if($res != "VERIFIED"){
				// check the payment_status is Completed
				if($payment_status != 'Completed'){
					$last_error = "Payment was not completed.";
					exit($last_error);
				}
				// get the ORDER ID and related data
				$query = "SELECT * FROM `orders` WHERE id='$item_number' LIMIT 1";
				$result = $sql->query($query);
				$row = $result->fetch_assoc();
				// check the Order ID exists
				if($result->num_rows != '1'){
					$last_error = "Invalid order ID: $item_number";
					exit($last_error);
				}
				// check that receiver_email is your Primary PayPal email
				if($receiver_email != $paypal_email){
					$last_error = "$receiver_email is not the correct PayPal email";
					exit($last_error);
				}
				// check that payment_amount is correct
				if($row['cost'] != $payment_amount){
					$cost = $row['cost'];
					$last_error = "The total paid was incorrect. We wanted $cost, but we received $payment_amount.";
					exit($last_error);
				} 
				// check that payment_amount is correct
				if($currency_c != $payment_currency){
					$last_error = "The the currency was WRONG. Customer paid in $payment_currency, we want $currency_c";
					exit($last_error);
				}
				// process payment
				// Client has successfully paid for the product 
				$product_id = $_POST['item_number'];
				// Get customer details
				$customer_email = $row['customer'];
				$query1 = "SELECT * FROM `customers` WHERE email='$customer_email' LIMIT 1";
				$result1 = $sql->query($query1);
				$row1 = $result1->fetch_assoc();
				// Loop through all the products ordered
				$cdate = date('j/M/Y');
				$query2 = "SELECT * FROM `purchases` WHERE date='$cdate' AND customer='$customer_email'";
				$result2 = $sql->query($query2);
				$summary = 'QTY		|		Product		|		Cost';
				while($row2 = $result2->fetch_assoc()){
					$prodid = $row2['prodid'];
					$query3 = "SELECT * FROM `products` WHERE id='$prodid'";
					$result3 = $sql->query($query3);
					$row3 = $result3->fetch_assoc();
					$qty = $row2['qty'];
					$title = $row3['title'];
					$cost = $row2['cost'];
					$summary .= "\n$qty		|		$title		|		$cost";
				}
				// Replace Tags
				$total = $row['cost'];
				$support_url = 'http://'.$_SERVER['SERVER_NAME'].'/'.$siteid.'/support';
				$download_url = 'http://'.$_SERVER['SERVER_NAME'].'/'.$siteid.'/myaccount';
				$tags = array("{first_name}","{last_name}","{payer_email}","{currency}","{currency_code}","{support_url}","{download_link}","{orderid}","{summary}","{product_price}");
				$vals = array($row1['firstname'],$row1['surname'],$customer_email,$currency,$currency_c,$support_url,$download_url,$row['id'],$summary,$total);
				// Email
				$subject = str_replace($tags,$vals,$email['subject']);
				$body    = str_replace($tags,$vals,$email['body']);	
				$sender = str_replace('{sender}',$email_add,$email['sender']);
				$headers = 'From: '.$sender.'';
				$body = str_replace('£','£',$body);
				// Send Email to the buyer
				mail($customer_email,$subject,$body,$headers);
				// Mark as PAID
				$product_id = $row['id'];
				$m_query = "UPDATE `orders` SET status='1' WHERE id='$product_id'";
				$sql->query($m_query);
			}else{
				$last_error = 'Payment was not verified';
			}
		}
		fclose ($fp);
	}
break;
// stip
?>

 

Any ideas whats causing it? Is it a PayPal bug, do you think?

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/197011-mail-sending-13-copies-of-the-message/
Share on other sites

I just looked real quick, but you know you're sending out the email in the while (!feof($fp)) loop, right? Perhaps that could be the problem.

 

I tried moving it to the bottom line (above the break;), but it's still sending 13 emails. It's strange.

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.