PHP X-Mailer not sending all fields - php

I am by no means a developer. Self-taught, but can usually wing it enough to make things happen. Working on company website for boss, and having trouble with the form. Regardless of how I try to define the PHP variable, SOME are not sending the input value to the e-mail. I'm sure I'm missing something simple, thank you in advance for your help! This is what's not showing:
Website:
Average Monthly Volume:
Preferred Contact Method:
Here is what I have so far:
HTML FORM:
<div class="product-screens2">
<div style="padding:200px">
<div class="form">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage">Please retry.</div>
<form action="form-email" method="post" role="form" class="contactForm">
<div class="form-row">
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Business Name" />
</div>
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="business_site" placeholder="Business Website" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="avgvolume" placeholder="Average Monthly Volume" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="contactmethod" placeholder="Preferred Contact Method" />
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Brief Description of Goods or Services Sold"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" title="Send Message">SUBMIT</button></div>
</form>
</div></div>
</div>
PHP MAILER SCRIPT
<?php
/***************** Configuration *****************/
// Enter your email, where you want to receive the messages.
$contact_email_to = "Support#XXXXX.com";
// Subject prefix
$contact_subject_prefix = "Message From XXXXX Website: ";
// Name too short error text
$contact_error_name = "Name is too short or empty!";
// Email invalid error text
$contact_error_email = "Please enter a valid email!";
// Subject too short error text
$contact_error_subject = "Subject is too short or empty!";
// Message too short error text
$contact_error_message = "Too short message! Please enter something.";
/********** Do not edit from the below line ***********/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Sorry Request must be Ajax POST');
}
if(isset($_POST)) {
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$business_site = $_POST["business_site"];
$avgvolume = $_POST["avgvolume"];
$contactmethod = $_POST["contactmethod"];
if(strlen($name)<4){
die($contact_error_name);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
die($contact_error_email);
}
if(strlen($message)<3){
die($contact_error_subject);
}
if(strlen($message)<3){
die($contact_error_message);
}
if(!isset($contact_email_from)) {
$contact_email_from = "contactform#" . #preg_replace('/^www\./','', $_SERVER['SERVER_NAME']);
}
$sendemail = mail($contact_email_to, $contact_subject_prefix . $subject,
"Name: $name" . PHP_EOL .
"Reply-To: $email" . PHP_EOL .
"Business: $subject" . PHP_EOL .
"Website: $business_site" . PHP_EOL .
"Average Monthly Volume: $avgvolume" . PHP_EOL .
"Preferred Contact Method: $contactmethod" . PHP_EOL .
"Business Description: $message" . PHP_EOL .
"X-Mailer: PHP/" . phpversion()
);
if( $sendemail ) {
echo 'OK';
} else {
echo 'Could not send mail! Please check your PHP mail configuration.';
}
}
?>

Related

Some issue in sending email using php email function

I am trying to send an email from a enquiry form available on a catalogue website on which I am working and found a strange issue.
On the same domain I have a file to test the email function with following code in it:
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
and its working fine. But the same code is not working on the index.php page where I have the enquiry form and also not showing any error at all (I have checked the error log. I have also tried by putting this code in a separate file and by including it on index.php but the same result).
<form name="frm_enquiry" id="frm_enquiry" method="post" autocomplete="off">
<div class="row">
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your name">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="mobile">Mobile No:</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Enter your mobile number">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label style="width: 100%;" for="mobile"> </label>
<button type="submit" class="btn btn-block btn-primary" name="submit">Submit</button>
</div>
</div>
</div>
</form>
php:
if( isset( $_REQUEST['submit'] ) )
{
$name = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
echo $name . ' : ' . $email . ' : ' . $mobile . '<br>';
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
}
Someone please guide me, what I am doing wrong here, I am stuck on this from last 2 days.
Thanks in advance.

Validate name in contact form using php - $error incorrect?

