Loïc Filleur Loïc Filleur - 2 months ago 9
PHP Question

PHP multiple fields contact form issue

I've got a problem with a php contact form (yes, classic issue i know).

I'm pretty new in PHP and i dont find what is broken in my code.

Here is the PHP :



<?php

// Email address verification
function isEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

if($_POST) {

// Enter the email where you want to receive the message
$emailTo = 'contact@must-assurances.com';

$clientEmail = addslashes(trim($_POST['email']));
$name = addslashes(trim($_POST['name']));
$entreprise = addslashes(trim($_POST['entreprise']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$jesuis = addslashes(trim($_POST['jesuis']));
$tel = addslashes(trim($_POST['tel']));
$siren = addslashes(trim($_POST['siren']));

$array = array('emailMessage' => '', 'entrepriseMessage' => '', 'messageMessage' => '', 'antispamMessage' => '', 'jesuisMessage' =>'', 'nameMessage' =>'', 'telMessage' =>'', 'sirenMessage' =>'' );

if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Email invalide';
}
if($name == '') {
$array['nameMessage'] = 'Nom manquant';
}
if($entreprise == '') {
$array['entrepriseMessage'] = 'Entreprise manquante';
}
if($tel == '') {
$array['telMessage'] = 'Numéro de téléphone manquant';
}
if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if($message == '') {
$array['messageMessage'] = 'Message manquant';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Mauvaise réponse';
}
if($jesuis == '') {
$array['jesuisMessage'] = 'Merci de préciser si vous êtes une entreprise, un courtier, un investisseur, un professionnel ou autre';
}
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail .
$headers = array("Content-Type: text/html; charset=UTF-8");
mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}
echo json_encode($array);
}

?>





And here the HTML snippet :



<form role="form" action="contact-mia.php" method="post" accept-charset='UTF-8'>
<div class="form-group col-md-3">
<label for="name">Votre nom :</label>
<input type="text" name="name" placeholder="Votre nom..." class="contact-name form-control" id="name">
</div>
<div class="form-group col-md-3">
<label for="email">Votre adresse e-mail :</label>
<input type="text" name="email" placeholder="Email..." class="contact-email form-control" id="email">
</div>
<div class="form-group col-md-3">
<label for="name">Votre numéro de téléphone :</label>
<input type="text" name="tel" placeholder="Numéro de téléphone..." class="contact-tel form-control" id="tel">
</div>
<div class="form-group col-md-3">
<label for="entreprise">Entreprise :</label>
<input type="text" name="entreprise" placeholder="Entreprise..." class="contact-entreprise form-control" id="entreprise">
</div>
<div class="col-md-6 form-group">
<label for="jesuis">Je suis : </label>
<select class="form-control" name="jesuis" id="jesuis">
<option value="">Selectionnez...</option>
<option value="entreprise">Une entreprise</option>
<option value="courtier">Un courtier</option>
<option value="investisseur">Un investisseur</option>
<option value="professionnel">Un professionnel</option>
<option value="autre">Autre</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="entreprise">Numéro de SIREN/SIRET :</label>
<input type="text" name="siren" placeholder="SIREN/SIRET..." class="contact-siren form-control" id="siren">
</div>
<div class="col-md-12 form-group">
<label for="message">Message :</label>
<textarea name="message" placeholder="Message..." class="contact-message form-control" id="message"></textarea>
</div>
<div class="col-md-12 form-group">
<label for="antispam">Question Antispam : 7 + 5 = ?</label>
<input type="text" name="antispam" placeholder="Votre réponse..." class="contact-antispam form-control" id="antispam">
</div>
<div class="col-md-6 col-md-offset-3 form-group centered">
<button type="submit" class="btn">Envoyer</button>
</div>
</form>





Do you have any ideas ?

Thanks !

Loïc

Answer

Your PHP side has an error on the IF statement:

Take a further look at the lines containing: $headers

Your code:

if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
    // Send email
    $message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;    
    $headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail . 
    $headers = array("Content-Type: text/html; charset=UTF-8");
    mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}

Corrected Code:

if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
    // Send email
    $message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;    
    $headers = "From: " . $clientEmail . " <" . $clientEmail . ">\r\n";
    $headers.= "Reply-To: " . $clientEmail . "\r\n";
    $headers.= "Content-Type: text/html; charset=UTF-8\r\n";

    // Wrap mail function in if statement to check if it worked
    if (@mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers)) {
        echo 'Successfully sent email';
    } else {
        echo 'Failed to send';
    }
}

To help debug your code ensure your PHP errors are enabled, you can do this at the top of your PHP code using the following:

error_reporting(E_ALL);
ini_set('display_errors', 'On');
Comments