-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact-form.php
More file actions
93 lines (71 loc) · 2.96 KB
/
contact-form.php
File metadata and controls
93 lines (71 loc) · 2.96 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//////////////////////////
//Specify default values//
//////////////////////////
//Your E-mail
$your_email = 'your@email.com';
//Default Subject if 'subject' field not specified
$default_subject = 'From My Contact Form';
//Message if 'name' field not specified
$name_not_specified = 'Please type a valid name';
//Message if e-mail sent successfully
$email_was_sent = 'Thanks, your message successfully sent';
//Message if e-mail not sent (server not configured)
$server_not_configured = 'Sorry, mail server not configured (function "mail()" disabled on your server?)';
///////////////////////////
//Contact Form Processing//
///////////////////////////
$errors = array();
//"name" field required by this PHP script even if
// there are no 'aria-required="true"' or 'required'
// attributes on this HTML input field
if(isset($_POST['name'])) {
if(!empty($_POST['name']))
$sender_name = stripslashes(strip_tags(trim($_POST['name'])));
if(!empty($_POST['message']))
$message = stripslashes(strip_tags(trim($_POST['message'])));
if(!empty($_POST['email']))
$sender_email = stripslashes(strip_tags(trim($_POST['email'])));
if(!empty($_POST['subject']))
$subject = stripslashes(strip_tags(trim($_POST['subject'])));
//Message if no sender name was specified
if(empty($sender_name)) {
$errors[] = $name_not_specified;
}
$from = "MIME-Version: 1.0" . "\r\n" ;
$from .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
$from .= (!empty($sender_email)) ? 'From: '.$sender_email : '';
$subject = (!empty($subject)) ? $subject : $default_subject;
//sending message if no errors
if(empty($errors)) {
//duplicating email meta (from and subject) to email message body
$message_meta = '';
//From name and email
$message_meta .= 'From: '. $sender_name . ' ' . $sender_email . "<br>";
//Subject or default subject
$message_meta .= 'Subject: '. ( $subject ? $subject : $default_subject ) . "<br>";
//adding another CUSTOM contact form fields that added by user to email message body
foreach ($_POST as $key => $value) {
//checking for standard fields
if ($key == 'name' || $key == 'message' || $key == 'subject' || $key == 'email' ) {
continue;
}
//adding key-value pare to email message body
$message_meta .= stripslashes(strip_tags(trim($key))) . ': ' . stripslashes(strip_tags(trim($value))) . "<br>";
}
$message = $message_meta . "<br>" . 'Message:' . "<br>" . $message;
$message = wordwrap($message, 70);
if (mail($your_email, $subject, $message, $from)) {
echo $email_was_sent;
} else {
$errors[] = $server_not_configured;
echo '<span class="form-errors">' . implode('<br>', $errors ) . '</span>';
}
} else {
echo '<span class="form-errors">' . implode('<br>', $errors ) . '</span>';
}
} else {
// if "name" var not send ('name' attribute of contact form input field was changed or missing)
echo '"name" variable were not received by server. Please check "name" attributes for your input fields';
}
?>