html/php contact form template not sending email - php

I'm using an html/php form template that I found. Every time I try to submit a completed form, I get a 405 error.
I want the submit button to send the contents of the form to my email address.
Heres the html:
<div class="col-md-6">
<form action="../assets/php/actionpage.php" name="emailform" method="POST">
<div class="row mb--20">
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Name" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="E-mail" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Phone" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Website" />
</div>
<div class="col-12 form-group">
<textarea cols="30" rows="5" class="form-control" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="col-lg-6 offset-lg-6 form-group">
<button class="btn btn-block btn--border-primary"><input type="submit" value="submit"></button>
</div>
</div>
</form>
</div>
This doesn't look out of the ordinary to me. So I think the problem is with the php.
actionpage.php:
<?php
if ( !empty($_POST) ) {}
if(!isset($_POST['submit']))
{
//This page shouldn't be accesed directly
echo "ERROR: you need to submit the form!";
exit;
}
// Collect
$name = $_POST['Name'];
$email = $_POST['E-mail'];
$phone = $_POST['Phone'];
$website = $_POST['Website'];
$message = $_POST['Message'];
// Validate
if(empty($name)||empty($email)||empty($message))
{
echo "Name, E-mail, and Message are mandatory!";
exit;
}
$email_from = $email;
$email_subject = "New Form Submission";
$email_body = "You have a new email from: $name\n their email: $email\n phone number: $phone\n and their website: $website\n Here is their message: $message";
$to = "######.##" // my email
$headers = "From $email_from \r\n";
// Send
mail($to, $email_subject, $email_body, $headers);
// redirect
header('Location: index.html');
?>
I am very new to php, so I decided to use a template.
I'm testing this using LiveServer in VScode. Is that having an impact?

Related

405 Method Not Allowed - Contact Form HTML + form.php

I am trying to enable a simple contact form, however, every time on the button submit, I get redirected to /form.php with the error 405 Method Not Allowed in the console.
Here is the form in index.html
<div class="container text-center pt-5 pb-5" id="contact">
<h1 class="word">Contact Us</h1>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<form action="form.php" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control text-white" id="name" name="name" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control text-white" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control text-white" id="message" name="message" rows="3" placeholder="Enter your message"></textarea>
</div>
<button type="submit" class="btn btn-primary mt-3 btn-lg mt-4">Send Message</button>
</form>
</div>
</div>
</div>
</div>
And here is my form.php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set the recipient email address
$to = "hello#doctrina.ai";
// Set the email subject
$subject = "Message from Contact Form";
// Build the email content
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers
$email_headers = "From: $name <$email>";
// Send the email
mail($to, $subject, $email_content, $email_headers);
// Redirect to the thank you page
header('Location: thanks.html');
}
?>
I have thanks.html page as well. I am using Vercel for deployment. Thank you in advance for your help!

Why isn't my PHP form working? I do receive the email but there is no content in it

