HTML + PHP contact form

Asked

Viewed 4,741 times

1

Hello! I’m making a website where the customer 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 there is no error message if any field is not correct. What is the best way to do this? I saw on other topics, the people talking about 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>
  • 1

    Use the phpmailer, the field from email should be an email and not contact form.

2 answers

1

Use HTML javascript authentication in the fields so the person is alerted while typing.

It is not interesting to use the php mail function because the messages can go to the trash or be rejected by the server. It’s best to always use php Mailer.

  • I’ll take a look at how php Mailer works! And the authentication you say is validation by js?

  • @Jefsilva, yes, he refers to JS validation. I still recommend redoing the server validation (PHP). phpmailer (https://github.com/PHPMailer/PHPMailer) is a library (library) that you include (usually) at the top of the php file that will send the messages. It is actually much superior to the php mail() function that should not be used.

  • 1

    @Renefreak should not be used because? I don’t see any problem with it if you don’t need the extras that Phpmailer offers. For the rest, I agree.

  • @Bacco, because of the way it’s implemented. It should only be used in very simple cases and even then, it usually causes problems, such as sending messages to junk mail (by the way it operates). If you don’t like the phpMailer, can use swiftmailer. And if you’re using mail() no problem, ignore!

  • @Renefreak asked me to see if you had any reference to obscenity, but from the comment, I think you’re confusing things a little. It doesn’t fall into the junk if you do the SPF correctly in DNS, and you can send it with any complexity you want, with attachments, HTML, it just doesn’t usually make up for the work. The fact is that as you commented, gave the impression that the mail() had some real problem, and in the case, the only problem of it is misuse by the "programmer" (so that if it was obsolete, it would be in the documentation).

  • As for the implementation, there is a point: in windows there are some differences, but documented. It is worth noting that the type of "opposition" to mail() is the same as the one mentioned in PHP Mailer itself: the Vast Majority of code that you’ll find online that uses the mail() Function directly is just Plain Wrong - and this is true: very common people use without knowing (by the way, this is a common point in everything that involves PHP, most of what is found online is actually below average, but has things well done).

  • @Renefreak Anyway, I just wanted to raise the issue to clarify the point of "should not use" (and in a way, really should only be used if the person has mastery over the subject, so I agree that if the person has qq doubt, better go to PHP Mailer directly). Thanks for the feedback. Just an extra tip: If you don’t want to use full PHP Mailer, and you need customization, you can take the SMTP part of it separately, which is excellent, as a starting point. (class.smtp.php). In this file you have everything you need to make the upload itself.

  • @Bacco, thank you for answering yourself!

Show 3 more comments

0

$to = "[email protected]";
$from = "[email protected]";
$subject = "subject";
$message = "mensagem";

$headers = "From: $from"; 
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);  
  • 3

    Vagneraraujo add some explanatory content to your reply so that it may be useful to members other than the author of the question. OK?

  • Might be useful to the author of the question too :D As I said, I know little about php...

  • Great , no problems =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.