roldahayes Posted March 30, 2011 Share Posted March 30, 2011 Hi, I am trying to set up a contact form and at the moment, the email is "coming from" the server when sent so hitting reply in an email program sends the mail back to the server. I need it to return back to the persons mail who filled out the form. I know that I would need something like $from = ................... but not sure how to actually write this. The code is: <?php $to = "test@test.com"; // Replace with your email address $subject = "Message from ".ucwords($_POST['name']); // Enter the subject here. //Validating email addres $email = $_POST['email']; function validateEmail($email) { if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $email)) return true; else return false; } //Validates the required fields if((strlen($_POST['name']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['message']) < 1 ) || validateEmail($email) == FALSE){ $emailerror .= true; } else { $emailerror .= false; //Composing the email $email_message = "Name: " . ucwords($_POST['name']) . "\n" . "Email: " . $email . "\n" . "Message: " . "\n" . $_POST['message'] . "\n"; //Sending the email mail($to, $subject, $email_message); } ?> <?php if($emailerror == true) { echo '<span>Please fill all the fields.</span>'; } else { echo "<span>Message has been sent. Thank you!</span>"; } ?> Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/ Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 You need to set the 'From' header and pass it in to the mail() function as the 4th "additional headers" parameter: $headers = "From: someone@example.com\r\n"; mail(.., .., .., $headers); As you may have noticed the headers are a string and delimited between a CRLF (Carriage return/linefeed - "\r\n"). You can add other headers to the string too, like 'Reply-To': $headers = "From: someone@example.com\r\n"; $headers .= "Reply-To: someone-else@example.com\r\n"; Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194271 Share on other sites More sharing options...
roldahayes Posted March 30, 2011 Author Share Posted March 30, 2011 Thats great, may thanks but can I get the "someone@example.com" to be the email of the person who has filled out the email field on the contact form? Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194310 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 Yeah you just need to feed in the variable where you have it saved: $headers = "From: {$from_email}\r\n"; Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194314 Share on other sites More sharing options...
roldahayes Posted March 31, 2011 Author Share Posted March 31, 2011 I'm really struggling with this - when clicking reply, it goes to the admin@mysite.com address and not that of the user who has filled the form in? Heres what I have as the code: <?php $to = "admin@mysite.com"; // Replace with your email address $subject = "Message from ".ucwords($_POST['name']); // Enter the subject here. $headers = "From: admin@mysite.com\r\n"; $headers .= "Reply-To: " . $email . "\n" ; //Validating email addres $email = $_POST['email']; function validateEmail($email) { if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $email)) return true; else return false; } //Validates the required fields if((strlen($_POST['name']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['message']) < 1 ) || validateEmail($email) == FALSE){ $emailerror .= true; } else { $emailerror .= false; //Composing the email $email_message = "Name: " . ucwords($_POST['name']) . "\n" . "Email: " . $email . "\n" . "Message: " . "\n" . $_POST['message'] . "\n"; //Sending the email mail($to, $subject, $email_message, $headers); } ?> <?php if($emailerror == true) { echo '<span>Please fill all the fields.</span>'; } else { echo "<span>Message has been sent. Thank you!</span>"; } ?> Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194888 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2011 Share Posted March 31, 2011 You are producing the $header variable before you have assigning anything to the $email variable that it contains and the Reply-to: header is an empty value. You would want to produce the $header variable after any variables it contains have been assigned a value. Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194896 Share on other sites More sharing options...
roldahayes Posted March 31, 2011 Author Share Posted March 31, 2011 Excellent, now works fine - Many thanks for your help! Link to comment https://forums.phpfreaks.com/topic/232159-mail-from-help-please/#findComment-1194903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.