php mail with .js validation -> can't see any mistakes - javascript

I got this piece of code from a free template and I followed all the instructions that came with it, everything seems fine but mail doesn't go trough.
HTML:
<!--Start Contact form -->
<form name="enq" method="post" action="email/" onsubmit="return validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name.." />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email.." />
<textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Message.."></textarea>
<div class="actions">
<input type="submit" value="Send!" name="submit" id="submitButton" class="btn btn-info pull-right" title="Send!" />
</div>
</fieldset>
</form>
<!--End Contact form -->
PHP
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="email#sample.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../contact.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
JavaScript
function validation()
{
var contactname=document.enq.name.value;
var name_exp=/^[A-Za-z\s]+$/;
if(contactname=='')
{
alert("Name Field Should Not Be Empty!");
document.enq.name.focus();
return false;
}
else if(!contactname.match(name_exp))
{
alert("Invalid Name field!");
document.enq.name.focus();
return false;
}
var email=document.enq.email.value;
//var email_exp=/^[A-Za-z0-9\.-_\$]+#[A-Za-z]+\.[a-z]{2,4}$/;
var email_exp=/^\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if(email=='')
{
alert("Please Enter Email-Id!");
document.enq.email.focus();
return false;
}
else if(!email.match(email_exp))
{
alert("Invalid Email ID !");
document.enq.email.focus();
return false;
}
var message=document.enq.message.value;
if(message=='')
{
alert("Query Field Should Not Be Empty!");
document.enq.message.focus();
return false;
}
return true;
}
I don't get any errors but mail doesn't simply go trough, checked spam etc.

I hope you changed
$to="email#sample.com";
to your actual e-mail.. I can't see anything else.

Seems it was due to webhost(mailserver). Works like a charm on another webhost.

Related

$error messate POST into div in index.php

This is my first question on this site however I have been here millions of times looking for answer and I hope you can help me out this time as I wasn't able to figure it out myself.
I am not very skilled when it comes to coding but was hoping to amend the php contact form I have found online (it's free), the form itself is worki9ng but what I am struggling with is to set the errors to be printed on the main page where the form is, at them moment the yare displayed on the contact.php form which is where the php code is and I would like it all displayed in a div for example on index.php just under the form.
my contact.php
<?php
if (isset($_POST['submit'])) {}
$sendTo = "email address";
$subject = "msg content";
$headers = 'From: email address' . "\r\n";
$name = #$_POST['name'];
$email = #$_POST['email'];
$message = #$_POST['message'];
$okMessage = 'thank you';
$errorMessage = 'Please fill all the fields';
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "----------------------------------------";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
$emailText = "Name: $name \n Email: $email \n Message: $message";
if (isset($data->success) AND $data->success==true) {
mail($sendTo, $subject, $emailText, $headers);
$responseArray = $okMessage;
}
else
{
//verification failed
$responseArray = $errorMessage;
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray;
}
?>
<form method="post" name="contactform" action="contact.php" >
<div class="field half first">
<label for="name">name</label>
<input type="text" name="name" id="name" placeholder="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" placeholder="email#email.com" />
</div>
<div class="field">
<label for="message">message</label>
<textarea name="message" id="msg" rows="4" placeholder="type your message here"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="----------------------------------"></div>
<ul class="actions">
<li><input type="submit" value="send" class="special" /></li>
<li><input type="reset" value="reset pola" /></li>
</ul>
I have tried to use $_POST and $_GET to echo responseArray but I can't figure it out...
Is anyone able to provide a code I would have to use to display $okMessage and $errorMessage on index.php please?
Many thanks for all help

Report Form not submitting (php, html)

I'm creating a small website and I have a one question report form. The problem is the email isn't sending. I'm not sure what I did wrong.
I think there might be something wrong with the php, or maybe the html, I'm not the best with forms. Thanks for the help!
Here's my code:
html
<h2 style="font-size:30px;"><i class="fa fa-exclamation-circle"></i> Report</h2>
</div>
<hr class="descriptionline3">
<div class="modal-body3">
<form action="report.php" id="errorform" onsubmit = "return validate();">
<p style="color:#363636;text-align:left;padding-left:10px;">What seems to be the problem?</p>
<input type="text" class="forminputproblem" id="name" placeholder="Write the problem here..." style="height:20px;font-size:14px;border-radius:6px;display:inline-block;border:1px solid #ccc;padding: 12px 20px;
">
<div style="height:10px;"></div>
<div style="color:red;" id="error_message"></div>
<div style="color:green;" id="success_message"></div>
<div style="height:10px;"></div>
<input name="sumbit" type="submit" value="SEND REPORT">
</form>
php
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailTo = "contact#email.com";
$headers = "Report Form Message";
$txt = "You have received a report form message: ".$name.".\n\n";
mail($mailTo, $txt, $headers);
header("Location: /index.html?reportsent");
}
?>
javascript
function validate(){
var name = document.getElementById("name").value;
var error_message = document.getElementById("error_message");
error_message.style.padding = "10px";
var text;
if(name.length < 10){
text = "✖ Report message has to be at least 10 characters!";
error_message.innerHTML = text;
text = "";
success_message.innerHTML = text;
return false;
}
text = "";
error_message.innerHTML = text;
text = "✔ Report has been submitted! We will get back to you as soon as possible.";
success_message.innerHTML = text;
return true;
}
Thanks!
By checking out the mail function's parameters...
mail($to, $subject, $message, $headers);
It seems like the problem is that your variables are a bit mixed up. Also, "header" doesn't refer to the subject: email headers contain info and have their own syntax. Try this code:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailTo = "contact#rileysunblockedgames.com";
$subject = "Report Form Message";
$headers = 'From: noreply#rileysunblockedgames.com' . "\r\n" .
'Reply-To: noreply#rileysunblockedgames.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$txt = "You have received a report form message: ".$name.".\n\n";
mail($mailTo, $subject, $txt, $headers);
header("Location: /games.html?reportsent");
}
?>
Also, on your input, you have to define the "name" attribute for it to work properly:
<input type="text" class="forminputproblem" name="name" id="name" placeholder="Write the problem here..." style="height:20px;font-size:14px;border-radius:6px;display:inline-block;border:1px solid #ccc;padding: 12px 20px;">
Also also (and funny enough this is the biggest barrier in your way right now), there is a typo in your submit button's name, so your PHP code will never run:
<input name="submit" type="submit" value="SEND REPORT">