PHP form on my website isn't responding well. It would be appreciated if someone could help me.
This is the HTML Code of the form
<form action="contactform.php" method="post" name="form" class="p-5 bg-white">
<div class="row form-group">
<div class="col-md-6 mb-3 mb-md-0">
<label class="text-black" for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-md-6">
<label class="text-black" for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="email">Email</label>
<input type="email" id="email" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="subject">Subject</label>
<input type="subject" id="subject" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="mssg">Message</label>
<textarea name="mssg" id="mssg" cols="30" rows="7" class="form-control" placeholder="Write your notes or questions here..."></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary py-2 px-4 text-white">
</div>
</div>
</form>
This is the PHP Code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "alyyashar#gmail.com";
$subject = "New email from your site!";
$fname = $_POST['fname'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$title = '<h3>Hello! You have received a new mail from your website!</h3>';
$body = "$title
<br/>
<b>From:</b> $fname
<br/>
<b>E-Mail:</b> $email
<br/>
<b>Message:</b>\n$message
<br/>
<br/>";
if (mail($to, $subject, $body, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$fname.", We will contact you shortly!</h1>";
} else {
echo "Something went wrong!";
}
}
?>
<br>
Back to Homepage
This is the email I receive
Screenshot of the email
When I enter information into the form and click send message, I do receive the email but there is no content in it.
The form elements have no name attributes, which is what the browser uses to send their name/value pairs to the server. So while it's posting the form, none of the values are being included.
For example, this:
<input type="text" id="fname" class="form-control">
Should be this:
<input type="text" id="fname" class="form-control" name="fname">
The same fix would need to be repeated for the remaining form elements.

trying to get feedback as email from a contact form

I have a created a html form and a php file (mail.php) and i have added this in my form <form action="mail.php" method="POST">
but When I try to submit the form, instead of emailing me and providing the thank you message, it downloads the php page. When I run the .html page in my browser, it shows up fine but when I click send it downloads mail.php, instead of working.
Any suggestions?
ok this is the code
mail.php:
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$formcontent=" From: $name \n telephone: $telephone \n comments: $comments";
$recipient = "lllkk#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" ;
test.html:
<form action="mail.php" method="POST">
<div class="row">
<div class="form-group col-md-8 col-md-offset-1">
<input type="text" name="name" placeholder="Name" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="tel" name="telephone" placeholder="telephone" class="form-control">
</div>
<div class="clearfix"></div>
<div class="form-group col-md-8 col-md-offset-1">
<textarea class="form-control" name="comments" placeholder="Comments" rows="6"></textarea>
</div>
</div>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

I need a contact submit form fixed

Ok so I have the modal toggling, that is easy with bootstrap. But now, whenever I click submit the form gets redirected to php/contact-process.php. It looks like the php is processed but then just stays at a white screen, and not being redirected and no email is received to my main email(i put example#gmail.com for stackexchange question). Please help me.
<!--Modals for Contact-->
<div class="modal fade" id="contact" role = "dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal contact" name="contact" action="php/contact-process.php">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4>Contact Us</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" name="name" class="form-control" id="contact-name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" name="email" class="form-control" id="contact-email" placeholder="you#example.com">
</div>
</div>
<div class="form-group">
<label for="contact-message" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea name="usertext" name="message" id="" rows="8" class="form-control" style="resize:none;"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
Close
<button class="btn btn-primary" type="submit" value="Send!">Send</button>
</div>
</form>
</div>
</div>
</div>
CONTACT-PROCESS.PHP:
<?php
//add the recipient's address here
$myemail = 'example#gmail.com';
//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
//generate email and send!
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect
header("Location: index.php");
exit();
}
?>

Contact form using html and php

I am trying to create a contact form using html and php, but when I submit the form instead of running the php file it displays the php file on my screen. I do not get any error message. This is my code:
HTML
<div class="container">
<h1>Contact Me</h1>
<div class="well">
<p class="lead">
Do you have any question? Want to write for us? Please use the below contact form and send a message. I'll reply you as quick as possible.
</p>
</div>
<div class="contact-form">
<form method="post" action="form.php" class="form-horizontal col-md-8" role="form">
<div class="form-group">
<label for="name" class="col-md-2">Name</label>
<div class="col-md-10">
<input name="name" type="text" class="form-control" id="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-2">Email</label>
<div class="col-md-10">
<input name="email" type="email" class="form-control" id="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="subject" class="col-md-2">Subject</label>
<div class="col-md-10">
<input name="subject" type="subject" class="form-control" id="subject" placeholder="Subject">
</div>
</div>
<div class="form-group">
<label for="message" class="col-md-2">Message</label>
<div class="col-md-10">
<textarea name = "message" class="form-control" id="message" placeholder="Message"></textarea>
</div>
</div>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<div class="form-group">
<div class="col-md-12 text-right">
<button type="submit" class="btn btn-lg btn-primary">Submit your message!</button>
</div>
</div>
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];;
$to = 'me#mariachavez.co';
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, you can send an email to me#mariachavez.co and I'll get in touch soon</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
You can't execute PHP files without server, if you don't have it, you need to install one, for example WAMP or XAMPP.
If you have one, try restarting it, or , if you haven't, configure HTTP server to execute PHP files.

Categories