-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendEmails.php
204 lines (176 loc) · 6.35 KB
/
sendEmails.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
require_once("./config.php");
$input = array();
$errors = array();
$success = array();
$input['number_ppl'] = ($_POST['number_ppl']) ? $_POST['number_ppl'] : $_GET['number_ppl'];
$input['form_key'] = ($_POST['form_key']) ? $_POST['form_key'] : $_GET['form_key'];
$input['rand_key'] = ($_POST['rand_key']) ? $_POST['rand_key'] : $_GET['rand_key'];
$input['names'] = ($_POST['names']) ? $_POST['names'] : $_GET['names'];
$input['emails'] = ($_POST['emails']) ? $_POST['emails'] : $_GET['emails'];
$input['others'] = ($_POST['others']) ? $_POST['others'] : $_GET['others'];
$input['gift_value'] = ($_POST['gift_value']) ? $_POST['gift_value'] : $_GET['gift_value'];
try{
if(validate_input($input, $errors) && validate_form($input, $errors) && eliminate_blank_values($input) && validate_emails($input, $errors) && (count($input['names']) >= 1)){
if(send_emails($input, $errors, $success)){ return_success($success); } else { return_errors($errors); }
} else {
return_errors($errors);
}
} catch (Exception $e) {
return_errors($errors, $input, $e);
}
function validate_input(&$input, &$errors){
$return_val = true;
//Testing Form Key
if(!isset($input['form_key']) || empty($input['form_key']) || !is_numeric($input['form_key'])){
$errors['form'] = "Form is not valid";
$return_val = false;
}
//Testing Random Key
if(!isset($input['rand_key']) || empty($input['rand_key']) || !is_numeric($input['rand_key'])){
$errors['form'] = "Form is not valid";
$return_val = false;
}
//Testing Number of People
if(!isset($input['number_ppl']) || empty($input['number_ppl']) || !is_numeric($input['number_ppl'])){
$errors['form'] = "Form is not valid";
$return_val = false;
}
//Testing Names
if(!isset($input['names']) || empty($input['names']) || !is_array($input['names']) ){
$errors['names'] = "One of the names is empty";
$return_val = false;
}
//Testing Random Key
if(!isset($input['gift_value']) || empty($input['gift_value']) || !is_numeric($input['gift_value'])){
$errors['gift_value'] = "Gift Value is not valid";
$return_val = false;
}
//Testing Emails
if(!isset($input['emails']) || empty($input['emails']) || !is_array($input['emails']) ){
$errors['names'] = "One of the emails is empty";
$return_val = false;
}
return $return_val;
}
function validate_form(&$input, &$errors){
mt_srand($input['rand_key']);
if(mt_rand() != $input['form_key']){
$errors['form'] = "Form is not valid";
return false;
}
return true;
}
function array_shuffle($array){
// shuffle using Fisher-Yates
$i = count($array);
while(--$i){
$j = mt_rand(0,$i);
if($i != $j){
// swap items
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
function return_success(&$success){
?> <div class="success"><ul> <?php
foreach($success as $sucs){ echo "<li>".$sucs."</li>"; }
?> </ul><p><a href="#" onclick="displayForm();">Want to try again?</a></p></div> <?php
}
function send_emails(&$input, &$errors, &$success){
require_once('./includes/phpmailer/class.phpmailer.php');
$return_val = true;
$shuffledArray = array();
$nonShuffledArray = array();
foreach($input['emails'] as $key => $email){
$shuffledArray[] = array(
'email' => $input['emails'][$key],
'name' => $input['names'][$key],
'other' => $input['others'][$key],
);
$nonShuffledArray[] = array(
'email' => $input['emails'][$key],
'name' => $input['names'][$key],
'other' => $input['others'][$key],
);
}
for($i = 0; $i <= 10; $i++){
$shuffledArray = array_shuffle($shuffledArray);
}
try{
foreach($shuffledArray as $key => $data):
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = MAIL_SERVER;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = SMTP_AUTH;
$mail->SMTPSecure = SMTP_SECURE;
$mail->Port = SMTP_PORT;
$mail->Username = MAIL_USERNAME;
$mail->Password = MAIL_PASSWORD;
$mail->AddReplyTo(FROM_EMAIL, FROM_NAME);
$mail->SetFrom(FROM_EMAIL, FROM_NAME);
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->AddAddress($nonShuffledArray[$key]['email'], $nonShuffledArray[$key]['name']);
$mail->Subject = "Secret Santa Assigned To You: ".$data['name'];
$messageHtml = "<p>Hi ".$nonShuffledArray[$key]['name'].",</p><p>You are assigned to purchase a ".$input['gift_value']." dollar or less gift for ".$data['name'].". Their email address is: ".$data['email']."</p>";
if(!empty($data['other'])):
$messageHtml .= "<p>Some other information: ".$data['other']."</p>";
endif;
$messageHtml .= "<p>Have a safe and fun holiday season!</p><p>This Message brought to you via Secret Santa App at <a href='".CANONICAL_URL."' title='".CANONICAL_URL."'>".CANONICAL_URL."</a><p>";
$mail->MsgHTML($messageHtml);
$mail->Send();
$success[] = $data['name']." has been assigned to a random person.";
endforeach;
} catch (Exception $e) { return_errors($errors, $input, $e); return false; }
return true;
}
function return_errors(&$errors, &$input = null, &$e = null){
?> <div class="error"><ul> <?php
if(isset($errors) && !empty($errors)){
foreach($errors as $error){ echo "<li>".$error."</li>"; }
echo "</ul>";
} else { ?> <li>Something Unknown Happened</li></ul> <?php }
?> <p><a href="#" onclick="displayForm();">Want to try again?</a></p></div> <?php
if(isset($e) && !empty($e) ){
?> <div class="hidden">Exception: <?php echo $e->getMessage(); ?> </div> <?php
}
}
function eliminate_blank_values(&$input){
foreach($input['names'] as $i => $name){
if(empty($name)){
unset($input['names'][$i]);
$input['names'] = array_values($input['names']);
//Unset email associated with empty name
unset($input['emails'][$i]);
$input['emails'] = array_values($input['emails']);
}
}
foreach($input['emails'] as $i => $email){
if(empty($email)){
unset($input['emails'][$i]);
$input['emails'] = array_values($input['emails']);
//Unset name associated with empty email
unset($input['names'][$i]);
$input['names'] = array_values($input['names']);
}
}
return true;
}
function validate_emails(&$input, &$errors){
$return_val = true;
foreach($input['emails'] as $i => $email){
if(!check_email_address($email)){
$errors[] = $input['names'][$i]."'s email address is not valid";
$return_val = false;
}
}
return $return_val;
}
function check_email_address(&$email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
?>