Processing an HTML form with PHP - php

Well, I have gotten the submit to work... somehow. But the email comes and it only contains my email as the body of the message (not even the email entered into the form)... no name, no phone number, no radio answers, ect.
Ok. I have finally gotten the html form to do SOMETHING. Unfortunately, when you hit "submit" it directs to a page that says this: Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Here is the PHP code:
<?php
if (!isset($_POST['submit'])) {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$telephone = $_POST['phone'];
$visitor_email = $_POST['email'];
//Validate first
if (empty($name) || empty($visitor_email)) {
echo "Name and email are mandatory!";
exit;
}
if (IsInjected($visitor_email)) {
echo "Bad email value!";
exit;
}
$email_from = 'heather#thetrinitydesign.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message" . $to = "heather#thetrinitydesign.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
//done. redirect to thank-you page.
header('Location: thank_you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array(
'(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject, $str)) {
return true;
} else {
return false;
}
}
?>
And the HTML. I feel like I need to have each of the questions I asked represented in the PHP file. Is this correct? And I have seen so many things preface the $POST thing that I am completely lost.
<form action="form-to-email.php" method="post" enctype="multipart/form-data" name="Info Form" id="Info Form">
<p class="form">Name:
<input name="Name" type="text" class="formbox" id="Name" size="20" />
</p>
<p class="form"> Phone:
<input name="Phone" type="text" class="formbox" id="Phone" size="12" maxlength="12" /></p>
<p class="form">Email:
<span id="sprytextfield2">
<input name="Email" type="text" class="formbox" id="Email" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span> </p>
<p class="form">
<label>Have you ever had Custom Interior Design work before?<br />
Here is the submit:
<br />
<input name="Submit" type="submit" id="button" value="Submit" />
<br />
</p>
<p class="form">

Just above the validate first comment is a $ that should not be there.

As well as all of the suggestions mentioned, I am going to hazard a guess that the internal error is because the form is posting to 'form-to-email.php', this needs to match the filename of your php file.

