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 : nom du fichier : support.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 : nom du fichier : send_mail.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
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 : nom du fichier : support.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 : nom du fichier : send_mail.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