How to show success message below html form after form submit which is being handled by different php file

There is a form in my index.php file. This form is handling from a different php file named send-mail.php. I want to show a message inside alert div in index.php file. Can this be done by php or javascript will be needed too?
index.php:
<section id="contact">
<form action="send-mail.php" id="form" method="post" name="form">
<input id="name" name="name" placeholder="your name" type="text" required>
<input id="email" name="email" placeholder="your e-mail" type="email" required>
<textarea cols="50" id="message" name="message" placeholder="your enquiry" rows="4" required></textarea>
<input type="submit" name="submit" id="submit" value="Send Message">
</form>
<div class="alert alert-dismissible fade in hide" role=alert>
<button type=button class=close data-dismiss=alert aria-label=Close><span aria-hidden=true>×</span></button>
</div>
</section>
send-mail.php:
<?php
if(isset($_POST['submit'])){
// Get the submitted form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Recipient email
$toEmail = 'user#example.com';
$emailSubject = 'Contact Request Submitted by '.$name;
$htmlContent = '<h2>Contact Request Submitted</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Message</h4><p>'.$message.'</p>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'alert-success';
header('location: index.php#contact');
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'alert-danger';
header('location: index.php#contact');
}
}
?>
Change your redirects to:
header('location: index.php?result='.$msgClass.'#contact');
Then adding the following to your index.php file:
if ($_GET['result']=="alert-success") {
// display success message here
} elseif ($_GET['result']=="alert-danger") {
// display error message here
}

html and php form will not submit to email?

I've been trying to submit a form that is on my website but the form will not submit. I am not a php expert and I can not tell where my error is. The php file opens instead of sending to my two emails.
html
<form action="email.php" method="post" name="emailForm">
<div class="form-group">
<label for="Name"></label>
<input class="form-control name" placeholder="Name" id="Name" name="name">
</div>
<div class="form-group">
<label for="Email"></label>
<input class="form-control email" placeholder="Email" id="Email" name="email">
</div>
<div class="form-group">
<label for="Subject"></label>
<input class="form-control subject" placeholder="Subject" id="Subject" name="subject">
</div>
<div class="form-group">
<label for="Message"></label>
<textarea class="form-control Message" placeholder="Message" id="Message" name="message"></textarea>
</div>
<input type="submit" id="button" class="btn btn-default button_submit"></input>
</form>
and I have the php file in the main directory
and it contains
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "georgestcm#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$email_subject = "New Form submission";
$to = "georgestcm#gmail.com, yahwehown#gmail.com";
$email_body = "You have received a new message from the user $name.\n"."Here is the message:\n $message".
mail($to,$email_subject,$email_body,$headers);
?>
and my form validation is in in jquery. That is why I have not done it in php, because I am not a php expert.
jquery
$(document).ready(function() {
var name = $('input.name');
var email = $('input.email').prop('disabled',true);
var subject = $('input.subject').prop('disabled',true);
var message = $('textarea.Message').prop('disabled',true);
var button = $('input#button').prop('disabled',true);
name.on('keyup',function() {
if($(this).val().length > 0) {
email.prop('disabled',false);
} else {
email.prop('disabled',true);
}
});
email.on('keyup',function() {
if($(this).val().length > 0 && $(this).val().includes('#') && $(this).val().includes('.com')) {
subject.prop('disabled', false);
message.prop('disabled',false);
} else {
subject.prop('disabled', true);
message.prop('disabled',true);
button.prop('disabled',true);
}
});
message.on('keyup',function() {
if($(this).val().length > 0){
button.prop('disabled',false);
}
})
button.on('click', function() {
name.val('');
email.val('');
subject.val('');
message.val('');
})
})
I want to send email to my two emails.

