PHP Contact Form Coming From Admin's Email - php

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.

Related

how to reply to sender email using php contact form

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."

PHP Contact Form return code [duplicate]

This question already has answers here:
PHP Page shows up raw code
(3 answers)
Closed 8 years ago.
I have the following PHP script that runs when a send button is clicked.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit') {
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']) {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
I am using MAMP to host the website locally. The problem occurs when the send button is hit, instead of the email being sent a page with the code is shown.
What is wrong in my code and how may I fix it?
Thank You.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
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{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
There are lots of error in your code
The condition in if and in else if are same so the else if condition never execute.
While checking submit from post value use isset($_POST['NAME']).

PHP form throwing errors - Notice: Undefined index

I am trying to implement php for a simple form.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
?>
<form class="form" method="post" action="say-hello.php">
<label>Name</label>
<input name="name" placeholder="Spongebob" required data-errormessage-value-missing="Whoa, you can't leave this blank!">
<label>Email</label>
<input name="email" type="email" placeholder="squarepants#krustykrab.com" required data-errormessage-value-missing="Whoa, you can't leave this blank!" data-errormessage-type-mismatch="Something isn't right...">
<label>Message</label>
<textarea name="message" placeholder="Well Hello!" required data-errormessage-value-missing="Whoa, you can't leave this blank!"></textarea>
<div class="bttnholder">
<input class="submit" name="submit" type="submit" value="Submit" placeholder="Send">
</div>
</form>
I can't figure out why part of my PHP is displayed as HTML and why I get the following errors on page:
Notice: Undefined index: name in C:\xampp\htdocs\sandbox\say-hello.php on line 35
Notice: Undefined index: email in C:\xampp\htdocs\sandbox\say-hello.php on line 36
Notice: Undefined index: message in C:\xampp\htdocs\sandbox\say-hello.php on line 37
Any help to get this code working?
The following are lines 35,36,37.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
You must first check variable to set. Try this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
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>';
}
}
The $_POST['submit'] is obsolete, as you should check for the correct form inputs.
I check for every form input to be set and not to be empty. If one of them is empty or not set -> error.
If all fields are given -> send the mail.
<?php
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
if(
!isset($_POST['name']) || empty($_POST['name']) ||
!isset($_POST['email']) || empty($_POST['email']) ||
!isset($_POST['message']) || empty($_POST['message'])
){
echo '<p>Please fill in all fields</p>';
}else{
$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Message:\n " . $_POST['message'] . "";
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>';
}
}
?>
To get rid of the Notices, you should change your php to:
<?php
if(isset($_POST['name'])) {
$name = $_POST['name'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_SERVER['CONTENT_LENGTH'])) {
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>';
}
Your mail function is wrong, there is no "from" argument in it, if you want a from argument your mail function should be like this:
First define a headers variable:
$headers = "From: $from";
And your mail function:
mail($to,$subject,$body,$headers)
You have to insert these lines inside the if statement.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test#yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
When the html page loads, the name for example doesn't exists. It's been set after clicking on the submit button. So if you put it inside the if statement, the script will read the variables only after the submit button is posted.
Anyway, for your future consideration, these are not errors, are warnings, the script still works.

How to specify from address in PHP email from a website

I have a PHP form. The form works, and can send emails through it. It doesn't look like it's sending them from a specific email address to the email address I want the emails to go to (xxx#a.com).
I would like these emails to be sent from yyy#a.com, which I have configured below. Here is the PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact';
$to = 'me#a.com';
$subject = 'Contact';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<script type="text/javascript">
alert("Your message has been sent!");
</script>';
} else {
echo '<script type="text/javascript">
alert("Something went wrong, try again.");
</script>';
}
}
?>
I have tried changing the $from to yyy#a.com, but that doesn't change the email's from address. Why isn't the From address getting set?
The fourth parameter isn't from, it is extra headers. So to include an extra from header, do this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact <yyy#a.com>'; // You can combine name and address
$to = 'xxx#a.com';
$subject = 'Contact';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$extraHeaders = 'From:'.$from; // Header field + header field value.
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $extraHeaders)) { // Pass the extra headers...
echo '<script type="text/javascript">
alert("Your message has been sent!");
</script>';
} else {
echo '<script type="text/javascript">
alert("Something went wrong, try again.");
</script>';
}
}

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

Categories