Contact form HTML + PHP

1

Hello! I'm doing a site where the client asked for a contact form, but I understand very little of php. So I searched the internet for tutorials on how to use this function. You are sending the email normally, but only the sent message appears, and you do not have any error message if any field is not correct. What is the best way to do this? I saw in other topics, the staff speaking PHPMailer, is more practical?

Here is the result of my PHP code:

<?php

// Recebendo os dados
$recebenome     = $_POST["nome"];
$recebefone     = $_POST["fone"];
$recebemail     = $_POST["email"];
$recebeassunto  = $_POST["assunto"];
$recebemsg      = $_POST["msg"];

// Definindo os cabeçalhos do e-mail
$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-type:text/html; charset=utf-8 \n"; 
$headers .= "From: Formulario de contato\n"; 

// Destinatário do email
$para = "[email protected]";

// Definindo o aspecto da mensagem
$mensagem   = "<h3>De:</h3> ";
$mensagem  .= $recebenome;
$mensagem  .= "<h3>Contato:</h3>";
$mensagem  .= $recebefone.' - E-mail: '.$recebemail;
$mensagem  .= "<h3>Observações</h3>";
$mensagem  .= "<p>";
$mensagem  .= $recebemsg;
$mensagem  .= "</p>";

// Enviando a mensagem para o destinatário
mail($para,'Contato pelo site - de: '.$recebenome,$mensagem,$headers);

// Resposta Automática, preparando o e-mail com a resposta.
$mensagem2  = "<p>Olá <strong>" . $recebenome . "</strong>.<p>Agradecemos sua visita ao nosso site e a oportunidade de receber-mos seu contato.
<br />Em breve responderemos sua questão através de correio eletrônico.</p><br><p>OBS.: Não é necessário responder esta mensagem!</p><br>";
$mensagem2 .= "<p>Atenciosamente<br />Firenze ".$empresa."</p>";

// Enviando a resposta sutomática

$envia =  mail($recebemail,"Agradecemos sua visita ao nosso site",$mensagem2,$headers);

// Exibe um alert que a mensagem foi enviada com sucesso.
echo '<script>
                alert("Mesagem enviada com sucesso!");history.go(-1);
          </script>';

?>

And here's the HTML code:

<form role="form" method="post" action="mail.php">
<div class="form-group">
<input type="text" class="form-control" id="nome" name="nome" placeholder="Nome" required>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="E-mail" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="fone" name="fone" placeholder="Telefone" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="assunto" name="assunto" placeholder="Assunto" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" id="msg" name="msg" placeholder="Mensagem" maxlength="180" rows="6"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>                    
</div>

<button type="submit" id="submit" name="submit" value="Enviar" class="btn btn-send pull-right">Enviar</button>
</form>
    
asked by anonymous 18.08.2015 / 01:59

2 answers

1

Use javascript HTML authentication in fields so the person is alerted as they type.

It's not interesting to use the php mail function because messages can go to waste or be rejected by the server. It's best to always use php mailer.

    
18.08.2015 / 12:35
0
$to = "[email protected]";
$from = "[email protected]";
$subject = "subject";
$message = "mensagem";

$headers = "From: $from"; 
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);  
    
18.08.2015 / 02:20