PHP mail() sending emails wrong

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ParadoxBTC
    New Member
    • Oct 2014
    • 2

    PHP mail() sending emails wrong

    So I am trying to send an email to the server admin to notify new support ticket, and one to the client to tell them thank you for contacting us. Server admin email receves the info correct but the client email gets both messages in one email like what is showen below. What is the problem?

    ===========emai l=============
    Hello!

    Thank you for reaching out. We will be in contact with you as soon as possible!:

    Name:
    E-mail:
    Account-Number:
    Ticket Number: 814

    Message:
    Hello!

    Your contact form has been submitted by:

    Name:
    E-mail:
    Account-Number:
    IP:


    Message:
    Testing of the 2way mesage system

    End of message


    End of message


    ======CODE===== ====

    Code:
    <?php
    /* Set e-mail recipient */
    $myemail  = "";
    
    /* Get submitent ip address */
    $ip=$_SERVER['REMOTE_ADDR'];
    
    /* Check all form inputs using check_input function */
    $name      = check_input($_POST['name'], "Enter your name");
    $subject   = check_input($_POST['subject']);
    $email     = check_input($_POST['email']);
    $acctnum   = check_input($_POST['acctnum']);
    $message   = check_input($_POST['message'], "Write your comments");
    
    
    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
        show_error("E-mail address not valid");
    }
    
    /* Generate ticket number */
    $ticketnum = mt_rand(1,1000);
    
    /* Let's prepare the message for the e-mail */
    $message = "Hello!
    
    Your contact form has been submitted by:
    
    Name: $name
    E-mail: $email
    Account-Number: $acctnum
    IP: $ip
    
    
    Message:
    $message
    
    End of message
    ";
    
    /* Prepare the email to the client */
    $subject2 = "Ticket: $ticketnum";
    
    $message2 = "Hello!
    
    Thank you for reaching out. We will be in contact with you as soon as possible!:
    
    Name: $name
    E-mail: $email
    Account-Number: $acctnum
    Ticket Number: $ticketnum
    
    Message:
    $message
    
    End of message
    ";
    
    /* send emails */
    mail($myemail, $subject, $message);
    mail($email, $subject2, $message2);
    
    /* Redirect visitor to the thank you page */
    header('Location: ../thanks.html');
    exit();
    
    /* Functions we used */
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }
    
    function show_error($myError)
    {
    ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php
    exit();
    }
    ?>
    Last edited by Rabbit; Oct 24 '14, 03:20 PM. Reason: Please use [code] amd [/code] tags when posting code or formatted data. Personal info removed.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The problem is you overwrite the $message variable with the body of the first email. And then you go and insert that value into the second message.

    Comment

    • Exequiel
      Contributor
      • Jul 2012
      • 288

      #3
      right now are you using the local server or did you already uploaded on line? because there are some changes you must do local and online.

      Comment

      • ParadoxBTC
        New Member
        • Oct 2014
        • 2

        #4
        I fixed it! Rabbit was right, thanks guys.

        Comment

        Working...