change this
<input name="Submit" type="submit" id="button" value="Submit" />
to
<input name="submit" type="submit" id="button" value="Submit" />
^ This should be lowercase here, see below
The code below needs to match the name above.... exactly...
if (!isset($_POST['submit'])) {
^ here!!!!
These two values need to match exactly...
$_POST['THIS'] and <input name="THIS" .../>
As Quinny has pointed out, you make this error with almost all the form inputs...
<input name="Email" .../> should be <input name="email" .../>
<input name="Phone" .../> should be <input name="phone" .../>
<input name="Name" ..../> should be <input name="name" ..../>

It looks to me like you are flying by the seat of your pants, and are just trying to get it working. Spend a little more time learn what is actually happening.
PHP Forms a great tutorial on how to get forms working.
a few things to note:
santize your $_POST data. make sure all data is safe. not just your email data.
look into coding standards, there a bunch, pick one drupals coding standards
give all form elements a name and id, not just an id, when you get into javscript later on, which you may or may not, this will be much easier.
avoid spaces in ids/names form name='Info Form' not a huge deal, could get ugly later on.
you can run the php file from command line, and get some good error details there.
turn on php error reporting Show Errors
these are just a few, not trying to be smug, trying to point you in the right direction.
EDIT:
change this:
$email_from = 'heather#thetrinitydesign.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message" . $to = "heather#thetrinitydesign.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
to this:
$message = "Hello My Friend";
$email_from = 'heather#thetrinitydesign.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message";
$to = $_POST['Email']; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
This will send an email from heather#thetrinitydesign.com to "what ever you type in email"
with the message
"You have received a new message from the user "what ever you type in name"
"Here is the message:
Hello My Friend";

Related

Routine spam on php contact form [duplicate]

This question already has answers here:
PHP email form shooting blank emails
(4 answers)
Closed 2 years ago.
I have a contact form on two different websites I have made for clients.
At around 8-9pm everyday a blank message is sent using the contact form and straight to my clients' respective email addresses.
PHP:
<?php
$name = $_POST['full-name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['full-name'];
$to = 'mobileguitarworkshop#hotmail.com';
if(!empty($_POST['field'])) die();
$email_from = 'mobileguitarworkshop#hotmail.com';
$email_subject = "Enquiry from $name.\n";
$body = "From: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to, $email_subject, $body, $headers);
header("Location: http://mobileguitarworkshop.co.uk/success.html");
exit();
?>
HTML:
<form action="contact.php" method="post" class="contact-form">
<label for="full-name">Name</label>
<input name="full-name" type="text" id="full-name" required>
<input type="text" id="field" name="field"/>
<label for="phone">Phone</label>
<input name="phone" type="tel" id="phone">
<label for="email">Email address</label>
<input name="email" type="text" id="email" required>
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<input name="send" type="submit" value="SEND" id="sendBtn">
</form>
I've tried adding 'required' to the Name and Email Address inputs to stop spammers, and also a hidden field that, if filled, directs them to 'success.html' without posting the message.
If anyone can explain why this is happening that would be great. The hosting service I'm using is 1&1 IONOS.
Thanks,
Jack
The spammers may be sending a request directly to the contact form endpoint, bypassing your form entirely. This means that required fields in the html wont do much to stop that. You'll need to check those properties on the backend to prevent those submissions. Something like this would work:
if(empty($_POST['full-name']) || empty($_POST['email'])) {
die();
}
If I were you, I'd also look into implementing a CSRF token. See How to properly add CSRF token using PHP
While we're talking, we really should sanitize the $_POST['message']; with something like the below to remove any questionable html content your users may have submitted:
$message = strip_tags($_POST['message']);

Strange Result With HTML contact/mail Form Using PHP File

I have a problem getting the data from my mail form being sent correctly in an email.
It does something strange with the email message.
Original form message:
Enter Name: Fake Name
Enter Email Address: fake.name#doesnotexist.com
Enter Message: Email test number 4(four).
Submit
_____________________________________________
The email message I received:
From: <fake.name#doesnotexist.com>
Subject: <New Form submission>
Reply to: <warnerheston#sungraffix.net>
To: Me <web289portfolio#sungraffix.net>
_____________________________________________
You have received a new message from the user Fake Name.
Here is the message:
Email test number 4(four).web289portfolio#sungraffix.net
_____________________________________________
The email in the message body is "(four).web289portfolio#sungraffix.net" and is underlined/hyperlinked... STRANGE!!
IT IS NOT SUPPOSED TO HAVE ANY EMAIL ADDRESS IN THE MESSAGE BODY AT ALL. THE USER DID NOT TYPE AN EMAIL ADDRESS AT THE END OF THE MESSAGE. SOMEHOW, THE PHP CODE IS DROPPING THE DESTINATION EMAIL ADDRESS INTO THE MESSAGE BODY AND ATTACHING THE LAST "WORD" OF THE USER'S MESSAGE TO THAT EMAIL ADRESS WHEN IT SENDS IT.
Can someone tell me what I am doing wrong?
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact-form-incomplete.html');
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $_POST['email']; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "web289portfolio#sungraffix.net";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: contact-thankyou.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
This is the HTML Form:
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'><span class='form'>Enter Name:</span></label><br>
<input type="text" name="name" size="36">
</p>
<p>
<label for='email'><span class='form'>Enter Email Address:</span></label><br>
<input type="email" name="email" size="36">
</p>
<p>
<label for='message'><span class='form'>Enter Message:</span></label> <br>
<textarea name="message" cols='48' rows='9' maxlength='300'></textarea>
</p>
<input type="submit" name='submit' value="submit"> <input type="reset">
</form>
Look carefully at your code on $email_body, you actually concatenate the $email_body with $to. the concatenation . at the end of your $email_body should be ;.

Wrong php email form

I've already spend hours on this simple code. Email arrives, but fields like name email newsletter are empty. It must be something stupid but it drives me crazzy that it's not working when it's just simple form.
Also, if you know, can you please help me prevent user from clicking on form twice and sending multiple emails. Thank you a lot!
<?php
$name = strip_tags(htmlspecialchars($_GET['name']));
$email_address = strip_tags(htmlspecialchars($_GET['email']));
$message = strip_tags(htmlspecialchars($_GET['message']));
$newsletter = strip_tags(htmlspecialchars($_GET['newsletter']));
// Create the email and send the message
$to = 'xxx#xxx.com';
$email_subject = "Message from from : $name";
$email_body = "Message from form arrived.\n\n"."Message details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $newsletter\n\nMessage:\n$message";
$headers = "From: form#xxx.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
?>
<form method="GET">
<span>Name</span>
<input type="text" name="name" placeholder="Your name"><br />
<span>E-mail</span>
<input type="email" name="email" placeholder="E-mail"><br />
<div>Message</div>
<textarea type="text" name="message" rows="8" cols="80"></textarea><br />
<input type="checkbox" name="newsletter" id="newsletter">
<label for="newsletter">I wold like to get email infromations</label><br />
<button type="submit">Send</button>
<span class="message"></span>
</form>
You may prefer HEREDOC syntax when working with lengthy strings that include variables, like you would use to generate email content. If your variables are being populated correctly, then this should work.
$email_body = <<<EOT
Message from form arrived.
Message details:
Name: $name
Email: $email_address
Phone: $newsletter
Message:
$message
EOT;

php contact form not sending content

I'm building a site in wordpress and usually use the contactform7 plugin to handle forms, but am trying to learn how to set up a form without having to rely on the plugin. I've had success with this recently, but on this latest site my content is being sent to the recipient. I get an email, but none of the values from the form are being sent.
Here's my form (I know I have to also learn about validation/ sanitization, that will be another topic!):
<form action="<?php echo home_url('/'); ?>sent/" method="POST" class="col-sm-7">
<input id="name" placeholder="name:" name="name" type="text" class="form-control" required></label>
<input id="email" placeholder="email:" name="email" type="text" class="form-control" required>
<textarea id="message" placeholder="message:" name="message" class="form-control" rows="8" required></textarea>
<button id="contact-submit" type="submit" class="btn form-control">Submit</button>
</form>
When the user presses submit the page redirects to url/sent/ in that file I have this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Now when I get the email in my inbox it shows up like this:
From: , ,
Message:
That's it. Is there something obvious I'm missing here? Any help is, as always, greatly appreciated.
thanks!
Added note: It seems that when the submit button is pressed and the page redirects, it's pulling a 404, though the url it's redirecting to IS correct, in fact, even if I just refresh the page without changing the url it drops the 404 and loads the page content. I guess this is why the content is not being sent, but then why is this happening?
As it turns out this problem is due to an issue with wordpress. Wordpress does not allow you to use the name="name" - this causes a 404 and the form values are not sent. I changes the name to 'form_name' and it works perfectly now.
Thanks for your help, hope this can help someone else too!
Thanks to Tom Elliot at http://www.webdevdoor.com for the help!
http://www.webdevdoor.com/wordpress/submitting-form-wordpress-redirects-404-page/
You should always check to see if the values are set
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
echo "message sent!";
}else{
die("Error!");
}
}else{
echo "email or name is not set";
}
Also try to change your button to <input type="submit"> even though it might not make a difference.
Other than that the code looks fine

