Php mail form sent but not received in email

Hi everyone
i’m having difficulties with my contact form because the message is sent successfully but i’m not getting any email
here is my form in html

<form action="send-mail.php" method="POST">
 <div class="col-md-6 col-sm-12 col-xs-12">

    <label>Prénom *</label>
    <input type="text" tabindex="1" value="" name="prenom"  title="Ce domaine ne doit pas être laissé vide." />
<br>
    <label>Email *</label>
    <input type="email" value=""  tabindex="3" name="email"  title="Ce domaine ne doit pas être laissé vide." />
<br>
<label>Age *</label>
    <input type="text" value=""  tabindex="6" name="age"  title="Ce domaine ne doit pas être laissé vide." />
   
    </div>
    
    <div class="col-md-6 col-sm-12 col-xs-12">
      
 <label>Nom *</label>
    <input type="text" value=""  tabindex="2" name="nom"  title="Ce domaine ne doit pas être laissé vide." /><br>
    <label>Téléphone *</label>
    <input type="text" value=""  tabindex="4"  name="tel"  title="Ce domaine ne doit pas être laissé vide." />
<br>
    <label>Code postal *</label>
    <input type="text" value=""  tabindex="7" name="zipcode"  title="Ce domaine ne doit pas être laissé vide." />

</div>
</form>

and here is my send-mail.php

<?php
	$mail_to = 'myemail@example.com'; // specify your email here

	// Assigning data from $_POST array to variables
	$name = $_POST['nom'];
	$prename = $_POST['prenom'];
	$email = $_POST['email'];
	$tel = $_POST['tel'];
	$zipcode = $_POST['zipcode'];
	$age = $_POST['age'];
	$message = $_POST['message'];
	

	$emtel = $_POST['emtel'];
	$consulter = implode(' | ', $_POST['consulter']);
	$week = implode(' | ', $_POST['week']);
	
	
	// Construct subject of the email
	$subject = 'website email ' . $name;

	// Construct email body
	$body_message = 'From: ' . $name . " - " . $prename . "\r\n";
	$body_message .= 'E-mail: ' . $email . "\r\n";
	$body_message .= 'Phone: ' . $tel . "\r\n";
	$body_message .= 'zipcode: ' . $zipcode . "\r\n";
	$body_message .= 'age: ' . $age . "\r\n";
	$body_message .= 'Avez-vous des questions particulières: ' . $message. "\r\n";
	
	$body_message .= 'Vos disponibilités de jours: ' . $week.  "\r\n";
	$body_message .= 'Vous voulez consulter pour : ' . $consulter . "\r\n";
	$body_message .= ' Souhaitez-vous être recontacté par: ' . $emtel . "\r\n";

	// Construct headers of the message
	$headers = 'From: ' . $mail_from . "\r\n";
	$headers .= 'Reply-To: ' . $mail_from . "\r\n";

	$mail_sent = mail($mail_to, $subject, $body_message, $headers);

	if ($mail_sent == true){ ?>
		<script language="javascript" type="text/javascript">
		alert('Merci pour le message. Nous vous contacterons sous peu.');
		window.location = 'nous contacter.html';
		</script>
	<?php } else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message non envoyé. S'il vous plaît, informez l'administrateur du site admin@admin.com');
        window.location = 'nous contacter.html';
    </script>
	<?php
	}
?>

so what is it that i’m doing it wrong ?

Hey @nourdarani, welcome to the forums.

This is a common problem, but no one can help you without seeing your code.

Are you using PHP’s mail() function, PHPMailer or SwiftMailer? If the former, does your host allow you to send mail this way?

Hi @ gandalf458
i just edit my post and i included the html and mail php code i have so can you have a look and tell me what is wrong please ?

This is most likely what is wrong.

Verify with your hosting company that they support using php’s mail() function, or else ask them what mailer settings need to be applied.

You may also be having a problem here:

// Construct headers of the message
$headers = 'From: ' . $mail_from . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";

in that I cannot see where you define $mail_from at all. This means that you are requiring your mail server to allow emails to be sent from a blank email address, which is unusual for a commercial mail server to allow.

You should send the email “from” an address on your domain, that your email server is configured to send from - you should not send it “from” the email address that the user types into the form. Your use of reply-to, however, is fine. Allowing emails to be sent “from” any address the user types in means that the mail server is allowing open relay, which is generally frowned upon.

1 Like