PHP Mail function sending 2 copies

Discussion in 'PHP' started by Kain, Aug 8, 2007.

  1. #1
    I have a form that is sending 2 copies of the email when clicked, one with all the data and another with no data added.

    Can anyone see an error in my code or have an idea why this is happening?

    <?php


    $to ="me@me.me";
    $subject ="Contact Form";

    $surname = $_POST['surname'];
    $firstname = $_POST['firstname'];
    $phone = $_POST['phone'];
    $Mobile =$_POST['Mobile'];
    $address = $_POST['address'];
    $address2 = $_POST['address2'];
    $email = $_POST['email'];


    $message="Below is the data submitted from your form \n

    SurName: $surname \n
    FirstName: $firstname \n
    Phone: $phone \n
    Mobile: $Mobile \n
    Address: $address \n
    $address2 \n


    ";
    mail($to, $subject, $message, "From: $email\r\nReply-To: $email\r\n");
    ?>
     
    Kain, Aug 8, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is the form in another script or in the same one?
     
    ecentricNick, Aug 8, 2007 IP
  3. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #3
    quick: are you using any loop or something like that? :)

    My 2 cents,
    Jakomo
     
    jakomo, Aug 8, 2007 IP
  4. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The form is in a separate html file.

    I removed any loops that were in the script and just kept the part that sends mail to test it and it still does it.
     
    Kain, Aug 8, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I did a bit of searching around on this one and it appears a common problem with very little information on a solution.

    However, if you have a very poorly performant system, or are using a busy shared host, it may be due to this...

    http://www.greatcircle.com/lists/majordomo-users/mhonarc/majordomo-users.200106/msg00130.html

    Something to do with messages sitting in the queue so long they get picked up by the process again or by another parallel mail process?

    If you have console access - try using sendmail raw from the console rather than through php. Also check the server mail logs to see if there are two copies or just one recorded in the there.
     
    ecentricNick, Aug 9, 2007 IP
  6. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for that. The server isn't that busy, I just upgraded it.

    I'll look into what you suggested.

    Thanks again
     
    Kain, Aug 9, 2007 IP
  7. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I found a solution to this, thought I'd post it in case anyone has the same problem.

    I think the problem was caused by a validation javascript sending the form twice, once without parameters. (Not sure about this bit though).

    To solve it I put in this code

    foreach($HTTP_POST_VARS as $key=> $val) {

    $count ++;
    }

    if($count >5){ // set this to the minimum required parameters for a successful send
    mail($to, $subject, $message, "From: $email\r\nReply-To: $email\r\n");
    }
     
    Kain, Aug 9, 2007 IP