• March 13, 2023
    why this code submit 1 time but sent 2 mail duplicate ? :
        // send email
        $name = $_POST['name'];
        $topic = $_POST['topic'];
        $detail = $_POST['detail'];
        $tel = $_POST['tel'];
        $email = $_POST['email'];
        $to = "mail@mail.com";
        $subject = "New form submission";
        $message = "Name: " . $name . "\n" . "Topic: " . $topic . "\n" . "Detail: " . $detail . "\n" . "Tel: " . $tel . "\n" . "Email: " . $email;
        $headers = "From: " . $email;
        mail($to, $subject, $message, $headers);
    
    	$headers = "From: " . $email . "\r\n" .
               "Reply-To: " . $email . "\r\n" .
               "X-Mailer: PHP/" . phpversion();
        
        if(mail($to, $subject, $message, $headers)) {
            // set session variables
            $_SESSION['last_submit_time'] = time();
            $_SESSION['submit_time'] = time();
            $_SESSION['page_load_time'] = time();
            $_SESSION['form_submitted'] = true;		
        } else {
            echo "Failed to send email. Please try again later.";
        }
    }
    ...

    The code is sending two emails because the mail() function is being called twice:
    once before the $headers variable is set, and once after.
    The first email does not have the $headers variable set, so it will use the default headers.
    The second email has the $headers variable set with the From, Reply-To, and X-Mailer headers.

    To fix the issue, remove the first call to mail() and keep only the second one that includes the $headers variable. This way, only one email will be sent, and it will use the custom headers:

    // send email
    $name = $_POST['name'];
    $topic = $_POST['topic'];
    $detail = $_POST['detail'];
    $tel = $_POST['tel'];
    $email = $_POST['email'];
    $to = "mail@mail.com";
    $subject = "New form submission";
    $message = "Name: " . $name . "\n" . "Topic: " . $topic . "\n" . "Detail: " . $detail . "\n" . "Tel: " . $tel . "\n" . "Email: " . $email;
    
    $headers = "From: " . $email . "\r\n" .
           "Reply-To: " . $email . "\r\n" .
           "X-Mailer: PHP/" . phpversion();
        
    if(mail($to, $subject, $message, $headers)) {
        // set session variables
        $_SESSION['last_submit_time'] = time();
        $_SESSION['submit_time'] = time();
        $_SESSION['page_load_time'] = time();
        $_SESSION['form_submitted'] = true;		
    } else {
        echo "Failed to send email. Please try again later.";
    }
    

    This will ensure that only one email is sent per form submission.



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories


Uncategorized