PHP reply-to error - need person's email, not admin - php

I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>

You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);

Related

This script show me this message every time "Please fill out all the mandatory fields." [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm trying everything but this PHP script show this error "Please fill out all the mandatory fields."
Please help me with how to solve this problem.
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
} else {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
}
?>
Your if else statements are positioned incorrectly.
They point to wrong conditions.
The rearranged code:
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($email, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
// #1: Swipe with #2
echo "Your enquiry could not be sent for some reason; please try sending us again."; // Flip this with #2
}
} else {
echo "Please fill out all the mandatory fields."; // #2
}
?>
Too many nested IF statements is a bad practice because it is difficult to debug. Instead, you should break them to small statements
For example:
<?php
session_start();
if !(isset($_POST['fullname']) || isset($_POST['email']) || isset($_POST['phone'])) {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
exit();
}
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
?>
Now the code becomes clearer and you can trace back. Seem your code has error at mail function so it return False, then the message shows up.

Validate full contact page

<?php
if (isset($_POST["sendMessage"])) {
$firstName = trim($_POST['firstName']);
$lastName = trim($_POST['lastName']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$message = trim($_POST['message']);
$from = 'somebody#gmail.com';
$to = 'someone#gmail.com';
$subject = 'webmaster#example.com';
$txt = 'Prottyasha School';
$headers = "From: somebody#gmail.com" . "\r\n" .
"CC: somebody#gmail.com";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
//echo "success?";
if (mail($to, $subject, $body, $headers)) {
$result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result;
}
?>
It works fine but i want to make it validated. suppose someone give a invalid phone number or email then it will message its invalid mail or phone number. i want to make it full validated. anyone please help me.
I don't think it's possible to check if the mail was successfully delivered. mail() returns boolean true when a mail has been accepted for delivery or sent to the local email service. There's no way to know if that email was delivered to the recipient.

Problems with contact form and input validation

Ok so I am trying to create a contact form that validates user input to keep hackers from submitting codes and trying to require number, text, and email only. I have already styled the form and imported my php file. The contact from will send it to my gmail account. but everytime I test the php It allows for any type of data to be entered no matter if it is supposed to be a number and letters are submitted and the other way around. If I could get some help in telling me where I went wrong that would be great. I am a beginner at programming and only have the knowledge I recieved from school but I'm pretty good at html and css but having problems with the php validation. The form sends the email but like I said it allows any and all input.
<?php
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: www.webdesignheros.com';
$to = 'heenanwrk#gmail.com';
$subject = 'Service Email for HeenanTech';
$tel = filter_input(INPUT_POST, 'tel', FILTER_SANITIZE_INT);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
?>
<?php
if ($_POST['submit']){
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
Additionally the form can be found at [http://webdesignheros.com/Contact.html][1]
[1]: http://webdesignheros.com/Contact.html and if someone could tell me how to reject certain input before the submit that would be awesome too. like if an invalid entry was input and they move on to the next input it would reject it and not let the submit button be pushed. would i use the pattern="a-z" in the html or would i need to add javascript for that?
<?php
if (isset($_POST["submit")){
$name = $_POST["name"];
$tel = $_POST["tel"];
$email = $_POST["email"];
$message = $_POST["message"];
$from = "From: www.webdesignheros.com";
$to = "heenanwrk#gmail.com";
$subject = "Service Email for HeenanTech";
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
mail($to, $subject, $body, $from);
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>

PHP Email Not Work

i have a website wherein you can inquiry or leave questions and comments and send it via email, the code runs perfectly but it doesn't send email. here is my code:
$myemail = 'albertdaracan#gmail.com';//<-----Put Your email address here.
$fname = "albert";
$lname = "daracan";
$subject = "Magosaburo Inquery Form";
$email_address = $_POST['email'];
$message = "Name: ".$fname." ".$lname;
$message = "test";
$to = $myemail;
$email_subject = $subject;
$email_body = $message;
$headers = "From: $fname $lname <$email_address>\n";
$headers .= "Cc: ";
$headers .= "Reply-To: $email_address";
//redirect to the 'thank you' page
if(mail($to,$email_subject,$email_body,$headers)){
echo "mail successful send";
}else{
echo "Could not send you mail, Please try again later";
}
$message2 = "test";
$to2 = $email_address;
$email_subject2 = "Magosaburo";
$email_body2 = "$message2";
$headers2 = "From: Magosaburo <$myemail>\n";
$headers2 .= "Reply-To: $email_address";
//redirect to the 'thank you' page
if(mail($to2,$email_subject2,$email_body2,$headers2)){
echo "mail successful send";
}else{
echo "Could not send you mail, Please try again later";
}
my other works was fine it send email, but it is in other server...
why not try to fix this issue first
$fname = "albert" //missing semi colon
$lname = "daracan" //missing semi colon
$subject = "Magosaburo Inquery Form"; //terminated here
may be the values were not assigned well which causes the faultness

email address in from field on contact form not showing in email header as "from"

iv added the header field in the form code but when i receive the email, the "from" field in the email shows a garbled code and not the email address of the person who sent the message. This is my message processing code:
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $nome <$mail>\r\n";
$to = $youremail;
$mailsubject = 'Message received from'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is '.$fname.' '.$lname.'
Address: '.$address.'
'.$city.', '.$zip.', '.$country.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You declared your ^From: header` here:
$headers = "From: $nome <$mail>\r\n";
But it isn't used when you call mail()
mail($to, $subject, $body);
You need to pass $headers as fourth parameter. Else the headers won't take effect
mail($to, $subject, $body, $headers);
You haven't declared $nome anywhere in your code nor $mail in:
$headers = "From: $nome <$mail>\r\n";
And besides that, as this comment mentions, you didn't pass the header into the mail function. See the fourth parameter of the mail function.

Categories