PHP contact form not sending

I have a contact form on a page that sends the details of the form to an email address. You can view it here, www.wonder.ie
The HTML for the form is the following:
<form id="form" name="form27" class="wufoo page" enctype="multipart/form-data" method="post" action="mailer.php">
<ul>
<li id="foli1">
<label class="op" id="title1" for="Field1">Name</label>
<div><input id="Field1" name="name" type="text" class="op required" value="" maxlength="255" tabindex="1" onkeyup="handleInput(this);" onchange="handleInput(this);" /></div>
</li>
<li id="foli2">
<label class="op" id="title2" for="Field2">Email</label>
<div><input id="Field2" name="email" type="text" class="op required email" value="" maxlength="255" tabindex="2" onkeyup="handleInput(this);" onchange="handleInput(this);" /></div>
</li>
<li id="foli3">
<label class="op" id="title3" for="Field3">Inquiry</label>
<div><textarea id="Field3" name="message" class="op required" rows="10" cols="50" tabindex="3" onkeyup="handleInput(this);" onchange="handleInput(this);"></textarea></div>
</li>
</ul>
<input id="button" name="saveForm" class="btTxt submit" type="submit" value="Submit" />
</form>
And for my PHP it is this:
<?php
if(isset($_POST['submit'])) {
$to = "AN_EMAIL#ADDRESS.COM";
$subject = "Email from Wonder.ie";
$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);
} else {
echo "blarg!";
}
?>
Does everything look correct? I know I have the form names matched correctly with the PHP but I can't figure out why I'm not receiving the email you know - FYI the PHP on the site has a real email address, not AN_EMAIL#ADDRESS.COM. Once I hit the submit button I am taken to mailer.php but I notice the echo "blarg!" so my guess is the email is not being sent.
Thank you!
You should change
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
Try changing
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
This is because $_POST looks for the name of a form input, not the type.
If nothing above helps, try and see if you can debug the code.
Catching PHP mail() errors and showing reasonable user error message
In your PHP code you check if $_POST['submit'] is set, but in your HTML code you gave the submit button the name of saveForm so you should change the line
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
Hope this helped you :)
Some email servers won't accept emails without appropriate headers and you haven't provided any. This is what I use.
http://us2.php.net/manual/en/function.mail.php
$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
you are using
if(isset($_POST['submit'])) { but, you have save the submit button name assaveForm So, use if(isset($_POST['saveForm'])) {
your problem is it's pour out blarg.
it's definitely the post does not reach your
code->
mail($to, $subject, $body);
and the name of the submit should be changed to
'saveForm'
by the way :)..
just tried
mail($to, $subject, $body);
in your x.php , upload it and chage to , subject and body to right things
and if it's sent then the mail function works okay.
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
this is also a good code I have found in stackoverflow
to check if mail function works.
In your html your have
<input id="button" name="saveForm" class="btTxt submit" type="submit" value="Submit" />
but in the php file when you check for $_POST["submit"], which is not right.
You need to change if(isset($_POST['submit'])) to if(isset($_POST['saveForm']))
or if(isset($_POST['submit'])) to if(isset($_POST))
HTML form mail sending guide in PHP http://top-answers.net/webhost/web-hosting.html

Categories