PHP and contact form - php

I'm having trouble getting the php script correct for my contact form. I managed to have it email me but it doesn't show a name, email or text in the email.
PHP Script
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'myemail#email.comm';
$subject = 'Hello';
mail ($to, $subject, $message, "From: " . $name);
echo "Your Message has been sent.";
?>
Contact Form
<form role="form" action="contact.php">
<div class="form-group">
<label for="InputName">Name</label>
<input name="name" type="text" class="form-control" id="InputName" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="InputEmail">Email Address</label>
<input name="email" type="email" class="form-control" id="inputEmail" placeholder="Enter email">
</div>
<div class="form-group">
<label for="InputText">Message</label>
<input name="message" type="text" class="form-control" id="InputText" placeholder="Enter Text">
</div>
<button name="submit" type="submit" class="btn btn-default">Send</button>
</form>

You're using POST variables, yet your form being the following:
<form role="form" action="contact.php">
doesn't have a POST method defined. Form defaults to GET when a method isn't defined.
Therefore, you will need to change your form to
<form role="form" action="contact.php" method="post">
From a comment you left:
"I'm still having a problem where instead of giving me the email they input it gives an email created by the hosting company which is no good."
A: It's most likely because of how you're using From: in your code, which is the person's name. Mail is expecting an Email address.
Replace:
mail ($to, $subject, $message, "From: " . $name);
with:
mail ($to, $subject, $message, $header);
and adding the following under $subject = 'Hello';
$header = "From: ". $name . " <" . $email . ">\r\n";
That way, you will see the person's name in the Email, while having a valid "From" header.
Visit http://php.net/manual/en/function.mail.php for more information on mail headers.
Additional notes:
I also suggest that you test if any of the fields aren't left empty using:
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']))
{
// execute code
}
Otherwise, anyone could send an Email without any information at all.
You can also add an else{} to it like else { echo "Please fill in all the fields."; }
This is a very basic method.

Your form needs the attribute
method="POST"
Without this, the browser defaults to method="GET", which submits the form in the url, e.g., http://example.com/default.php?name=First%20Last&email=...

You should specify POST method:
<form role="form" method="POST" action="contact.php">

Related

HTTP ERROR 405 on submitting HTML form when using PHP mail

I am trying to send mail to my account using PHP but it show HTTP ERROR 405 when i submit my HTML form.
Here's my HTML form:
<form action="spledmailer.php" method="post" enctype="text/plain" class="needs-validation" novalidate>
<div class="container text-center w-50">
<input type="text" class="form-control" placeholder="Enter Your Name" name="name" required><br>
<input type="email" class="form-control" placeholder="Enter Your E-mail" id="email" name="Email" required><br>
<textarea class="form-control" rows="5" placeholder="Tell Us About Your Request" name="query" required></textarea>
</div>
<div class="container" style="margin-top: 3%;">
<div class="row">
<div class="col text-center">
<button type="submit" class="btn btn-outline-dark">Send Query</button>
</div>
</div>
</div>
</form>
And here's my PHP code:
if(isset($_POST['submit'])) {
$to = "example#gmail.com";
echo $subject = "Form Tutorial";
echo $name_field = $_POST['name'];
echo $email_field = $_POST['Email'];
echo $message = $_POST['query'];
$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 "blarg!";
}
If you are using a web host, you might want to ask them if they block POSTing to certain files, or just the php mail function in general. This has been a problem for me in the past, so contact their support.
Also, make sure you enable error reporting or look through your error log. That can probably tell you what the problem is. See here: How can I get useful error messages in PHP?

How to make my current PHP contact form check the email address input is valid? [duplicate]

