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?
– Leonardo Pessoa
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.
– Rúben Oliveira Nunes
I would need to see the HTML of the form, but it seems to me that it is not passing the information of
from
andto
PHP pro because they are manually set.– Leonardo Pessoa
Leonardo, I edited the question and inserted the HTML form... if you can help, thanks! thanks
– Rúben Oliveira Nunes
Try to replace the 3
$from
on the line after the$header...
for$_POST["email"]
and see if it works– Leonardo Pessoa
Thank you very much, Leonardo! It worked perfectly!
– Rúben Oliveira Nunes
Not at all! For documentation purposes, I put the answer below in the question.
– Leonardo Pessoa