I'm having trouble validating the name in my contact form. If the name field is left empty, the form should generate an error message such as "Please enter your name". Instead there is no error message and the e-mail is allowed to be sent. Here's my code:
<form id="ajax-contact-form" class="form-horizontal" action="javascript:alert('success!');">
<div class="row">
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputName">Your full name:</label>
<div class="controls">
<input class="span4" type="text" id="inputName" name="name" value="Your full name:" onBlur="if(this.value=='') this.value='Your full name:'" onFocus="if(this.value =='Your full name:' ) this.value=''">
</div>
</div>
</div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputEmail">Your email:</label>
<div class="controls">
<input class="span4" type="text" id="inputEmail" name="email" value="Your email:" onBlur="if(this.value=='') this.value='Your email:'" onFocus="if(this.value =='Your email:' ) this.value=''">
</div>
</div>
</div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputPhone">Phone number:</label>
<div class="controls">
<input class="span4" type="text" id="inputPhone" name="phone" value="Phone number:" onBlur="if(this.value=='') this.value='Phone number:'" onFocus="if(this.value =='Phone number:' ) this.value=''">
</div>
</div>
</div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputDrums">Do you have a drum set?</label>
<div class="controls">
<select input class="" type="text" id="inputDrums" name="drums">
<option>Do you have a drum set?</option>
<option>Yes</option>
<option>No</option>
</select>
</div>
</div>
</div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputLevel">What level is the student?</label>
<div class="controls">
<select input class="span4" type="text" id="inputLevel" name="level">
<option>What level is the student?</option>
<option>Beginner</option>
<option>Intermediate</option>
<option>Advanced</option>
</select>
</div>
</div>
</div>
<div class="span4">
<div class="control-group">
<label class="control-label" for="inputCity">What city do you live in?</label>
<div class="controls">
<select input class="span4" type="text" id="inputCity" name="city">
<option>What city do you live in?</option>
<option>Boulder</option>
<option>Erie</option>
<option>Gunbarrel</option>
<option>Lafayette</option>
<option>Longmont</option>
<option>Louisville</option>
<option>Niwot</option>
<option>Other</option>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="span12">
<div class="control-group">
<label class="control-label" for="inputMessage">Message:</label>
<div class="controls">
<textarea class="span12" id="inputMessage" name="content" onBlur="if(this.value=='') this.value='Message:'"
onFocus="if(this.value =='Message:' ) this.value=''">Message:</textarea>
</div>
</div>
</div>
</div>
<div class="row">
<div class="span12">
<div class="control-group captcha">
<label class="control-label" for="inputCaptcha">Enter captcha numbers here:</label>
<div class="controls">
<input class="" type="text" id="inputCaptcha" name="captcha" value="Enter captcha numbers here:" onBlur="if(this.value=='') this.value='Enter captcha numbers here:'" onFocus="if(this.value =='Enter captcha numbers here:' ) this.value=''">
<img src="captcha/captcha.php">
<img src="images/trans.png" style="width: 5px; height: 5px">
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Refresh captcha" style="width: 120px; height: 42px">
</div>
</div>
</div>
</div>
<button type="submit" class="submit">Send</button>
</form>
**Contact.php**
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/
include 'contact_config.php';
session_start();
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$phone = stripslashes($_POST['phone']);
$drums = stripslashes($_POST['drums']);
$level = stripslashes($_POST['level']);
$city = stripslashes($_POST['city']);
$subject = "Website Inquiry";
$message = "
Name: ".$_POST['name']
."
E-mail Address: ".$_POST['email']
."
Phone: ".$_POST['phone']
."
Drums: ".$_POST['drums']
."
Level: ".$_POST['level']
."
City: ".$_POST['city']
."
Message: ".$_POST['content'];
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.<br />';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}
if(isset($_SESSION['captcha_keystring']) && strtolower($_SESSION['captcha_keystring']) != strtolower($_POST['captcha']))
{
$error .= "Incorrect captcha.<br />";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
**functions.php**
<?php
function ValidateEmail($email)
{
$regex = '/([a-z0-9_.-]+)'. # name
'#'. # at
'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
'.'. # period
'([a-z]+){2,10}/i'; # domain extension
if($email == '') {
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}
return empty($eregi) ? true : false;
}
?>
Your $_POST['name'] may not be actually empty it could be something like ' '. Replace
if(!$name){//...
with
if(empty($name)){//...
You could also trim your strings as well. e.g.
$name = trim(stripslashes($_POST['name']));
Use isset to verify if the parameter was sent or not
if(!isset($_POST['name']))
{
$error .= 'Please enter your name.<br />';
}
Try printing the $error parameter before the condition and see what the output is.
About the email varification you can use the PHP build in filters:
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.";
}
Example #1 Validating email addresses with filter_var()
In my testing, I have found that empty strings always evaluate to false, so if the $_POST['name'] variable is an empty string, your code should work.
It may be, then, that you are receiving white space (e.g. a space or newline) from your form without realising it. In this case, you can filter your $_POST variable like so:
$filtered = array_map("stripslashes", array_filter(array_map("trim", $_POST)));
Having done this, $filtered['name'] will not be set if $_POST['name'] is empty or only whitespace. This can be checked with either isset or array_key_exists.
Change the value to placeholder
Original code
<input class="span4" id="inputName" name="name" value="Your full name:" onblur="if(this.value=='') this.value='Your full name:'" onfocus="if(this.value =='Your full name:' ) this.value=''" type="text">
To
changed code
<input class="span4" id="inputName" name="name" placeholder="your full name" type="text">
A place holder will appear in the input field. But When you add something to the field, the placeholder will disappear. Also when your clear text in the input field (make the input field empty) and you focus out of it, the input field will retain its placeholder value.
do this for all the input fields such as email.
But If you still want to keep the onfocus and onblur events, call this function on submit (Although I dont recommmend as this as it will only work if javascript on the client side is enabled).
function()
{
var name = document.getElementById('inputName');
if(name.value === "Your full name:" || name.value === "")
{
return false;
}
}

I am receiving empty emails on my website

I am building a html5 based contact form with php mail script.
I do not know why i am getting empty emails on submission as well as If i want to print any message like "Thank you for contacting us", How would I do that.
<?php
$name = (isset($_POST['name']) ? $_POST['name'] : null);
$phone = (isset($_POST['phone']) ? $_POST['phone'] : null);
$address = (isset($_POST['address']) ? $_POST['address'] : null);
$appointment_datepicker =(isset($_POST['appointment-datepicker']) ? $_POST['appointment-datepicker'] : null);
$message = (isset($_POST['textarea']) ? $_POST['textarea'] : null);
$email_address = (isset($_POST['email']) ? $_POST['email'] : null);
// Create the email and send the message
$to = 'dipen2512#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nAppointment: $appointment_datepicker\n\nAddress: $address\n\nMessage:\n$message";
$headers = "From: noreply#dev-designers.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
$mail_status = mail($to,$email_subject,$email_body,$headers);
/** Check for empty fields
if(!empty($_POST['name']) || !empty($_POST['phone']) || !empty($_POST['address']) || !empty($_POST['appointment-datepicker']) || !empty($_POST['textarea']) || filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{**/
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
print( "Thank you for the message. We will contact you shortly.");
window.location = 'http://thankyou.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
print( 'Message failed. Please, send an email to gordon#template-help.com');
window.location = 'http://thankyou.html';
</script>
<?php
}
?>
<form id="appointment-form" action="contact_me.php" method="post" class="appointment-form">
<div class="col-md-6">
<div class="form-group form-md-line-input form-md-floating-label">
<input id="name" type="text" class="form-control" required="required">
<label for="name">Name</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="phone" type="text" class="form-control" required="required" pattern="/[1-9][01][0-9]-?[0-9]{3}-=[0-9]{4}">
<label for="phone">Phone Number</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="address" type="text" class="form-control" required="required">
<label for="address">Address </label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="appointment-datepicker" type="text" class="form-control form-line-input" required="required">
<label for="appointment-datepicker">Book a date</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-md-line-input form-md-floating-label">
<input id="email" type="text" class="form-control" required="required" pattern="[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~.-]+#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*">
<label for="email">Email Address</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<textarea id="textarea" rows="4" class="form-control form-textarea"></textarea>
<label for="textarea">Message</label>
</div>
<div class="btn-wrapper">
<button type="submit" class="btn btn-make-app">Send to us</button>
</div>
</div>
<div class="clearfix"> </div>
</form>
Your form inputs are missing "name" attributes. IDs don't get passed along in a form request; only the name-value pairings.
v---------v
<input id="name" name="name" type="text" class="form-control" required="required">

How do I stop the Phantom Emails from my PHP code?

I'm currently using an html5 website with a contact form and a php action to receive contact emails. I've tried a few different codes in my php but non have stopped the blank emails from coming. I do have google analytics enables on my code but have blocked the crawler through robot.txt. Here is my code.
PHP CODE
<?php
foreach ($_GET as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
$name = trim(htmlentities($_GET['name'],ENT_QUOTES,'utf-8'));
$email = trim(htmlentities($_GET['email'],ENT_QUOTES,'utf-8'));
$phone = trim(htmlentities($_GET['phone'],ENT_QUOTES,'utf-8'));
$messages = trim(htmlentities($_REQUEST['messages'],ENT_QUOTES,'utf-8'));
if (strlen($name) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
if (strlen($email) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
$to = "junior.8791#gmail.com";
$message = "Name: ".$name;
$message.="\n\nEmail: ".$email;
$message.="\n\nPhone: ".$phone;
$message .= "\n\nMessage: ".$messages;
$headers = "From: $email";
$headers .="\nReply-To: $email";
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "<script>window.location = 'http://www.mason372.org/thankyou.html'</script>";
} else {
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
?>
CONTACT FORM
<form action="email.php" method="post" id="form">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="name" name="name" id="name" placeholder="Name" value="" aria-describedby="name-format" required aria-required=”true” pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="e.g.'John Doe'" required="" />
</div>
<div class="6u">
<input type="email" class="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="tel" class="tel" name="phone" id="phone" name="phone" type="text" placeholder="Phone Number" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" required />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="messages" id="messages" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message">
<input type="reset" class="button button-alt" value="Clear Form">

php form not working

I cant get this to work properly. I know its something stupid but I cant find it.
<?php
$mailto = "stormygurl73#yahoo.com";
$name = ucwords($_POST['name']);
$subject = $_POST['Contact form'];
$email = $_POST['email'];
$message = $_POST['message'];
if(strlen($_POST['name']) < 1 ){
echo 'email_error';
}
else if(strlen($email) < 1 ) {
echo 'email_error';
}
else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", $email)) {
echo 'email_error';
}
else if(strlen($message) < 1 ){
echo 'email_error';
} else {
// NOW SEND THE ENQUIRY
$email_message="\n\n" .
"Name : " .
ucwords($name) .
"\n" .
"Email : " .
$email .
"\n\n" .
"Message : " .
"\n" .
$message .
"\n" .
"\n\n" ;
$email_message = trim(stripslashes($email_message));
mail($mailto, $subject, $email_message, "From: \"$vname\" <".$email.">\nReply-To: \"".ucwords($name)."\" <".$email.">\nX-Mailer: PHP/" . phpversion() );
}
?>
Html
<div class="wrapper">
<div id="main" style="padding:50px 0 0 0;">
<form id="contact-form" action="sendemail.php" method="post">
<h3>Get in touch</h3>
<h4>Fill in the form below, and we'll get back to you within 24 hours.</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
<!-- /Form -->
Any help would be awesome!
You must have the name attribute on your input-elements. The first one should have
name="name"
and so on. You can read more about it here: Variables From External Sources : HTML Forms (GET and POST)
It looks like you've missed the attribute name name='somename' everywhere in your form fields. e.g.
This is what you have
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
This is what it should have been
<input name="name" placeholder="Please enter your name" type="text" tabindex="1" required autofocus>

Categories