This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 5 years ago.
Fixed:
There was a small error in my code, I had changed the "type" to "text" somehow in the email input, where as it should have been
type="email"
and not
type="text"
I have made a php contact form which I use on website I make and all works fine.
I thought the form was checking the email address had been entered but I have now realised people can just put their name in the email input and not their full email address (i.e with an # symbol and a .com or .co.uk at the end)
I am not so great at PHP so I have had a go but emails from the form where I haven't put a valid email address in still seem to go through despite the statements I added
I tried to define some error variables and set with empty values as follows:
$nameErr = $emailErr = $genderErr = $websiteErr = "";
and then adding these if else statements to my form php:
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
So the my code looks as follows:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = 'me#myemailaddress.co.uk';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
?>
<div class="container-fluid">
<div class="about-msg">
<h2>CONTACT US</h2>
<div class="container-fluid contactform">
<div class="col-md-7 contactform-padding">
<br>
<h1>GET IN TOUCH</h1>
<p>Please drop us a message if you have any enquires. We always aim to reply
within 24 hours.</p>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $message, $headers)) {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
echo '<p>Thanks for getting in touch. Your message has been sent & we
will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="contact-us.php" data-ajax="false" method="POST"
enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label " for="name">
<b>NAME</b>
</label>
<input class="form-control" id="name" name="name" type="text"
placeholder="name">
</div>
<div class="form-group col-md-6">
<label class="control-label requiredField" for="email">
<b>EMAIL</b>
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="email" name="email" placeholder="email"
type="text"/>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label requiredField" for="contact-subject">
<b>SUBJECT</b></label>
<input type="text" name="subject" class="contact-subject form-control"
id="contact-subject" placeholder="subject">
</div>
</div>
<div class="form-group">
<label class="control-label " for="message">
<b>MESSAGE</b>
</label>
<textarea class="form-control" cols="40" id="message" name="message"
rows="10" style="height: 275px !important;"></textarea>
</div>
<div class="form-group">
<label><b>*What is 2+2? (Anti-spam)</b></label>
<input name="human" placeholder="Type Here"></div>
<br>
<button class="btn btn-success" name="submit" type="submit" value="submit">
SEND
</button>
</form>
</div>
However this lets me send an email despite putting my email address as just my name!
Could someone please take a look and see if I have put the code in the right place or make a suggestion as to how to fix it? I am just learning PHP so I may have made an error in my code or maybe need to take the if statement out for the submit?
Thanks for any help!
Use PHP Filter Validation
$email_b = "me#myemailaddress.co.uk";
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.\n";
} else {
echo "This ($email_b) email address is considered invalid.\n";
}
I think you want the user to enter a valid email address, right?
You could change the type to email
<input class="form-control" id="email" name="email" placeholder="email" type="email"/>
Well you could either go for regex but this is also an option

FILTER_VALIDATE_EMAIL saying a valid email is invalid

I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.

PHP contact form wont send message [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form id="contact-form" action:"" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="action" value="submit">
<label for="name">
Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" placeholder="Enter email" required /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="General">General</option>
<option value="Hiring">Hiring</option>
<option value="My Work">My Work</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="message">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required
placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send Message</button>
</div>
</div>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$subject=$_REQUEST['subject'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($subject=="")||($message==""))
{
echo "All fields are required.";
}
else{
$from="From: $name, $email";
$subject="Message sent using your contact form";
mail("nilsbittmannmyers#yahoo.co.uk", $subject, $message, $from);
echo "Message sent!";
}
}
?>
THANKS :)
Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD
You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.
You're also missing a name attribute for the email and name input fields.
I.e.: name="email" and name="name"
Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";
It's best to use something like this:
$from = "From: ". $name . " <" . $email . ">\r\n";
as your header's 4th parameter.
That way you will have a proper From Email with the person's name appearing in the Email also.
Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.
Consult the PHP.net Website for details on mail() and headers:
http://php.net/manual/en/function.mail.php
a few headers options:
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
in your case would be:
$from = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";
Sidenote:
I noticed you are using enctype="multipart/form-data"
Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.
Footnotes:
Using error reporting would have signaled these errors, including Undefined index... warnings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
placed just beneath your opening <?php tag.

Simple contact form won't work - php

Again - I've looked and troubleshooted for about an hour and cannot get the php contact form to work. I get the "Thank you!" email, but I haven't received the email to my inbox ever. I've done it about 20 times.
<div class="container">
<div class="row">
<div class="col-lg-12">
<h5>Want to contact me quickly? Use this form!</h5>
<form id="contact" method="post" action="submit.php" class="form" role="form">
<div class="row">
<div class="form-group col-lg-4">
<label>Name</label></br>
<input type="text" name="name" placeholder="Enter your full name" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Email address</label></br>
<input type="email" name="email" placeholder="Enter your email address" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Subject</label></br>
<input type="text" name="subject" placeholder="Subject">
</div>
<div class="form-group col-lg-12">
<label>Message</label>
<textarea name="comments" name="message" data-provide="markdown-editable" rows="6" placeholder="Let's chat!" style="width:100%"></textarea>
</div>
<div class="form-group col-lg-12 form-action">
<input type="hidden" name="Submit" value="contact">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
PHP:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='MY EMAIL ADDRESS';
$from='website';
if (mail($to, $subject, $name, $email, $comments))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}?>
Any help is greatly appreciated. I have my MAMP server on and I get the "thank you!" message every time, but I haven't gotten any emails. Also, I'd really like it to show the "thank you!" and then return to my webpage, but I can't even get the php to work so I maybe should hold off on that part.
You have a wrong call of mail() function.
It should be like this:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So, for your case:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='email#email.com';
$from='website';
$message = 'Message: ' . $comments . "\r\n";
$message .= 'From: ' . $from;
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}
?>
The mail() function simply returns true if the message has been queued without error in your system... but it doesn't tell you whether the email was actually sent or not. I see you're using Mac OS X, so you'll have to configure SMTP settings (provided by your ISP) in the PHP environment to get mail sending working.
Alternately, you can use a service like Amazon SES to send emails through PHP.

Categories