考虑到我使用PHP的经验非常有限,我定制了一个我找到的联系表单,它可以按我的意愿运行。困扰我的唯一一件事就是我找不到在其中包含“发件人姓名”的方法,这意味着当我收到电子邮件时,发件人字段为空,如下所示。
我想将“Contato”设置为发件人姓名。有可能吗?
非常感谢有关如何实现这一目标的任何提示。
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.';
}
}
?>
答案 0 :(得分:3)
更改
$headers = 'From: '.$user_Email.'' . "rn" .
到
$headers = 'From: '.$user_Name.' <'.$user_Email. '>'. "\r\n" .
请参阅mail()
- http://www.php.net/manual/en/function.mail.php#example-3416
答案 1 :(得分:0)
使用PHPmailer?
http://phpmailer.worxware.com/index.php?pg=examplebmail
$mail->SetFrom('name@yourdomain.com', 'Sender name here');