-
Notifications
You must be signed in to change notification settings - Fork 20
/
sendmail.php
133 lines (110 loc) · 3.72 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Define the from email
*/
// email
define('TO_EMAIL', 'myemail@mydomain.com');
define('FROM_EMAIL', 'info@test.com');
define('FROM_NAME', 'Test');
/**
* define the body of the email. You can add some shortcode, with this format: %ID%
*
* ID = the id have you insert on the html markup.
*
* e.g.
* <input type="text" name="email" />
*
* You can add on BODY, this:
* email: %email%
*/
define( 'BODY', '%message%<br /><br /><small>email send by %name%, email %email%, tel.: %phone%.</small>' );
define( 'SUBJECT', 'Email from yoursite.com' );
// here the redirect, when the form is submitted
define( 'ERROR_URL', 'contact-error.html' );
define( 'SUCCESS_URL', 'contact-success.html' );
define( 'NOTSENT_URL', 'contact-error.html' );
// the message feedback of ajax request
$msg = array(
'error' => '<p class="error">Warning! Fill correctly the fields marked in red</p>',
'success' => '<p class="success">Email sent correctly. Thanks to get in touch us!</p>',
'not-sent' => '<p class="error">An error has been encountered. Please try again.</p>'
);
// the field required, by name
$required = array( 'name', 'email', 'message' );
/**
* Send the email.
*
* SERVER-SIDE: the functions redirect to some URL, in base of result of control and send.
* The urls must be set in the constants above: ERROR_URL, SUCCESS_URL, NOTSENT_URL
*
* CLIENT-SIDE: in js/contact.js, there is already script for real-time checking of fields
* and for ajax request of send email, that request in this page (sendmail.php) and echo the feedback message.
*/
sendemail();
// NO NEED EDIT
function sendemail()
{
global $msg, $required;
if ( isset( $_POST['ajax'] ) )
$ajax = $_POST['ajax'];
else
$ajax = false;
if ( isset( $_POST['yit_action'] ) AND $_POST['yit_action'] == 'sendmail' )
{
$body = BODY;
$post_data = array_map( 'stripslashes', $_POST["yit_contact"] );
//print_r($post_data);
//die;
foreach ( $required as $id_field ) {
if( $post_data[$id_field] == '' || is_null( $post_data[$id_field] ) ) {
if ( $ajax )
end_ajax( $msg['error'] );
else
redirect( ERROR_URL );
}
}
if( !is_email( $post_data['email'] ) OR $post_data['email'] == '' )
if ( $ajax )
end_ajax( $msg['error'] );
else
redirect( ERROR_URL );
foreach( $post_data as $id => $var )
{
if( $id == 'message' ) $var = nl2br($var);
$body = str_replace( "%$id%", $var, $body );
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: '.FROM_NAME.' <'.FROM_EMAIL.'>' . "\r\n" . 'Reply-To: ' . $post_data['email'];
$sendmail = mail( TO_EMAIL, SUBJECT, $body, $headers );
if ( $sendmail )
if ( $ajax )
end_ajax( $msg['success'] );
else
redirect( SUCCESS_URL );
else
if ( $ajax )
end_ajax( $msg['not-sent'] );
else
redirect( NOTSENT_URL );
}
}
function is_email($email)
{
if (!preg_match("/[a-z0-9][_.a-z0-9-]+@([a-z0-9][0-9a-z-]+.)+([a-z]{2,4})/" , $email))
{
return false;
}
else
{
return true;
}
}
function end_ajax( $msg = '' ) {
echo $msg;
die;
}
function redirect( $redirect = '' ) {
header( 'Location: ' . $redirect );
die;
}