How can I add a "sender name" in a PHP contact form? -
considering experience php limited, customised contact form found , works want. thing that's bothering me can't find way include "sender name" in it, means when recieve e-mail, sender field empty seen below.
i set "contato" sender name. possible?
any tips on how achieve appreciated.
php:
<?php if($_post) { //check if ajax request, exit if not if(!isset($_server['http_x_requested_with']) , strtolower($_server['http_x_requested_with']) != 'xmlhttprequest') { die(); } $to_email = "giovanna.coppola@yahoo.com"; //replace recipient email address $subject = 'formulário de contato (site)'; //subject line emails //check $_post vars set, exit if 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 less 6 throw 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 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 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 manual mail()
- http://www.php.net/manual/en/function.mail.php#example-3416
Comments
Post a Comment