Reputation: 25
I'm trying to send a basic email from a form using PHP. The form is validated using jquery.validate.js (which is working fine), but I can't get the email to send. I simply keep getting the php file that is declared in the action when I submit.
The site is here, the form looks like this:
<form method="post" action="send-email.php" id="contact-form" class="form-horizontal" role="form">
<fieldset>
<div class="col-12 col-md-6 col-lg-6 contact--left">
<div class="form-group">
<label for="name" class="col-12 control-label brand brand-font">Name</label>
<div class="col-12">
<input type="text" class="form-control brand-font--standard" name="name" id="name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-12 control-label brand brand-font">Email</label>
<div class="col-12">
<input type="email" class="form-control brand-font--standard" name="email" id="email">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-12 control-label brand brand-font">Telephone</label>
<div class="col-12">
<input type="tel" class="form-control brand-font--standard" name="phone" id="phone">
</div>
</div>
</div>
<div class="col-12 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5 contact--right">
<div class="form-group">
<label for="message" class="col-12 control-label brand brand-font">Message</label>
<div class="col-12">
<textarea class="form-control brand-font--standard" rows="6" name="message" id="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-12"> </div>
<div class="col-12">
<button type="submit" class="btn btn-block btn--orange brand-font">Submit</button>
</div>
</div>
</div>
</fieldset>
</form>
...and the send-email.php file looks like this:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
I've tried using a few other examples i've found on the web but not got anything to stick yet. Apologies for the noob esque question but i'm more of a front-ender so have little idea about this type of thing (however basic it may seem to you guys)
Thanks for your time!
Upvotes: 0
Views: 159
Reputation: 364
You have to change the parameter in the following line of send-email.php
if(!preg_match($email_exp,$email_from)){
As you had already taken $_POST['email'] in $email_from
And while using default PHP mail function change your SMTP settings in php.ini file
Hope it Helps!!!
Upvotes: 1
Reputation: 76666
You've defined the visitor's email address as follows:
$email_from = $_POST['email']; // required
But in line 33, you're trying to use $email
, which is undefined:
if(!preg_match($email_exp,$email)) {
Change it to:
if(!preg_match($email_exp,$email_from)) {
That should fix the issue, and your form should work.
Unrelated sidenote: PHP's mail()
function is a bad choice. I'd recommend switching to PHPMailer or SwiftMailer, as they give you better control and is more robust.
Upvotes: 0