I'm using PHP mailer to build a contact form and bear in mind I'm new to PHP. I have the form sending nicely, however, in the email that I receive the From address does not match the email that the user inputs into the contact form and is instead displaying the same email as the recipient. I'm 100% sure I'm entering the correct email into the live contact form. I have replaced my sensitive information with $$$$$$$$$
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must specify a value for name, email address, and message.";
}
if (!isset($error_message)) {
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once("../inc/phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "$$$$$$$$$$$$$";
$mail->Password = "$$$$$$$$$$$$$";
$mail->SMTPSecure = 'tls';
if (!isset($error_message)) {
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
$mail->isHTML(true);
$mail->Subject = '$$$$$$$$$$| ' . $name;
$mail->Body = $message;
$mail->AltBody = $message;
if($mail->Send()) {
header("Location: ../contact/?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
require_once("../inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="center-block">
<h1>Contact</h1>
</div>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div class="alert alert-success">
<p>Thanks for the email! I’ll be in touch shortly!</p>
</div>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email.</p>';
} else {
?><div class="alert alert-warning"><?php
echo '<p>' . $error_message . '</p>';
?></div><?php
}
?>
</div>
</div>
<form class="form-horizontal" method="post" role="form">
<div class="form-group">
<label for="name" class="col-sm-4 control-label">Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-4 control-label">Email</label>
<div class="col-sm-4">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-4 control-label">Message</label>
<div class="col-sm-4">
<textarea class="form-control" id="message" name="message" rows="3"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
</div>
</div>
<div class="form-group" style="display:none;">
<label for="address" class="col-sm-4 control-label">Address</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="address" name="address" placeholder="Name">
<p>Humans: please leave this field blank.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
<?php require_once("../inc/footer.php"); ?>
change this
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
to
$mail->setFrom($email, $name);
$mail->addAddress($EMAIL THAT ITS GOING TO, $NAME THATS ITS GOING TO);
Also see this link on sending FROM other address with GMAIL
https://support.google.com/mail/answer/22370?hl=en
It looks like you forgot to call the Sender variable:
public $Sender = '';
/**
* The Return-Path of the message.
* If empty, it will be set to either From or Sender.
* #type string
*/
https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
Thanks for your replies guys. I did a bit of external research and it turns out that it is an issue with gmail itself. They don't allow you to use their smtp service with a from address different to your gmail account. I think it's because of spam.
Related
Need some help with PHPMailer code. I made a contact form with SMTP, and when i submit the vaules, i get this error:
Undefined property: PHPMailer\PHPMailer\Exception::$getMessage in C:\xampp\htdocs\contact\mail.php on line 32
What is the wrong?
Here is the php code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/Exception.php';
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer (true);
$alert = '';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
try{
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myEmail#gmail.com';
$mail ->Password = 'appPassword';
$mail->SMTPSecure = "tls";
$mail->Port = '587';
$mail->setFrom( 'myEmail#gmail.com');
$mail->addAddress('myEmail#gmail.com');
$mail->isHTML (true);
$mail->Subject = 'Message Received from Contact: ' . $name;
$mail->Body = "Name: $name <br>Email: $email<br>Subject: $subject<br>Message: $message";
$mail->send();
$alert= "<div class='alert-success'><span>Message Sent! Thanks for contact us.</span></div>";
} catch (Exception $e) {
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
}
}
?>
Here's the form code so you have it:
<form name="form1" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control input-lg" name="name" id="name" placeholder="Enter name" required="required" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" class="form-control input-lg" name="email" id="email" placeholder="Enter email" required="required" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control input-lg" name="subject" id="subject" placeholder="Subject" required="required" />
</div>
</div>
</div>
<div class="col-md-12"><?php echo $alert; ?></div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" cols="25" required="required" placeholder="Message" style="height: 170px;"></textarea>
</div>
<button type="submit" class="btn btn-skin btn-block" name="submit" id="submitcontact">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
You have a semantics error. You started a string with a single quote and you closed it with a double quote. Be sure to start and end each string with the same type of quote.
change this
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
to this
$alert = "<div class='alert-error'><span>" . $e->getMessage()."</span></div>";
You got your quotes in your alert message wrong. Change this line:
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
to this:
$alert = '<div class="alert-error"><span>' . $e->getMessage(). '</span></div>';
My view
<div class="col-sm-6">
<div class="single_contant_left padding-top-90 padding-bottom-90">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/contactus", $attributes);?>
<div id="formid">
<div class="col-lg-8 col-md-8 col-sm-10 col-lg-offset-2 col-md-offset-2 col-sm-offset-1">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" name="name" value="<?php echo set_value('name'); ?>" placeholder="First Name" required>
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>" required>
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Subject" value="<?php echo set_value('subject'); ?>" required>
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="7" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="">
<input type="submit" name="submit" value="SEND MESSAGE" class="btn btn-lg">
</div>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
My controller
<?php
class Contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function contactus()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on form input
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('header_vw');
$this->load->view('center_vw');
$this->load->view('footer_vw');
}
else
{
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'alshoja#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = '....#gmail.com';
$config['smtp_pass'] = 'als....#......';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/contactus');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contactform/contactus');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
Am getting error while sending message my server is configured good but mail is not sending from my site, how can I configure shall I want to give my password and username for sending mail in configuring section or else how can configure that please help :(
Am confused with them to address and from address in email configuration :(
Though I have done this several times, sending email through another provider is always a painful task.
Though this maybe ok,
$config['newline'] = "\r\n"; //use double quotes
but, Sending email with gmail smtp with codeigniter email library suggests
$this->email->set_newline("\r\n");
Also are you sure you allowed less secure apps in gmail?
I am receiving empty email from Submit form every day around the same time.
Form Code :
<div class="form">
<h4 class="h-light text-center">Submit a Query</h4>
<form class="form-horizontal" action="sendemail.php" method="post" id="contact_form">
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="name" placeholder="Name" class="form-control input-text" type="text" required>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email" placeholder="E-Mail Address" class="form-control input-text" type="email" required>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">Phone #</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
<input name="phone" placeholder="(123)456-7890" class="form-control input-text" type="text" required>
</div>
</div>
</div>
<!-- Text area -->
<div class="form-group">
<label class="col-sm-2 control-label">Message</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control input-text text-area" name="message" placeholder="Enter your massage for us here. We will get back to you." required></textarea>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"><img src="media/home/captcha.png"></label>
<div class="col-sm-10">
<div class="input-group">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"></div>
</div>
</div>
</div>
<!-- Success message -->
<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div>
<!-- Button -->
<div class="form-group">
<label class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<button type="submit" class="input-btn" >Send <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</form>
sendemail.php Code:
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'New query !';
$mailto = 'xxx#domain.in';
/* These will gather what the user has typed into the field. */
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email Address: $email <br>
Phone Number: $phone <br>
Message: $message<br>
EOD;
$headers = "From:domain.in<xxx#domain.in>\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
else
{
echo "Failure";
}
?>
I have no idea what is going on, the code seems to be fine, but still i am receiving that empty email only with the field name like...
Name :
Email Address :
Phone Number :
Message :
But no information filled in it.
Thanks in advance.
Validate the input on the PHP side before you send the email. eg:
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
// Repeat for other fields
if($valid)
{
// Send the email
}
I am not so good with PHP, please let me know if it correct.. Thank you
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'New query!';
$mailto = 'xxx#domain.in';
/* These will gather what the user has typed into the fieled. */
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email Address: $email <br>
Phone Number: $phone <br>
Message: $message<br>
EOD;
$headers = "From:domain.in<info#domain.in>\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
$email = $_POST['email'];
if(empty($email))
{
echo('Please enter your email');
$valid = false;
}
$phone = $_POST['phone'];
if(empty($phone))
{
echo('Please enter your Phone number');
$valid = false;
}
$message = $_POST['message'];
if(empty($message))
{
echo('Please enter your message');
$valid = false;
}
if($valid)
{
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
}
else
{
echo "Failure";
}
i
?>
Should be working now?
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
<br/>
$email = $_POST['email'];
if(empty($email))
{
echo('Please enter your email');
$valid = false;
}
<br/>
$phone = $_POST['phone'];
if(empty($phone))
{
echo('Please enter your Phone number');
$valid = false;
}
<br/>
$message = $_POST['message'];
if(empty($message))
{
echo('Please enter your message');
$valid = false;
}
<br/>
if($valid)
{
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
}
else
{
echo "Failure";
}
?>
Checking for whether $_Post is empty is the usual and logical way to go.
But for someone who is curious about why the same empty email is being sent to you everyday (usually it's some kind of bot) why not add the following fields in your message body so you know what's happening.
This way you learn how the web works better too.
For example
$ip = $_SERVER['REMOTE_ADDR']; // for IP address
$browser = $_SERVER['HTTP_USER_AGENT']; // get browser info
and you add above info to your message body so you know what's going on.
I have the Contact Me form that I can't get to send me an email. I am still a newbie but I don't see anything wrong. Help me out guys.
Here's the contactform controller
<?php
class contactform extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index() {
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE) {
//validation fails
//$this->load->view('contact_view');
$this->load->view('header_view');
$this->load->view('nav_view');
$this->load->view('contact_view');
$this->load->view('footer_view');
}
else {
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'mygmail#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'mygmail#gmail.com';
$config['smtp_pass'] = 'mypass';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
//$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str) {
if (!preg_match("/^[a-zA-Z ]+$/",$str)) {
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else {
return TRUE;
}
}
}
?>
and here is my contact_view
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
I already checked my codes. But the problem is, I can't see why it cant send. I even tried to upload it to my website, but it still won't let me send an email.
I have successfully set up a php script for my contact form on my website but I have recently found out that my server provider does not accept php. Instead I have use SMTP.
Can anyone help me as this is new to me. I've attempted using other scripts but I cannot implement it.
This is my php code:
<?php
/* Set e-mail recipient */
$myemail = "mail#louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
My HTML form:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
Any suggestions welcome!
Thanks! :)
Louis
Oke I checked a few thinks. I guess you could atleast try the following. I know your host doesn't support php which find hard to believe tough. So maybe something else is wrong. What is the host you are using?
<?php
if($_POST) {
//Strip user input
function sanitize($value) {
//Escape the user input and then strip all html tags
//mysql_* is not recommended but I guess you don't store data in a database
$value = mysql_real_escape_string(strip_tags($value));
//Return the sanitized user input
return $value;
}
//To display all errors
function errors($error) {
echo '<ul class="error">';
foreach($error as $fail) {
echo '<li>'.$fail.'</li>';
}
echo '</ul>';
}
//All the input fields
$name = sanitize($_POST['name']);
$email = sanitize($_POST['email']);
$subject = sanitize($_POST['subject']);
$message = sanitize($_POST['message']);
//Check if there are no empty fields
if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
if(strlen($name) <= 3) {
$error[] = 'Name '.$name.' is too short';
}
if(strlen($email) <= 3) {
$error[] = 'Email '.$email.' is too short';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Email '.$email.' is not a valid email';
}
if(strlen($subject) <= 3) {
$error[] = 'Subject '.$subject.' is too short';
}
if(strlen($message) <= 8) {
$error[] = 'Message is too short';
}
//If there are no errors
if(empty($error)) {
$to = "mail#louisreed.co.uk"; //The email you want to send it to
//Subject already set
$headers = "FROM: My site"; //Sets the headers
//The message for the email
$message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
$mail = mail($to, $subject, $message, $headers);
//If the mail has been send
if($mail) {
header('Location: http://louisreed.co.uk/index.php?success');
exit();
}
}
} else {
$error[] = 'There are empty fields';
}
//If there are errors show them
if(!empty($error)) {
echo errors($error);
}
}
//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
So try the exact code I gave you above just copy and past it over the form code you have now. You can leave the rest of your code as is. After that save the complete file as index.php. After that remove the index.html file from your FTP. If you have done that you can upload the index.php file now check what you get. If there are still some errors please let me know