0

I created a small form with 2 textareas to send emails. Everything works, except that when I reload my page, an email is sent without my clicking on "submit". I don't understand. Thank you.

myFunction.php:

public function sendMail($sujet,$message,$expediteur) {

    require_once('../SRC/Connexion/db_connect.php');
    $pdo = DbConnect();
    $sql = "SELECT email FROM players";
    $query = $pdo->prepare($sql);
    $query->execute();
    $result = $query->fetchAll(\PDO::FETCH_ASSOC)

    if (isset($_POST['sujet'])) {
    
        $mail = new PHPMailer(true);
                
        try {
            //Server settings
            $mail->SMTPDebug = false;                      
            $mail->isSMTP();                                            
            $mail->Host       = 'mail.gmx.com';                     
            $mail->SMTPAuth   = true;                                   
            $mail->Username   = 'foo@gmx.fr';                     
            $mail->Password   = 'xyz';                               
            $mail->SMTPSecure = //PHPMailer::ENCRYPTION_SMTPS;            
            $mail->Port       = 587;                                    

            $mail->setFrom('foo@gmx.fr', 'Ratea Tennis Club');
            $mail->addAddress('me@gmx.fr', 'Patrice');

            $mail->isHTML(true);                                  
            $mail->Subject = $sujet;
            $mail->Body    = $message;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        
            $mail->send();

            unset($_POST['sujet']);
            unset($_POST['message']);

        } catch (Exception $e) {
            '<div class="alert alert-danger" role="alert">Message non envoyé</div>. Erreur: {$mail->ErrorInfo}';
        }

}

  • Without seeing the actual code you're using, anything we say will be little better than guesswork. – Tangentially Perpendicular Jul 10 '21 at 04:18
  • Can you also submit your HTML file to send the email? I suspect you are not setting your submit function to false to prevent recurring submissions upon page reloads. – Ezani Jul 10 '21 at 04:46
  • Web usage 101: when you resubmit the POST request, the form elements will be resubmitted as well. `unset()` does nothing to clear the browsers state. – mario Jul 10 '21 at 04:49

0 Answers0