php – Send a message via e-mail to customers who responded to the form on my site

Question:

I have the following code to send responses from a form on my website to my email:

<?php

//
//Variáveis
$n_nome = $_POST['n-nome'];
$n_email = $_POST['n-email'];
$n_fone = $_POST['n-fone'];
$n_msg = $_POST['n-msg'];

$data_envio = date('d/m/Y');
$hora_envio = date('H:i:s');

//
// Configuração do e-mail
  $arquivo = "
<html>
    <h1>NOVA MENSAGEM RECEBIDA VIA SITE</h1>

    <p>Nome: <b>$n_nome</b></p>
    <p>E-mail: <b>$n_email</b></p>
    <p>Telefone: <b>$n_fone</b></p>
    <p>Mensagem: <br> <b>$n_msg</b></p>
</html>
";

//
//enviar
  $emailenviar = "meuemail@contato.com";
  $destino = $emailenviar;
  $assunto = "[SITE CONTATO] $n_nome";

  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  $headers .= 'From: Meu Site <naoresponda@contato.com>';

  $enviaremail = mail($destino, $assunto, $arquivo, $headers);
  if($enviaremail){
  $mgm = "MENSAGEM RECEBIDA COM SUCESSO!";
  echo " <meta http-equiv='refresh' content='2;URL='> "; // Configurar página de destino
  } else {
  $mgm = "ERRO AO RECEBER MENSAGEM!";
  echo "<meta http-equiv='refresh' content='2;URL='>";
  }
?>

Now when I go to reply to the message in my email I have to copy the email from the form and paste it into the appropriate field. It always tries to reply to the Is there a way to pre-defined that when I reply it already puts $n_email as a recipient?

Answer:

The From: should be the sender and not a fixed email:

$headers .= 'From: Meu Site <naoresponda@contato.com>';

Should be:

$headers .= 'From: ' . $n_nome . ' <' . $n_email . '>';

Note that you can also use Reply-To: , ie set the sender to one, but when replying Reply-To will give preference to the email defined in it (this may not work in some web email clients anymore archaic, like squiremail), then it would look like this:

$headers .= "From: Meu Site <naoresponda@contato.com>\r\n";
$headers .= 'Reply-To: ' . $n_nome . ' <' . $n_email . '>';
Scroll to Top