Question:
PHP email sending!!!?
bluboy
2007-06-10 21:46:56 UTC
I have a page with a form in it to send a short message to my email address. The form has three fields: Name, E-mail & Message. The name one is where the sender types his/her name; Email where sender puts their email address, and Message where they type the message (simple)

when the user clicks submit it runs the following php code:

sendEmail($_POST);

function sendEmail($post){
$emailto .= "email@somewhere.com";
$subject .= "Question from " . $post['uname'];
$header .= $post['$uname'];
$message .= $post['umail']." wrote the following:\n\n";
$message .= $post['umessage']."\n\n";
mail($emailto, $subject, $message, $header);
header("LOCATION: contact_sent.php#sent");
}

?>

Problem: when I check the sent message i see the date, the subject, but the sender appears as follows:

nobody@server#.domain.com

Question: How can I do so that the field with the email appears as the sender, or that the field with the name appears as the sender (or maybe both) Thanks
Three answers:
Memphis
2007-06-11 00:25:11 UTC
To simplify your code and make it more easy for youto understand, the actual mailing function (one that does the emailing) is simply:

mail($emailto, $subject, $message, $header);



You will notice that the $header variable contains the visitor inputs (most probably his/her name... right?).



You need to change $header variable to:



$header .= "From: $post['email']\r\nReply-To: $post['email']";

where $post['email'] contains the email address entered by the visitor through your HTML contact form (thus, "email" is the name of the text field that gathers the email address of the visitor - please change it accordingly).



Another thing, I suggest you don't use $header as a variable name. "header", if I am not mistaken, would be a PHP reserved word.



Finally, check all visitor inputs for email injection attempts.
teef_au
2007-06-10 22:05:11 UTC
OK well it is always going to appear that way because the user in effect is not sending you a message, the host web server is sending it. Best way is to include the sender's email as part of the message body.



After your line: $message .= $post['umail']." wrote the following:\n\n";



Try inserting this:

$message .= "Sender was" ".$post['E-mail']\n\n";



See how you go.
anonymous
2007-06-10 22:01:24 UTC
Try here:



http://www.w3schools.com/php/php_post.asp


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...