Website Contact Form Not Sending Email - php

So I recently uploaded my first website to an iPage server. The website runs perfectly with the exception of the Contact Form which for some reason refuses to send any email whatsoever from the form.
I use a PHP Script with the Post Method, and tried to fix my code multiple times to correct any error I might have entered, but so far to no avail. Here is the code:
HTML:
<
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
Can someone help me with this? It might be an error with my hosting server, or an error with my code. Your help is very much appreciated
Additional Comments: Followed the help provided (thank you for that) I made the necessary changes to the HTML and PHP, but the form is still not functional. It does not echo failure or redirect to the confirmation page, and upon inspecting the element with Firefox, I notice that upon hitting the submit button a subdivision appears under it saying "Sending....". But no email is sent, no message is echoed or page opened.

From what i see in the code you posted, the PHP mailing script won't work as you are checking if a POST variable with the name 'submit' exists which it does not as in your form the submit button does not have a name attribute.
Try giving the submit button a name and put that name in the PHP if statement.

Submit button name is missing in your form, You need to add name into your submit button,
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
instead of
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">

Your HTML part should be something like below,
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
In above, I have added name="sendemail" in submit button.
And your PHP script that sends email should be like
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>

Related

Question about contact form HTTP ERROR 405 crash

so I am trying to make a link to contact form but it seems to be crashing and I do not know why is this happening. I double checked the link and variable name, but seems like error 405 happens without stopping every time I click.
This is my html part
<h3>Contact Form</h3>
<div class="containerofcontact" style="text-align: left !important;">
<form method="POST" action="contactform.php">
<label for="name">Name</label>
<input type="text" id="Name" name="Name" placeholder="Your name..">
<label for="email">Email</label>
<input type="text" id="Email" name="Email" placeholder="Your Email">
<label for="subject">Subject</label>
<textarea id="Subject" name="Subject" placeholder="Place Detail of Concern" style="height:200px"></textarea>
<input type="submit" value="Send">
</form>
</div>
This is php part
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$text = $_POST['Subject'];
$mailTo = "blankforshare";
$headers = "From: ".$email;
$txt = "You have received an e-mail from ".$name."\n\n".$text;
mail($mailTo, $headers, $txt) or die("Error!");
header("Location: index.html");
?>
Thank you for the help.

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 configure email address on submit button in html contact form

I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class

Contact form with reCaptcha [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I know this may have been submitted before (sorry)
I have basic form, these are the details id like to be sent, however i cannot get the reCaptcha to work with it. I have googled all day, but when i try other peoples code (amending to fit mine) it doesnt seem to work.
I would like: Name, Email, Number, newsletter (yes/no) and recaptcha to be sent/work.
Can someone please give me an idea where i may be going wrong? what i may need to add?
Thanks in advance!
Here is my Form (html)
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Here is my php (basic)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
//$password = $_POST['password'];
//$keyy = $_SERVER['UNIQUE_ID'];
$msg = "Name: $name\r\n \r\n";
$msg .= "Email: $email\r\n \r\n";
$msg .= "Number: $number\r\n \r\n";
$msg .= "Message: $message\r\n \r\n";
$recipient = "info#islandwebdesign.co.uk";
$subject = "New Website Request";
$mailheaders = "From:$email";
//$mailheaders .= "Reply-To:$email";
mail($recipient,$subject,$msg,$mailheaders);
header("Location: contactus.php?msg=1");
?>
First of all make sure that you've included the necessary JavaScript resource to render reCAPTCHA widget properly, like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the reference:
Displaying the widget
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
g-recaptcha-response - a POST parameter in the submitted form
grecaptcha.getResponse(widget_id) - will provide the response after the user completes the captcha.
A string argument to the callback function specified in the config object passed to the render method.
Here's the reference:
Verifying the user's response
For your purpose use g-recaptcha-response to get the user's response. So your code should be like this:
HTML
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" name="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Add a name attribute in your submit button.
Form_Activation.php
<?php
if(isset($_POST['submit'])){
//your site secret key
$secret = 'XXXXXXX_Secret-key_XXXXXXX';
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
// so on
}else{
// failure
}
}
}
?>
Don't forget to add your secret key in $secret variable.

Guidance Needed with Basic Contact Form?

I'm totally new to web design and have gotten stuck creating a contact form. FWIW The original 'frame' of the site came from a template I downloaded.
This is what my form looks like on the index.html side of things:
<div class="col-sm-6 col-md-6">
<form action="contact.php" method="post">
<div class="form-group">
<!--<label for="name">name</label>-->
<input type="text" id="name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="email">email</label>-->
<input type="text" id="email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="message">message</label>-->
<textarea id="message" class="form-control" rows="7" placeholder="Write a message">
</textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
And this is what the contact.php looks like:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "exampleemail#gmail.com";
$subject = "New Contact Us Enquiry";
$text = "A visitor of ninadaviesopticians.co.uk has submitted the following enquiry.\n\nName:$name\n\nEmail: $email\n\nMessage: $message\n\nPlease respond to this enquiry within 24 hours";
mail ($to, $subject, $text);
header("Location:index.html");
?>
When I fill in the contact form, it sends the email to the desired address and gives me the 'A visitor of.....' text without an issue. However, it leaves 'Name', 'Email' and 'Message' fields blank, as if the user never entered them.
Any ideas? Appreciate the help in advance.
Your fields require the "name" attribute to be filled to be sent in POST or GET data, not ID.
So for example, your name field would be this :
<input type="text" name="name" class="form-control" placeholder="Name" />
You can have both an id and a name, though the id is not used for posting data.
Ref : http://www.php.net//manual/en/reserved.variables.post.php
To post data via forms, the fields need to have the name attribute:
<input type="text" id="name" name="name" class="form-control" placeholder="Name" />
The ID is only really useful for client side tools.

Categories