/
contact-send.php
72 lines (61 loc) · 1.89 KB
/
contact-send.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
//Catch any errors while testing our script
//Remove when going live.
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Ensures no one loads page and does simple spam check.
if(isset($_POST['name']) && empty($_POST['spam_check']))
{
//Include our email validator for later use
require 'email-validator.php';
$validator = new EmailAddressValidator();
//Declare our $errors variable we will be using later to store any errors.
$errors = array();
//Setup our basic variables
$input_name = strip_tags($_POST['name']);
$input_email = strip_tags($_POST['email']);
$input_subject = strip_tags($_POST['subject']);
$input_message = strip_tags($_POST['message']);
//We'll check and see if any of the required fields are empty.
//We use an array to store the required fields.
$required = array('Name field' => 'name', 'Email field' => 'email', 'Message field' => 'message');
//Loops through each required $_POST value
//Checks to ensure it is not empty.
foreach($required as $key=>$value)
{
if(isset($_POST[$value]) && $_POST[$value] !== '')
{
continue;
}
else {
$errors[] = $key . ' cannot be left blank';
}
}
//Make sure the email is valid.
//this is adding invalid even when it is blank
if (($input_email!=='')&&(!$validator->check_email_address($input_email))){
$errors[] = 'Email address is invalid.';
}
//Now check to see if there are any errors
if(empty($errors))
{
//No errors, send mail using conditional to ensure it was sent.
if(mail(''/*<--enter email here*/, "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
{
echo 'Your email has been sent.';
}
else
{
echo 'There was a problem sending your email.';
}
}
else
{
//Errors were found, output all errors to the user.
echo implode('<br />', $errors);
}
}
else
{
die('Direct access to this page is not allowed.');
}