How to prevent spammers to send me blank emails from my contact form

I have simple contact form on my website and few days ago I started to receive half blank emails from contact form on website.
They looking the same as the sample below, filled out just Name and email field, but Phone and Message fields are empty:
Name: 5906f36c9c72b
E-mail: njhjlee#gmail.com (any time different emails)
Phone:
Message:
This is HTML code:
<form id="contactform" action="assets/php/mail_submit.php" method="post">
<div class="row">
<div class="col-xs-12 col-md-4">
<input type="text" name="name" id="name" placeholder="Your Name" required/>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input type="text" name="email" id="email" placeholder="Your email" required/>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input type="text" name="phone" id="phone" placeholder="Ваш телефон" required/>
</div><!--column-->
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<div class="col-xs-12 col-md-8">
<textarea placeholder="Message" name="message" id="message" required></textarea>
</div><!--column-->
<div class="col-xs-12 col-md-4">
<input class="btn btn-default" id="submit" type="submit" value="Send"/>
</div><!--column-->
</div><!--row-->
</form>
This is PHP script:
<?php
if (!isset($_REQUEST['name']) || !isset($_REQUEST['email']) ||
!isset($_REQUEST['message']) || !isset($_REQUEST['phone'])) {
die();
}
if (isset($_POST['name']) || isset($_POST['email']) ||
isset($_POST['message']) || isset($_POST['phone']) && !empty($_POST['name'])
|| !empty($_POST['email']) || !empty($_POST['message']) |
!empty($_POST['phone'])) {
$your_email="my#mail.com";
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$phone=$_POST['phone'];
$to = $your_email;
$subject = "My Website: New Message! \r\n";
$message = '
<html>
<head>
<title>Message from '. $name .'</title>
</head>
<body>
<table class="table">
<tr>
<th align="right">Name:</th>
<td align="left">'. $name .'</td>
</tr>
<tr>
<th align="right">E-mail:</th>
<td align="left">'. $email .'</td>
</tr>
<tr>
<th align="right">Phone:</th>
<td align="left">'. $phone .'</td>
</tr>
<tr>
<th align="right">Message:</th>
<td align="left">'. $message .'</td>
</tr>
</table>
</body>
</html>';
$headers .= "From: no-reply#website.com\r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to, $subject, $message, $headers);
} else {
die();
}
?>
This is AJAX script:
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]
{2,4})+$/;
return regex.test(email);
}
if($("#contactform").length!=0){
$("#contactform").submit(function (e) {
e.preventDefault();
var name = $("#name").val(),
email = $("#email").val(),
phone = $("#phone").val(),
message = $("#message").val(),
dataString = 'name=' + name + '&email=' + email+ '&phone=' + phone +
'&message=' + message;
if (name === '' || !IsEmail(email) || phone === '' || message ===
'') {
$('#valid-issue').html('Not correct data').slideDown();
} else {
$.ajax({
type: "POST",
url: "/assets/php/mail_submit.php",
data: dataString,
success: function () {
$('#contactform').slideUp();
$('#valid-issue').html('Thank you. Message was sent successfully.').show();
}
});
}
});
}
I've checked logs and found that all spammer bots was from Thor network and with different IPs. I tried captcha but it doesn't help. Help with advise please...
Try to reinforce your javascript checking, also use "trim":
var name = $("#name").val().trim();
Use PHP str_word_count to reinforce the checking for empty message.
eg.
$message= filter_input(INPUT_POST, 'message');
// check $message is not empty and that it contains more than 5 words
if($message != "" || str_word_count($message) < 5) {
echo "Message is valid"
}else{
echo "Invalid Message";
}
I few other wrong things with your PHP code:
You should never access superglobal POST/GET variables directly. Use PHP inbuilt filter functions or a customised one.
$name= filter_input(INPUT_POST, 'name');
Or you can use
function FilterData($value){
$trimmed = trim($value);
$notags = strip_tags($trimmed);
return $notags;
}
Then:
$name = FilterData($_POST['name']);
For the email you can use
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
More info here and here
Hope it helps ;)

Categories