I have a contact form on a website, I'm testing it live on the website and the submit button works when nothing is in the text fields, but when it's filled out it doesn't send. I've tried changing different things about the HTML like the method="post" to method="POST", I tried removing the noValidate="noValidate" from the section. I tried changing $_REQUEST to $_POST in the PHP document. I'm kind of new to contact forms and PHP so it could be something simple, but I'd love to know what I did wrong.
HTML:
<form class="form-contact contact_form" action="contact_process.php" method="POST" id="contactForm" noValidate="noValidate">
<div class="row">
<div class="col-12">
<div class="form-group">
<textarea class="form-control w-100" name="message" id="message" cols="30" rows="9" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Message'" placeholder = 'Enter Message'></textarea>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control" name="name" id="name" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" placeholder = 'Enter your name'>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control" name="email" id="email" type="email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" placeholder = 'Enter email address'>
</div>
</div>
<div class="col-12">
<div class="form-group">
<input class="form-control" name="subject" id="subject" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Subject'" placeholder = 'Enter Subject'>
</div>
</div>
</div>
<div class="form-group mt-3">
<button type="submit" class="button button-contactForm btn_4 boxed-btn">Submit</button>
</div>
</form>
And here is contact_process.php:
<?php
$to = "myemail#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = $_REQUEST['subject'];
$number $_REQUEST['number'];
$cmessage = $_REQUEST['message'];
$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "This is a test subject.";
$logo = 'img/logo.png';
$link = '#';
$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";
$send = mail($to, $subject, $body, $headers);
echo "Thank you!";
?>
Related
I am using php. I want to know how to validate the form before it sends data. Currently the code works, but it must validate before a user sends data, it just send data without validating it and store it to the db record list and needs some help around and don't know if it safe to do on the back end than client side using jquery.
Front end code / HTML
<div class="row">
<div class="col-12">
<h2 class="contact-title">Get in Touch</h2>
</div>
<div class="col-lg-8">
<form action="contact_process.php" method="post">
<form class="form-contact contact_form">
<div class="row">
<div class="col-12">
<div class="form-group">
<textarea class="form-control w-100" name="message" id="message" cols="30" rows="9" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Message'" placeholder=" Enter Message"></textarea>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="name" id="name" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" placeholder="Enter your name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="email" id="email" type="email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" placeholder="Email">
</div>
</div>
<div class="col-12">
<div class="form-group">
<input class="form-control" name="subject" id="subject" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Subject'" placeholder="Enter Subject">
</div>
</div>
</div>
<div class="form-group mt-3">
<input type="submit" value="Submit">
</div>
</form>
</div>
Back end / PHP
<?php
$to = "info#acifinance.co.za";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = $_REQUEST['subject'];
$number = $_REQUEST['number'];
$message = $_REQUEST['message'];
$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "$subject";
$message = "$message";
$logo = 'img/logo.png';
$link = '#';
$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$subject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'><strong>Message:</strong> {$message}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";
$send = mail($to, $subject, $body, $headers);
$message = $_POST['message'];
$name =$_POST['name'];
$email =$_POST['email'];
$subject =$_POST['subject'];
$send = mail($to, $subject);
// It's time to connect with mysql database
$conn = new mysqli('localhost','acifinan_acifinan','Finance_01','acifinan_acifinanc');
If ($conn->connect_error)
{die('Connection Failed:'.$conn-> connect_error); }
else
{
$stmt= $conn->prepare("insert into form(message, name, email, subject) values (? , ? , ? , ?)");
$stmt-> bind_param("ssss", $message,$name,$email,$subject);
$stmt->execute();
echo "Submitted Successfully";
$stmt->close();
$conn->close();
}
?>
I've been trying to send mail through the contact form on my website. I've used the POST method and that has seemed to work for another one of my projects, but now I'm puzzled as to what the problem is within the code. I've literally copy-pasted the code and still seem to come across the error. Also is there a way in which the name of the sender is displayed rather than the gibberish that actually ends up showing?
//PHP CODE
<?php
function has_header_injection($str){
return preg_match("/[\r\n]", $str);
}
if(isset ($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$msg = $_POST['msg'];
if(has_header_injection($name) || has_header_injection($email)){
die();
}
if(!$fname || !$lname || !$email || $msg){
echo '<h4> All fields required!</h4>';
exit;
}
$to = 'mymail8#gmail.com'; //FOR ELUCIDATION
$subject = $fname . $lname ."sent you via your contact form";
$message = "Name: ". $fname . $lname;
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap($message, 100);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: $lname <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
mail($to, $subject, $message, $headers);
}
else{
echo "failed to send mail!";
}
?>
//FORM (HTML)
<form action="mail-handler.php" class="msg" method="POST">
<div class="name">
<div class="fname">
<div>FIRST NAME</div>
<input type="text" placeholder="Enter your first name" id="fname" name="fname">
</div>
<div class="lname">
<div>LAST NAME</div>
<input type="text" placeholder="Enter your last name" id="lname" name="lname">
</div>
</div>
<div class="add-info">
<div class="mail">
<div>E-MAIL</div>
<input type="text" placeholder="Enter your email" id="mail" name="email">
</div>
<div class="phn">
<div>PHONE NUMBER</div>
<input type="text" placeholder="Enter your phone number" id="phn" name="phn">
</div>
</div>
<div class="msg-box">
<div>MESSAGE</div>
<input type="text" placeholder="Enter your Message" id="msg" name="msg" >
</div>
<button type="submit" id="sub" name="aubmit">Submit</button>
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I am trying to create a contact me form with HTML and PHP but whenever someone try to send me a message it gives an error.
here is my HTML code:
<div class="col-lg-9">
<form class="row contact_form" action="contact_process.php" method="post" id="contactForm" novalidate="novalidate">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter your name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email address">
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Enter Subject">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" name="message" id="message" rows="1" placeholder="Enter Message"></textarea>
</div>
</div>
<div class="col-md-12 text-right">
<button type="submit" value="submit" class="primary_btn">
<span>Send Message</span>
</button>
</div>
</form>
</div>
and it is associated with a PHP file contact_process.php and here it is:
<?php
$to = "mygmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = $_REQUEST['subject'];
$number = $_REQUEST['number'];
$cmessage = $_REQUEST['message'];
$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "You have a message from your Bitmap Photography.";
$logo = 'img/logo.png';
$link = '#';
$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";
$send = mail($to, $subject, $body, $headers);
?>
So can you please let me know where I am doing it wrong
Change the code ( of all variable):
$number = $_REQUEST['number'];
to
$number = $_POST['number'];
And for check mail sent change:
$send = mail($to, $subject, $body, $headers);
to
if(mail($to, $subject, $body, $headers)){
echo 'done';
}else{
echo 'mail not sent';
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I recently created a contact form with php as well but everytime I click submit, I only get an undefined error, nothing else. Can anyone please give an insight into what I missed?
HTML
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.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" name="Phone" 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>
PHP
<?php
if (isset($_POST["submit"])){
$EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['Subject']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= "$Subject: ";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message; $Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}
?>
I only get an undefined error on the html nothing else
There were some case sensitivity issues in your post and I corrected it and commented at corresponding lines.
Check it
<?php
if (isset($_POST["submit"]))
{ $EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['subject'])); //s should be small
$Message = trim(stripslashes($_POST['message'])); // m should be small
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject; // copy paste is very bad
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page i
if ($success) { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; }
else { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; }}?>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.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" name="Phone" 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">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Submit" />
</div>
</div>
</form>
I know this question has been asked before, I have researched but can't seem to get to the route of the problem. I'm hoping someone can help. Ok so form seems to be working in as much as the form has to be completed before it allows to be sent, the form sends and an email arrives. Unfortunately the email fields are blank.
So here's the php:
<?php
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; ch***t=utf-8' . "\r\n";
$Body .= 'From: '. $EmailFrom . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$EmailFrom = "info#mywebsite.com";
$EmailTo = "info#mywebsite.com";
$Subject = "Contact Form";
$Name = trim(stripslashes($_POST['Name']));
$Email = trim(stripslashes($_POST['Email']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://mywebsite.com/formredirect.html");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" . "\r\n");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=formredirect.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=error.html\">";
}
?>
My HTML:
<form method="post" action="contact.php" name="contact" id="contact">
<div class="row 50%">
<div class="6u 12u(mobile)">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="6u 12u(mobile)">
<input type="text" name="email" id="email" placeholder="Email" required />
</div>
</div>
<div class="row 50%">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" required></textarea>
</div>
</div>
<div class="row 50%">
<div class="12u">
<input type="submit" class="button" id="submit" value="submit"></a>
</div>
</div>
</form>
I appreciate it's probably me being really dense. I'm new to this so am fumbling my way around. Any help would be greatly appreciated. Many thanks!
The $_POST keys are CaSe SenSitIve
$_POST['Name'] is different from $_POST['name']
Either change the form name attribute or the POST keys so they match, i.e.:
FORM
<input type="text" name="name" id="name" placeholder="Name" required />
...
<input type="text" name="email" id="email" placeholder="Email" required />
...
<textarea name="message" id="message" placeholder="Message" required></textarea>
...
PHP
...
$Name = trim(stripslashes($_POST['name']));
$Email = trim(stripslashes($_POST['email']));
$Message = trim(stripslashes($_POST['message']));
...