This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to create a html and php contact form but it doesn't seem to be working.
I'm sure it's a simple answer but I can't figure it out.
Can anyone tell me were i'm going wrong?
It doesn't redirect to the mail.php page.
<div class="col-lg-8">
<form action="mail.php" class="form-horizontal" id="contactForm" name="contactForm" method="post" >
<div class="form-group" >
<label for="first-name" class="col-lg-2">First name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="first-name" name="first-name" placeholder="What your mam calls you ">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="last-name" class="col-lg-2">Last name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="last-name" name="last-name" placeholder="What your army buddies call you">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="email" class="col-lg-2">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter your email address, we won't send you junk mail">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="website" class="col-lg-2">Your current website</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="website" name="website" placeholder="If you have one.">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="message" class="col-lg-2">Any Message</label>
<div class="col-lg-10">
<textarea name="message" id="message" name="message" class="form-control"
cols="20" rows="10" placeholder="Maybe tells us a bit about your business. I'll start, mine is web development. ;)"></textarea>
</div>
</div><!--end form group-->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div><!--end of row-->
</section>
</div>
mail.php
<html>
<body>
<?php
if(isset($_POST['submit'])){
$to = "info#wonderfulwebsites.ie"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$f_name = $_POST['first_name'];
$s_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $f_name . " " . $s_name . " wrote the following:" . "\n\n" . $_POST['message'] "\n" $website = $_POST['website'];
$message2 = "Here is a copy of your message " . $f_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $f_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
</body>
</html>
Change this:
<button type="submit" class="btn btn-primary">Submit</button>
to this:
<input type="submit" class="btn btn-primary" value="Submit" />
Related
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!
I want to get an email from an html form. I was following a tutorial that used PHP, which I have never used before, and it doesn't seem to work. After I hit submit, the php page opens, but an email is not sent to me. Can someone please help me.
HTML(A couple inputs like name and email, etc. and a submit button at the bottom):
<form action="contact_form_process.php" method="POST">
<section id="contact" class="py-3">
<div class="container">
<div class="row">
<div class="col-md-9 mx-auto">
<div class="card p-4">
<div class="card-body">
<h3 class="text-center lato-font">Please Fill Out This Form To Contact Us</h3>
<hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="first_name" type="text" class="form-control" placeholder="First Name">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="last_name" type="text" class="form-control" placeholder="Last Name">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="email" type="text" class="form-control" placeholder="Email">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="phone_num" type="text" class="form-control" placeholder="Phone Number">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control" name="" id="message" rows="10" placeholder="Message"></textarea>
</div>
</div>
</div>
</div>
<div class="card-footer">
<input type="submit" class="btn btn-outline-success btn-block" value="Send">
<button type="submit" class="btn btn-outline-success btn-block">Send</button>
</div>
</div>
</div>
</div>
</div>
</section>
</form>
Here is my PHP:
<?php
// Subject and Email Variables
$emailSubject = 'Test Email';
$webMaster = 'tamiroffen#gmail.com';
// Gathering Data Variables
$first_nameField = $_POST['first_name'];
$last_nameField = $_POST['last_name'];
$emailField = $_POST['email'];
$phone_numField = $_POST['phone_num'];
$messageField = $_POST['message'];
$body = <<<EOD
<br><hr><br>
Email: $emailField <br>
Name: $first_nameField <br>
Phone Number: $phone_numField <br>
Message: $message <br>
EOD;
$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
?>
Thank you!
mail() needs to be enabled as #esqew points out in his comment - this would look something like this (this is on my BigRock environment YMMV) -
ini_set("include_path", '<path to php>' . ini_get("include_path") );
require_once "Mail.php";
Also, if you are using GMail, you also need to set up GMail to allow sending mail through SMTP - https://support.google.com/mail/answer/7126229?visit_id=636741342414654323-2069917091&hl=en&rd=1
$webMaster = 'tamiroffen#gmail.com';
I assume this is the address where the emails are sent to. From what I've heard, PHP mail() function doesn't work for gmail(if it's on the receiver end).
You can just use another email provider like hotmail or any and forward emails from it to your gmail
Watch this video if you need more info on it, How to create a PHP Contact Form Tutorial
Trying to send an email not displaying any error message or anything just refreshing the page that it.
I am trying an email through contact form but its not working.Here is the code for that. tried by doing echo but it is not working .not displaying any error as well.
contact and contactus:
<?php ob_start();
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$headers = "From: " .$email . "\r\n" .
"CC: xyz#gmail.com";
mail($to,$subject,$txt,$headers);
header("Location: contact.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>HMS System</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h2>Contact Us Form</h2>
</div>
<form class="form-horizontal" action="contactus.php" method="post" role="form">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="name" placeholder="Insert your Name" id="name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-8">
<input type="email" class="form-control" name="email" placeholder="Email Address" id="email">
</div>
</div>
<div class="form-group">
<label for="subject" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="subject" placeholder="Subject" id="subject">
</div>
</div>
<div class="form-group">
<label for="comments" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-8">
<textarea class="form-control" rows="10" name="comment" style="resize:none;"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-8">
<input type="submit" value="Send Message" name="submit_contact" class="btn btn-block btn-danger" id="subject">
</div>
</div>
</form>
</section>
</article>
</div>
<div style="width:50px;height:50px;"></div>
</body>
use this
if(isset($_POST['submit_contact']))
If you are doing it from your local server, you will have to setup your mail properly in ini then only you will be able to send mail from your local machine.
A better approach would be to wrap the mail function in a statement. Mail is returning a bool value. And beginn as simple as possible.
error_reporting (E_ALL);
$to = "mail#whatever.com"
// ... your settings ...
if ( mail ( $to , $subject , $message ) ) {
// do when ok
}
else {
// do when error
}
To verify that you receive all values from $_POST do
echo '<pre>';
print_r ($_POST);
echo '</pre>';
I have a contact form using php however when I test the form I receive an email with no content in the fields, I have checked that the fields match with the php but cannot seem to identify the problem.
The form html part of the contact page:
<section id="contact-page">
<div class="container">
<div class="center">
<h2>Send us a Message</h2>
<p class="lead">Thanks for your enquiry we will respond within 24 hours. * Indicates required information</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form action="sendemail2.php" method="post" id="main-contact-form" class="contact-form" name="contact-form" >
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" name="tel"class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" name="company" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required>
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
</div><!--/.row-->
</div><!--/.container-->
</section><!--/#contact-page-->
The Php script:
<?php
header('Content-type: application/json');
$status = array(
//'type'=>'success',
//'message'=>
'Thank you for contacting us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST["name"]));
$email = #trim(stripslashes($_POST["email"]));
$subject = #trim(stripslashes($_POST["subject"]));
$tel = #trim(stripslashes($_POST["tel"]));
$company = #trim(stripslashes($_POST["company"]));
$message = #trim(stripslashes($_POST["message"]));
$email_from = $email;
$email_to = 'xxx#xxx.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Phone Number: ' . $tel . "\n\n" .'Company Name : ' . $company . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Any help would be appreciated.
Thanks
or use
<form method="post" id="main-contact-form" class="contact-form" name="contact-form" >
in place of
<form action="sendemail2.php" method="post" id="main-contact-form" class="contact-form" name="contact-form" >
This bootstrap using html/php contact form sends me an email just fine, but without any of the users input information!
The email I received looks empty like this:
Name:
Email:
Subject:
Message:
The html for the form is:
<section id="partner">
<div class="container">
<div class="center wow fadeInDown">
<h2><br>Contact</h2>
<p class="lead">Send a general enquiry here or order your service here.</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
</div><!--/.row-->
</div><!--/.container-->
</section><!--/#contact-page-->
The PHP for the form looks like this:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'heerschapnikki#gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Why isn't it working properly? The message "thank you for contacting us" pops up fine and the email sends through, but non of the users information comes through.
Try to print the values you are sending. So use this:
var_dump( $_POST['name'] );
var_dump( $_POST['email'] );
etc...
Doing this, you will be able to see wether your fields are even posted correctly.
the fields have no id param. in the post you get the "id" nor the "name"
<label>Name *</label>
<input type="text" id="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" id="email" class="form-control" required="required">