PHP mail function help needed

Hi all,
My name is Julian, I am new to this forum and I send you all greetings from really down here…New Zealand :slight_smile:
I have an issue with my php mail function, I have asked my provider but they told me that they can`t help with coding.
I have a page, success.php which does not output to screen. A successful credit card transaction is redirected here, the MySql database is queried 2 times to get details to insert in email and an email is sent out, at the moment to me, customer and website owner. Also the session is destroyed (will clear the shopping cart) and all ends with a redirection to the next page.
The issue I have is that all 3 emails arrive in 2 copies each. This will be not so important if I will not send a copy out to customer. I noticed that all my other mail functions (which do not query the database, from other pages) are send out in one copy each.
The code with comments is bellow if you can be bothered to have a look. Any suggestions are welcome.
Thank you in advance,

/* No output to screen is generated from this page. A succesfull credit card transaction is redirected here with customers email and order number attached to url
2 database queries are executed to obtain the products in order and customers details. The retrieved data is emailed to 3 addressess at the moment:
customer`s email, me for testing purpose and owners website. All emails arrive in 2 copies. The linked pages have no email functions
//database credentials
include ("../cp/kiaora.dora");
include ("../cp/db.dora");
//obtain customer details
$sql = "SELECT email, fname, lname, phone, street, suburb, city, postCode, country, fnameShip, lnameShip, streetShip, suburbShip, cityShip, postCodeShip, countryShip FROM customers WHERE email='" . $_GET['email'] . "'";
$result = mysql_query($sql) or die(mysql_error());
while ($results = mysql_fetch_array($result)) {
  $orderno = $results['orderno'];
  $email = $results['email'];
  $fname = $results['fname'];
  $lname = $results['lname'];
  $phone = $results['phone'];
  $street = $results['street'];
  $suburb = $results['suburb'];
  $city = $results['city'];
  $postCode = $results['postCode'];
  $country = $results['country'];
  $fnameShip = $results['fnameShip'];
  $lnameShip = $results['lnameShip'];
  $phoneShip = $results['phoneShip'];
  $streetShip = $results['streetShip'];
  $suburbShip = $results['suburbShip'];
  $cityShip = $results['cityShip'];
  $postCodeShip = $results['postCodeShip'];
  $countryShip = $results['countryShip'];
//start creating the email`s body
$orderItems = "Status: Payment received, thank you.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bill to&lt;/b&gt;&lt;br /&gt;$fname $lname&lt;br /&gt;Address: $street $suburb&lt;br /&gt;$city $postCode&lt;br /&gt;Country: $country&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Ship to&lt;/b&gt;&lt;br /&gt;$fnameShip $lnameShip&lt;br /&gt;Address: $streetShip $suburbShip&lt;br /&gt;$cityShip $postCodeShip&lt;br /&gt;Country: $countryShip&lt;br /&gt;&lt;br /&gt;";
//items in order query
$sql = "SELECT orderno, item_id, item_name, item_price, item_qty, total FROM shopping_cart WHERE orderno='" . $_GET['orderno'] . "'";
$result = mysql_query($sql) or die(mysql_error());
$orderItems .= "&lt;p&gt;Items in order&lt;/p&gt;&lt;table width=\\"90%\\" border=\\"0\\" cellpadding=\\"5\\"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Item&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Price&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Quantity&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=\\"3\\"&gt;&lt;hr/&gt;&lt;/td&gt;&lt;/tr&gt;";
while ($items = mysql_fetch_array($result)) {
  $orderno = $items['orderno'];
  $name = $items['item_name'];
  $price = $items['item_price'];
  $qty = $items['item_qty'];
  $total = $items['total'];
  //define shippingCharge to add it to email body if there is post charge
  //if total different price of all items together will set shipping charge to a message that $ 5 from total is shipping charge
  if($price!==$total){$shippingCharge="(Includes 5 \\$ shipping charge)";}
  //adding the order items to email and finishing the email
  $orderItems .= "&lt;tr&gt;&lt;td&gt;$name&lt;/td&gt;&lt;td&gt;$price NZD&lt;/td&gt;&lt;td&gt;$qty&lt;/td&gt;&lt;/tr&gt;";
}$orderItems .= "&lt;/table&gt;&lt;br /&gt;&lt;strong&gt;Total: $total \\$ $shippingCharge&lt;/strong&gt;&lt;p&gt;Your order will be shipped next business day.&lt;br /&gt;Any enquiries contact &lt;a href=\\" $orderno\\" title=\\"Contact us\\"&gt;;/a&gt;&lt;br /&gt;Phone: 07 849 3805 Please quote order number $orderno&lt;/p&gt;&lt;em&gt;The team at Everhealth Pharmacy&lt;/em&gt;";
//getting new zealand times
$date = date("l, F jS, Y");
$time = date("h:i A");
//email to customer
$to = $_GET['email'];
$subject = "Everlife Pharmacy ORDER NO. $orderno placed. Credit Card Order";
$message = $orderItems;
$headers = "From: [email][/email]\\r\
$headers .= "Content-type: text/html\\r\
mail($to, $subject, $message, $headers);
//email to developer
$to = "";
mail($to, $subject, $message, $headers);
//email to website owner
$to = "";
mail($to, $subject, $message, $headers);
//destroy session to clear the shopping card
unset ($_COOKIE["PHPSESSID"]);
setcookie("PHPSESSID", "", time() - 3600, "/");
//redirection to next page

If you’re getting two copies of each email, then either the page is loading a second time or your email system is CC’ing each other, which seems unlikely. Have you checked your server mail log to confirm that PHP is actually sending two copies of the email?

Your while loop ends before the mail is sent, so I don’t see how multiple copies would be sent from this snippet of code alone.

Just to be sure add " LIMIT 1" to the end of the $sql variable for obtaining customer details, cant quite figure out why there is a while loop in there at all…

and take a closer look at the page calling the mail script for a misconfigured event trigger.

Sure will add Limit 1 thanks …About the loop…What are you using to get just one result out of a MySql table? :slight_smile:

I usually find it’s helpful to echo the mail command to see exactly what’s happening:

echo "mail($to, $subject, $message, $headers)";

If you do that for every mail() you’ll be able to see if it’s sending twice or the problem is elsewhere. Also might be worth checking the full headers once you receive the email, to see if there are any clues there.

As for getting the result of one row only, just omit the while():

$row = mysql_fetch_array($result);

Hi Guys,
Thank you all for your help. Looks like the “double” is caused by the page calling the success.php page That one is modified by me from a sample given by the credit card company. Is functional but looks a bit messy at the moment. if anyone has time and mood to have a look at it let me know.
Anyway thank you all for your time, I really appreciate it.