Configure Reply-To in form

Asked

Viewed 161 times

2

Hello,

I am working on a PHP form where I need to configure the sender’s email in the 'Reply-To: ' field. $from , that way, the reply to the email will be direct to whoever filled out the form. Follow the current PHP code.

    <?php

    $from = 'Nome <nome@email>';

    $sendTo = 'Nome <nome@email>';

    $subject = 'Formulário de Contato';

    $fields = array('name' => 'Nome', 'email' => 'E-mail', 'phone' => 'Telefone', 'empresa' => 'Empresa', 'assunto' => 'Assunto', 'message' => 'Mensagem');

    $okMessage = 'Mensagem enviada com sucesso! Em breve lhe retornaremos.';

    $errorMessage = 'Erro no envio da mensagem. Por favor, tente novamente.';

    error_reporting(E_ALL & ~E_NOTICE);

    try
    {

    if(count($_POST) == 0) throw new \Exception('Formulário está vazio.');

    $emailText = "Formulário de Contato\n=============================\n";

    foreach ($_POST as $key => $value) {
    // If the field exists in the $fields array, include it in the email 
    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
    }

    $headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
    );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
    }

    else {
    echo $responseArray['message'];
    }

Below, it follows HTML:

    <form id="contact-form" method="post" action="contact.php" role="form">
    <div class="messages"></div>
    <div class="controls">
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <label for="form_name">Nome *</label>
    <input id="form_name" type="text" name="name" class="form-control" placeholder="Digite seu nome *" required data-error="Por favor, preencha este campo.">
    <div class="help-block with-errors"></div>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <label for="form_email">E-mail *</label>
    <input id="form_email" type="email" name="email" class="form-control" placeholder="Digite seu e-mail *" required data-error="Por favor, digite um e-mail válido.">
    <div class="help-block with-errors"></div>
    </div>
    </div>
    </div>
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <label for="form_phone">Telefone</label>
    <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Digite seu telefone">
    <div class="help-block with-errors"></div>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <label for="form_empresa">Empresa</label>
    <input id="form_empresa" type="text" name="empresa" class="form-control" placeholder="Digite o nome de sua empresa">
    <div class="help-block with-errors"></div>
    </div>
    </div>
    </div>
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <label for="form_assunto">Assunto *</label>
    <select id="form_assunto" name="assunto" class="form-control" required data-error="Por favor, selecione um assunto.">
    <option value="" selected="">Selecione um assunto *</option>
    <option value="Solicitação de Orçamento">Solicitação de Orçamento</option>
    <option value="Trabalhe Conosco">Trabalhe Conosco</option>
    <option value="Administrativo/Financeiro">Administrativo/Financeiro</option>
    <option value="Comercial">Comercial</option>
    <option value="Diretoria">Diretoria</option>
    <option value="Dúvidas, críticas ou sugestões">Dúvidas, críticas ou sugestões</option>
    </select>
    <div class="help-block with-errors"></div>
    </div>
    </div>                 
    </div>
    <div class="row">
    <div class="col-md-12">
    <div class="form-group">
    <label for="form_message">Mensagem *</label>
    <textarea id="form_message" name="message" class="form-control" placeholder="Digite sua mensagem *" rows="4" required data-error="Por favor, preencha este campo."></textarea>
    <div class="help-block with-errors"></div>
    </div>
    </div>
    <div class="col-md-12">
    <input type="submit" class="btn btn-success btn-send" value="Enviar">
    </div>
    </div>
    <div class="row">
    <div class="col-md-12">
    <p class="text-muted">Campos terminados em <strong>*</strong> são obrigatórios.</p>
    </div>
    </div>
    </div>
    </form>

Thanks in advance!

  • Welcome to the OS! What is your mistake or doubt exactly?

  • Thank you, Leonardo, thank you! I would like, when replying to the email sent by the form, in the Recipient field to include the email of the person who contacted through the site. I’ve tried changing Reply-To: . $from putting the email variable, but since it’s not php domain and I don’t understand array (I got this script ready on the web), I’m not getting it done.

  • I would need to see the HTML of the form, but it seems to me that it is not passing the information of from and to PHP pro because they are manually set.

  • Leonardo, I edited the question and inserted the HTML form... if you can help, thanks! thanks

  • Try to replace the 3 $from on the line after the $header... for $_POST["email"] and see if it works

  • Thank you very much, Leonardo! It worked perfectly!

  • Not at all! For documentation purposes, I put the answer below in the question.

Show 2 more comments

1 answer

1


Replace the code snippet like this:

$headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $_POST["email"],
    'Reply-To: ' . $_POST["email"],
    'Return-Path: ' . $_POST["email"],
    );

You were manually setting the value of $from at the beginning of the code. This way, you will get the value that was sent dynamically by POST of form.

Browser other questions tagged

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