Aide code envoi de formulaire HTML + PHP
BMPCreated with Sketch.BMPZIPCreated with Sketch.ZIPXLSCreated with Sketch.XLSTXTCreated with Sketch.TXTPPTCreated with Sketch.PPTPNGCreated with Sketch.PNGPDFCreated with Sketch.PDFJPGCreated with Sketch.JPGGIFCreated with Sketch.GIFDOCCreated with Sketch.DOC Error Created with Sketch.
Question

Aide code envoi de formulaire HTML + PHP

by
Gaëlle Hic
Created on 2025-10-15 07:31:14 (edited on 2025-10-15 10:27:38) in E-mails et solutions Office

bonjour, j'ai un soucis apparemment de code pour l'envoi de mail : j'ai fait un formulaire, HTML et PHP or quand je fais l'envoi cela ne fonctionne pas j'ai contacter OVH qui m'a renvoyé ici pour une aide.  le code d'essai d'envoi de mail fonctionne, mais pas mon formulaire. 

voici le code test qui fonctionne qui montre que l'envoi de mail fonctionne  : 

<?php
$to = 'pison.jessica@gmail.com';  // Ton adresse mail de test
$subject = 'Test';
$message = 'Ceci est un test';
$headers = 'From: site.cowboy.rhythm33@cowboyrhythmgironde.com' . "\r\n" .
           'Reply-To: site.cowboy.rhythm33@cowboyrhythmgironde.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $message, $headers)) {
    echo 'Mail envoyé avec succès';
} else {
    echo 'Échec de l\'envoi du mail';
}
?>

 

Mon Code HTML : 

<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8">
  <title>Contact / Support</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #fff;
      padding: 40px;
    }
    form {
      max-width: 400px;
      margin: 0 auto;
      display: flex;
      flex-direction: column;
    }
    label {
      margin-bottom: 5px;
      font-weight: bold;
    }
    input[type="text"],
    input[type="email"],
    textarea {
      width: 100%;
      padding: 10px;
      font-size: 1em;
      border: 1px solid #ccc;
      border-radius: 4px;
      margin-bottom: 20px;
      box-sizing: border-box;
    }
    textarea {
      resize: vertical;
      min-height: 120px;
    }
    button {
      padding: 12px;
      font-size: 1em;
      background-color: #007BFF;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>

  <h1 style="text-align: center;">Formulaire de contact</h1>

  <form action="send_mail.php" method="POST">
    <label for="name">Nom :</label>
    <input type="text" id="name" name="name" required>

    <label for="email">Email :</label>
    <input type="email" id="email" name="email" required>

    <label for="message">Message :</label>
    <textarea id="message" name="message" required></textarea>

    <button type="submit">Envoyer</button>
  </form>

</body>
</html>

 

 

code PHP : 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $name    = htmlspecialchars(trim($_POST["name"] ?? ''));
    $email   = filter_var(trim($_POST["email"] ?? ''), FILTER_SANITIZE_EMAIL);
    $message = htmlspecialchars(trim($_POST["message"] ?? ''));

    if (empty($name) || empty($email) || empty($message)) {
        echo "Tous les champs sont obligatoires.";
        exit;
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Adresse email invalide.";
        exit;
    }

    $to      = "site.cowboy.rhythm33@cowboyrhythmgironde.com";
    $subject = "Nouveau message du formulaire de contact";
    $body    = "Nom : $name\n";
    $body   .= "Email : $email\n\n";
    $body   .= "Message :\n$message\n";

    $headers = "From: site.cowboy.rhythm33@cowboyrhythmgironde.com\r\n";
    $headers .= "Reply-To: $email\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n";

    if (mail($to, $subject, $body, $headers)) {
        echo "Merci, votre message a bien été envoyé.";
    } else {
        echo "Une erreur est survenue lors de l'envoi du message.";
    }

} else {
    echo "Accès non autorisé.";
}
?>

 

 

pouvez-vous m'aider ? 

 

Je vous remercie 


1 Reply ( Latest reply on 2025-10-15 12:46:02 by
Gaëlle Hic
)

Bonjour,

Tu devrais plutôt demander ça sur un Forum PHP type https://www.developpez.net/forums/f443/php/

J'ai regardé très rapidement ton code sans trouver d'erreur évidente.
Cela fait longtemps que je confie l'envoi des mails à une lib spécialisée https://github.com/PHPMailer/PHPMailer

N’oublie surtout pas de protéger ton formulaire contre les robots avant de proder ça ! 

je vous remercie 

je vais aller regarder sur le site donner. 

Cordialement