I'm having some trouble with a basic mail PHP script. I can get the email to send, but can't get the variables from the html form.
Here is the code, any ideas?
<form action='sendmail.php' method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email</label>
<input type="email" name="email" id="email" />
<label for="email">Phone Number</label>
<input type="number" name="number" id="number" />
<label for="comments">Message</label>
<textarea id="comments" name="message"></textarea>
<input type="submit">
</form>
And here is the PHP file
<?php
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
$to = 'sam.weinhandl#gmail.com';
$subject = 'Contact Form Message';
$message = "Name: ". $name . "\r\nPhone: " . $phone . "\r\nMessage: " . $message;
$headers = "From: " . $email_from . "\r\n" .
"Reply-To: " . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
You are using POST method to send form data to PHP. Use $_POST to get the values.
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
Either change method when posting your form
<form action='sendmail.php' method="GET">
Or
change the variables
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
You can use $_POST or $_REQUEST to fetch values. $_REQUEST will be the best option because it works for both get and post methods.
$name = $_REQUEST["name"];
$email_from = $_REQUEST["email"];
$phone = $_REQUEST["number"];
$message = $_REQUEST["message"];
U should change your method to 'GET'.
Or u should change the variables.
The answers above all work.
Related
So I am rather new to coding in these languages, I am getting a good understanding of how to write in them but not how to troubleshoot well. I went on and found some videos and read some sites to understand how to get this to work, and got this
HTML
<form id="Contact-form" method="post" action="contactform.php">
<input type="text" name="name" placeholder="Your Name" class="form-control" required>
<input type="text" name="Mail" placeholder="Your E-mail" class="form-control" required>
<input type="text" name="name" placeholder="Subject" class="form-control" required>
<textarea name="message" placeholder="Message" rows="7" class="form-control" required></textarea>
<button class="form-control submit" type="submit" name="sumbit"> Send message</button>
The PHP
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
I have it live and attempted to test send myself an email and I get a basic error (HTML Error 500) and no email, it changes the URL to "contactform.php" as expected. I followed the tutorials to the T, what am I doing wrong!
Thank you for the help in advance
Please change the codes from
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
to
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend");
}
I'm new to development and I'm trying to build a contact form using php that pulls the info from a contact form. My contact form works and collects the data as needed, but when I click submit the first time I don't receive the information to my inbox. When I refresh the page, complete the form a second time and click submit once more, I receive both submissions to my inbox at the same time. It's as if the first submission waits to be pushed through by the second submission. Can anyone explain why this is happening and how to stop it?
I've seen a lot of posts regarding submissions happening twice but my issue is the first doesn't come through until the second is sent.
Here is my PHP
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
I'm testing on localhost8888 using MAMP Pro.
Here is HTML:
<form class="contact-form" action="contactform.php" method="post" id="form">
<label for="name">Full Name:</label><br>
<input type="text" id="name" name="name" placeholder="Please enter your name" required>
<label for="email">Email Address:</label><br>
<input type="email" id="email" name="email" placeholder="Please enter your email address" required>
<label for="phone">Phone Number:</label><br>
<input type="text" id="phone" name="phone" placeholder="Please enter your phone number" required></input>
<label for="subject">Subject</label><br>
<input type="text" id="subject" name="subject" placeholder="Please enter a subject" required></input>
<label for="address">Postcode:</label><br>
<input type="text" name="address" placeholder="Please enter your postcode"></input><br>
<label for="body">Comments:</label><br>
<textarea name="body" placeholder="Please enter your message"></textarea>
<input type="radio" id="morning" name="time" value="morning">Morning</input>
<input type="radio" id="afternoon" name="time" value="afternoon">Afternoon</input>
<input type="radio" id="anytime" name="time" value="anytime">Anytime</input>
<button type="submit" id="submit" name="submit">Send Message</button>
</form>
This happens because when you update the page, you will be resending the POST.
As this is a contact form, i suggest installing Google reCaptcha. Thus, it will be necessary to resolve the captcha and this will not happen.
There are other ways. One of them would be to store user data in a database and only authorize a sending by IP or session at a certain time.
Or you can just work with direct session and prevent this from happening, using the function in your code:
function prevent_multi_submit($type = "post", $excl = "contact") {
$string = "";
foreach ($_POST as $key => $val) {
// this test is to exclude a single variable, f.e. a captcha value
if ($key != $excl) {
$string .= $val;
}
}
if (isset($_SESSION['last'])) {
if ($_SESSION['last'] === md5($string)) {
return false;
} else {
$_SESSION['last'] = md5($string);
return true;
}
} else {
$_SESSION['last'] = md5($string);
return true;
}
}
Your code will look like this:
session_start();
if (isset($_POST['submit']) and prevent_multi_submit()) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
EDIT 1:
Or, add a simple parameter to the URL that redirects, so that a second POST is not authorized:
if (isset($_POST['submit']) and $_GET["sent"] != "ok") {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?sent=ok");
}
Read more about HTTP GET variables at PHP.net
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So everything works except that I don't receive an email after sending and wonder why?
keeps getting this error "Could not execute mail delivery program '/usr/local/bin/sendmail -oi -t' in"
Html
<form action="process.php" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
</li>
<li>
<label for="topic">Topic:</label>
<select>
<option value="optiona">optiona</option>
<option value="optionb">optionb</option>
<option value="optionc">optionc</option>
</select>
</li>
<li>
<label for="message">your message:</label>
<textarea id="message" name="message" cols="42" rows="9"></textarea>
</li>
<li><input type="submit" value="Submit"></li>
</form>
PHP
<?php
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$message = <<<EMAIL
Hi! My name is $name.
$message
From $name
my email is $email
EMAIL;
$header ='$email';
if($_POST){
mail($to, $subjects, $message, $header);
$feedback = 'Thankyou for your email';
echo $feedback;
}
?>
Maybe your $header and $message definition is not ok. Try this:
<?php
// Constants
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
// Variable contents from the form
$name = (!empty($_POST['email'])) ? $_POST['name'] : '????';
$email = (!empty($_POST['email'])) ? $_POST['email'] : $to;
$message = 'Hi? my name is ' . $name;
if (!empty($_POST['message'])) $message .= . "\r\n" . $_POST['message'];
// Header
$header = 'From: ' . $email . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send mail and give feedback
$accepted = mail($to, $subjects, $message, $header);
if ($accepted) {
$feedback = 'Thankyou for your email';
} else {
$feedback = 'Email not accepted';
}
echo $feedback;
my PHP code:
$myemail = myemail;
$subject = "success";
if(isset($_POST['firstname'])){ $name = $_POST['firstname']; }
if(isset($_POST['lastname'])){ $price = $_POST['lastname']; }
$message = "$firstname $lastname";
mail($myemail, $subject, $message);
my html using twitter bootstrap:
<form class="form-horizontal" name="application" method="POST" action="submission.php">
<legend></legend>
<div class="control-group">
<label class="control-label" for="firstname">First Name</label>
<div class="controls">
<input type="text" id="firstname" name="firstname" placeholder="First Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="lastname">Last Name</label>
<div class="controls">
<input type="text" id="lastname" name="lastname" placeholder="Last Name">
<button type="submit" class="btn btn-success" id="submit">Submit</button>
</div>
</div>
</form>
I receive the email, but there is nothing in the body.
You're using the wrong variables. $name and $price in:
$name = $_POST['firstname']; // should be $firstname
$price = $_POST['lastname']; // should be $lastname
& in conjunction with:
$message = "$firstname $lastname";
change it to:
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
Or, use $message = "$name $price"; while leaving the rest of your code intact. Either way, variables need to match up and to remain consistent throughout the code's execution.
Use error reporting:
http://php.net/manual/en/function.error-reporting.php
which would have signaled an Undefined index error.
Additional notes:
Your code does not contain additional headers, especially a From: which stands to be rejected by many servers, or ending up in Spam.
Use proper mail headers:
http://php.net/manual/en/function.mail.php
as per documentation:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if(isset($_POST['firstname'])){ $name = $_POST['firstname']; }
if(isset($_POST['lastname'])){ $price = $_POST['lastname']; }
$message = "$firstname $lastname";
You must be kidding.
$name =...
$price =...
..."$firstname $lastname"
There are no values assigned to $firstname/$lastname variables, maybe use "$name $price" instead?
I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>