-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsendmail.php
70 lines (52 loc) · 2.04 KB
/
sendmail.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
<?php
/**
* Contact form PHP
* Credits: http://dev-tips.com/featured/ajax-and-php-contact-form
*/
// If the form have been submitted and the spam check field is empty
if ( isset( $_POST['name'] ) && empty( $_POST['spam_check'] ) ) :
// Enter your email
$mail = '[email protected]';
// Declare our $errors variable we will be using later to store any errors.
$errors = array();
$name = strip_tags( utf8_decode( $_POST['name'] ) );
$emailfrom = strip_tags( $_POST['email'] );
$subject = strip_tags( utf8_decode( $_POST['subject'] ) );
$message = strip_tags( utf8_decode( $_POST['message'] ) ); // Use uft8_decode to make special characters æ, ø, å, ü and é work
// We'll check and see if any of the required fields are empty.
// We use an array to store the required fields.
$required = array( 'Email' => 'email', 'Message' => 'message' );
// Set a default subject
if ( empty( $subject ) )
$subject = 'Default subject';
// Set a default name
if ( empty( $name ) )
$name = 'Anonymous person';
// 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.
if ( !filter_var( $emailfrom, FILTER_VALIDATE_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( $mail, "$subject", $message, "From: $emailfrom" ) )
echo '<p class="success">Your email has been sent</p>';
else
echo '<p class="error">There was a problem sending your email. Please try again</p>';
} else {
// Errors were found, output all errors to the user.
echo '<p class="error">';
echo implode( '<br />', $errors );
echo '</p>';
}
else :
// The user have tried to access thid page directly or this a spambot
echo "You're not allowed to access this page directly";
endif;