[英]How can I add a “sender name” in a PHP contact form?
Considering that my experience with PHP is very limited, I customised a contact form that I found and it works as I want. 考虑到我对PHP的经验非常有限,我定制了一个我发现的联系表单,它可以按我的意愿运行。 The only thing that's bothering me is that I can't find a way to include a "sender name" in it, which means that when I recieve the e-mail, the sender field is empty as seen below. 唯一困扰我的是我找不到在其中包含“发件人姓名”的方法,这意味着当我收到电子邮件时,发件人字段为空,如下所示。
I would like to set "Contato" as the sender name. 我想将“Contato”设置为发件人姓名。 Is it possible? 可能吗?
Any tips on how to achieve this would be greatly appreciated. 任何有关如何实现这一目标的提示将不胜感激。
PHP: PHP:
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$to_Email = "giovanna.coppola@yahoo.com"; //Replace with recipient email address
$subject = 'Formulário de Contato (Site)'; //Subject line for emails
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Phone = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<6) // If length is less than 6 it will throw an HTTP error.
{
header('HTTP/1.1 500 Nome muito curto ou campo vazio.');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Insira um e-mail válido.');
exit();
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
header('HTTP/1.1 500 Esse campo só aceita números.');
exit();
}
if(strlen($user_Message)<10) //check emtpy message
{
header('HTTP/1.1 500 Mensagem curta demais.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "rn" .
'Reply-To: '.$user_Email.'' . "rn" .
'X-Mailer: PHP/' . phpversion();
$message = 'Nome: '.$user_Name."\r\n".
'E-mail: '.$user_Email."\r\n".
'Telefone: '.$user_Phone."\r\n".
'Mensagem: '.$user_Message;
$sentMail = mail($to_Email, $subject, $message, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Não foi possível enviar. Tente novamente.');
exit();
}else{
echo 'Olá, '.$user_Name.'. Agradecemos o contato! ';
echo 'Retornaremos o mais breve possível.';
}
}
?>
Change 更改
$headers = 'From: '.$user_Email.'' . "rn" .
to 至
$headers = 'From: '.$user_Name.' <'.$user_Email. '>'. "\r\n" .
see example #4 in the manual for mail()
- http://www.php.net/manual/en/function.mail.php#example-3416 请参阅mail()
手册中的示例#4 - http://www.php.net/manual/en/function.mail.php#example-3416
Use PHPmailer? 使用PHPmailer?
http://phpmailer.worxware.com/index.php?pg=examplebmail http://phpmailer.worxware.com/index.php?pg=examplebmail
$mail->SetFrom('name@yourdomain.com', 'Sender name here');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.