I'm not a PHP guy so I googled and found some simple code for a contact form via kirupa.com Everything works great, however I'd love to serve up a custom thank you / acknowledgement page instead users getting a simple echo text "Data has been submitted to your#email.com!" on a white blank page.
My contact form:
http://leongaban.com/contact.html
Here is the PHP code:
<?php
if(isset($_POST['submit'])) {
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "error! :'(";
}
?>
How should I edit this code to serve the users a thankyou.html page after they send an email?
if(isset($_POST['submit']))
{
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
if(mail($to, $subject, $body))
{
header("Location: http://yourdomain/thankyoupage.php");
}
else
{
echo "Error !";
}
}
You could use header for redirecting to page:
if(mail($to, $subject, $body)) {
header("Location: http://your/thankyou/page/url");
exit;
}
else{
...
}
Did you mean something like this
use header function http://php.net/manual/en/function.header.php
<?php
if(isset($_POST['submit'])) {
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
header('Location: thankyou.html');
}
else {
echo "error! :'(";
}
?>
Related
I'm trying to make a simple contact page, but everytime I try to send a message, I get an error.
Here is the PHP code:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "mailto:benten#benniestudios.eu";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $email, $formcontent, $mailheader) or die("Error!");
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
echo 'Return Home';
?>
What did I do wrong? If you need more info, please tell me.
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."
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.
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.
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>';
}
}