I am trying to set up a simple contact form for my website. The end goal of the form is for the user to submit their information and have the server process and deliver the email as if it came directly from the user. In other words, if John Doe, whose email address is johndoe@example.com, fills out the form, then the email would read as if John Doe sent it from his email address, johndoe@example.com. What's mind-boggling to me is that I figured my server might not have authorization to send on the user's behalf; however, an older PHP form that I'm trying to move away from executes just fine.
Currently, this is my mail processing form PHP:
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
if (isset($_POST['myname'])) { $myname = $_POST['myname']; }
if (isset($_POST['myemail'])) {
$myemail = filter_var($_POST['myemail'], FILTER_VALIDATE_EMAIL );
}
if (isset($_POST['mysubject'])) { $mysubject = $_POST['mysubject']; }
if (isset($_POST['mycomments'])) {
$mycomments = filter_var($_POST['mycomments'], FILTER_SANITIZE_STRING );
}
if (isset($_POST['reference'])) { $reference = $_POST['reference']; }
if (isset($_POST['requesttype'])) { $requesttype = $_POST['requesttype']; }
$formerrors = false;
if ($myname === '') :
$err_myname = '<div class="error">Sorry, your name is a required field</div>';
$formerrors = true;
endif; // input field empty
if ($mysubject === '') :
$err_myname = '<div class="error">Please enter a subject for your message.</div>';
$formerrors = true;
endif; // input field empty
if ( !(preg_match('/[A-Za-z]+, [A-Za-z]+/', $myname)) ) :
$err_patternmatch = '<div class="error">Sorry, the name must be in the format: Last, First</div>';
$formerrors = true;
endif; // pattern doesn't match
if (!($formerrors)) :
$to = 'example@example.com';
$subject = "$mysubject";
$message = "\nName: $myname\n\nEmail: $myemail\n\nSubject: $mysubject\n\nComments: $mycomments\n";
$from = $myname.'<'.$myemail.'>';
$headers = 'From: '.$from."\r\n".
'Reply-To: '.$myemail."\r\n";
if (mail($to, $subject, $message, $headers)):
$msg = "Thanks for filling out our form";
else:
$msg = "Problem sending the message";
endif; // mail form data
endif; // check for form errors
endif; //form submitted
?>
Currently the reply-to works just fine, but the email received via the form indicates LastName@hostdomain.com. Thank you for your contributions.