SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Location
    Auckland New Zealand
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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
    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,
    Julian

    Code PHP:
    <?php
    /* 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
    */
    date_default_timezone_set("Pacific/Auckland");
    //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.<br /><br /><b>Bill to</b><br />$fname $lname<br />Address: $street $suburb<br />$city $postCode<br />Country: $country<br /><br /><br /><b>Ship to</b><br />$fnameShip $lnameShip<br />Address: $streetShip $suburbShip<br />$cityShip $postCodeShip<br />Country: $countryShip<br /><br />";
    //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 .= "<p>Items in order</p><table width=\"90%\" border=\"0\" cellpadding=\"5\"><tbody><tr><td><b>Item</b></td><td><b>Price</b></td><td><b>Quantity</b></td></tr><tr><td colspan=\"3\"><hr/></td></tr>";
    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
      $shippingCharge="";
      //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 .= "<tr><td>$name</td><td>$price NZD</td><td>$qty</td></tr>";
    }$orderItems .= "</table><br /><strong>Total: $total \$ $shippingCharge</strong><p>Your order will be shipped next business day.<br />Any enquiries contact <a href=\"mailto:orders@everhealthpharmacy.co.nz?Subject=Order $orderno\" title=\"Contact us\">orders@everhealthpharmacy.co.nz</a><br />Phone: 07 849 3805 Please quote order number $orderno</p><em>The team at Everhealth Pharmacy</em>";
    //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]orders@everhealthpharmacy.co.nz[/email]\r\n";
    $headers .= "Content-type: text/html\r\n";
    mail($to, $subject, $message, $headers);
    //email to developer
    $to = "julianstefan@focusdesign.biz";
    mail($to, $subject, $message, $headers);
    //email to website owner
    $to = "orders@everhealthpharmacy.co.nz";
    mail($to, $subject, $message, $headers);
    //destroy session to clear the shopping card
    unset ($_COOKIE["PHPSESSID"]);
    setcookie("PHPSESSID", "", time() - 3600, "/");
    //redirection to next page
    header("Location:http://www.everhealthpharmacy.co.nz?cc=1");
    ?>

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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.
    TAKE A WALK OUTSIDE YOUR MIND.

  3. #3
    SitePoint Zealot Sogo7's Avatar
    Join Date
    May 2011
    Posts
    129
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by julianStefan View Post
    all 3 emails arrive in 2 copies each
    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.
    Lovelogic.net Personal Projects Pit - Spammers welcome

  4. #4
    SitePoint Member
    Join Date
    Aug 2012
    Location
    Auckland New Zealand
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure will add Limit 1 thanks ....About the loop....What are you using to get just one result out of a MySql table?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually find it's helpful to echo the mail command to see exactly what's happening:

    PHP Code:
    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():

    PHP Code:
    $row mysql_fetch_array($result); 
    Martin.

  6. #6
    SitePoint Member
    Join Date
    Aug 2012
    Location
    Auckland New Zealand
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •