how to reply to sender email using php contact form - php

I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you
<?php
$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];
$to = 'myemail#gmail.com';
$from = 'myemail#gmail.com';
$subject = 'Contact Form';
$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n
email: $email\n telefon: $telefon\n";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.website.com/sent.php");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>

First make headers
$headers = "From: $from\r\nReply-to: $email";
Than fix calling of mail function to be
mail ($to, $subject, $body, $headers)
Didn't tried it from times when it was PHP 4 but it will probably work as you expected...
Addition:
I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."

Related

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.

PHP Contact Form Coming From Admin's Email

I made a PHP Contact Form using this tutorial and it works great, but I've encountered one potential security risk / inconvenience. Each email I receive comes from my admin login name.
I added $headers as this thread instructed, but to no avail.
My Current PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myClientsEmail#gmail.com';
$subject = 'Estimate Contact Form';
$headers = "From: $email\r\n"; /* I added this */
$headers .= "Reply-To: $email\r\n"; /* and this */
$body = "From: $name\n Phone: $phone\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
What exactly am I missing? Any help is greatly appreciated. Thank you!
Your mail() function call has an extra parameter it looks like. The correct mail() call should be:
if (mail($to, $subject,$body,$headers)) {
....
}
So just remove the $from portion and it should be good.

Open a "Thank You" -html page on submit (with php form mail)

I just installed a feedback form that uses php, but I'm very new to the language.
The form itself is working. My question is this:
At the moment the form echoes a "Thank You" string when it's submitted. Can I have it redirect the user to a html-page instead?
Here's is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Myname';
$to = 'info#mydomain.com';
$subject = 'mydomain.com feedback';
$human = $_POST['human'];
$answers = array('red','Red');
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && in_array($human,$answers)) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks!</p>';
} else {
echo '<p>Something went wrong!</p>';
}
} else if ($_POST['submit'] && !in_array($human,$answers)) {
echo '<p>You ansered the captcha wrong!</p>';
}
?>
Instead of echoing '<p>Thanks!</p>';
Just change it to a header() function...
And direct to whatever URL you wanna go to...
Example....
if (mail ($to, $subject, $body, $from)) {
header('Location: http://www.example.com/');
} else {
Yes you can using header location.
Take a look at: http://php.net/manual/en/function.header.php

PHP Unknown sender

Hello I am fairly new to PHP and do not know a lot at the moment. I have modified a contact form an have come into some problems regarding the mail going straight to junk.
I assume this is for the reason that (unknown sender) keeps displaying in the email header. I would appreciate it if someone could help me correct this. The following is the code that I have implemented into the website:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Wirral PT Enquiry';
$to = 'joebloggs#hotmail.com';
$subject = 'Wirral PT Enquiry';
$human = $_POST['human'];
$headers = "enquiry#wirralpt.co.uk";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '2') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p> 1+1=2!! </p>';
}
} else {
echo '<p>You need to fill everything!!</p>';
}
}
?>
$from = 'From: Wirral PT Enquiry'; should contain the 'from' email address, not just the name:
$from = 'From: Wirral PT Enquiry <enquiry#wirralpt.co.uk>';
Try that?
try using
$headers = "Reply To :enquiry#wirralpt.co.uk";
Might work for you
also,
$headers = "From :enquiry#wirralpt.co.uk";
try both of these with you relevant email IDs
Change your headers to this:
$headers = 'From: enquiry#wirralpt.co.uk' . "\r\n" .
'Reply-To: enquiry#wirralpt.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and your mail should look this this:
mail ($to, $subject, $body, $headers)
This error can also be caused if while using SMTP settings for PhpMailer, $mail->IsSMTP(); is missed

ajax php mail function not working for hotmail addresses

I am having a problem with my contact form and the php mail() function. For some reason, they work for every email address (#gmail, #yahoo, #outlook and even #facebook!) except the old dreaded hotmail. I am just curious as to where my code is missing something. I have checked the mail servers and there is apparently no issue with hotmail addresses.
The email does not even get delivered to the spam/junk folder (it does not reach hotmail). I had a look online and some say to change the headers to avoid being caught in the spam filter. Any pointers to this?
PHP CODE
<?php
header('Content-Type: application/json charset=utf-8');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'companyname#mail.com';
$to = 'myemail#hotmail.com';
$subject = $name . ' has sent you a message';
$human = $_POST['antispam'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['name']) && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '{"status":"1"}';
} else {
echo '{"status":"0"}';
}
}
else
{
echo '{"status":"2"}';
}
?>
The if statements is just a check if all forms are valid and the anti spam (2+2) is correctly entered. nothing much to do in this part. The issue I guess is somewhere in the header
Try using these changes:
<?php
header('Content-Type: application/json; charset=utf-8');
...
$from = 'companyname#mail.com';
$headers = 'From: '. $from. "\r\n";
...
if (mail ($to, $subject, $body, $headers)) {
...
?>
4th parameter of mail function is expected to be additional_headers, not just from address.

Categories