-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsendmails.php
489 lines (423 loc) · 20.8 KB
/
sendmails.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
// This file is part of Olam Autoresponder.
// Copyright (c) 2004-2007 Aaron Colman and Adaptive Business Design.
// Copyright (c) 2016 Anna Burdette, Benjamin Jobson, and David Reed.
//
// Olam Autoresponder is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.
//
// Olam Autoresponder is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olam Autoresponder. If not, see <http://www.gnu.org/licenses/>.
if (isset($_REQUEST['silent']) && $_REQUEST['silent'] == "1") {
$silent = TRUE;
} else {
$silent = FALSE;
}
include_once 'common.php';
require_once 'class.phpmailer.php';
if ($silent != TRUE) {
print "<html>\n";
print "<head>\n";
print " <title>Olam Responder Mailer</title>\n";
print " <meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">\n";
print "</head>\n";
}
# Verbose
if ($silent != TRUE) {
echo "Loading...<br>\n";
}
# Check mail and bounces?
if ($sendmails_included != TRUE) {
if ($config['check_mail'] == '1') {
$included = TRUE;
if ($silent != TRUE) {
echo "Running MailChecker...<br>\n";
}
include_once('mailchecker.php');
}
if ($config['check_bounces'] == '1') {
$included = TRUE;
if ($silent != TRUE) {
echo "Running BounceChecker...<br>\n";
}
include_once('bouncechecker.php');
}
}
# Verbose
if ($silent != TRUE) {
echo "<br>Initializing...<br>\n";
}
# Init the batch send count
$Send_Count = 0;
# Prep the daily send count
$now = time();
if ($config['daily_reset'] == 0) {
$config['daily_reset'] = $now;
$query = "UPDATE InfResp_config SET daily_count = '0', daily_reset = '$now'";
$result = $DB->query($query) or die("Invalid query: " . $DB->error);
}
$reset_time = strtotime("+1 day", $config['daily_reset']);
if ($now > $reset_time) {
# It's time to reset the count!
$config['daily_reset'] = $now;
$config['daily_count'] = 0;
$query = "UPDATE InfResp_config SET daily_count = '0', daily_reset = '$now'";
$result = $DB->query($query) or die("Invalid query: " . $DB->error);
}
# - - - - - - - - - - - - - - - - - - -
# Pre-cache DB data to reduce SQL calls
# - - - - - - - - - - - - - - - - - - -
# Check the send counts first...
if ($config['daily_count'] <= $config['daily_limit']) {
# Verbose
if ($silent != TRUE) {
echo "Pre-caching the database...<br>\n";
}
# Cache the messages
$query = "SELECT * FROM InfResp_messages ORDER BY MsgID";
$DB_Message_Result = $DB->query($query) or die("Invalid query: " . $DB->error);
while ($this_row = $DB_Message_Result->fetch_assoc()) {
$message_id = $this_row['MsgID'];
$message_array[$message_id] = $this_row;
}
$DB_Message_Result->free();
$today = new DateTime();
$today->setTime(0, 0, 0);
$today_string = $today->format('Y-m-d');
# Cache the responders
$query = "SELECT * FROM InfResp_responders WHERE '$today_string' > StartDate OR StartDate IS NULL ORDER BY ResponderID";
$DB_Responder_Result = $DB->query($query) or die("Invalid query: " . $DB->error);
while ($this_row = $DB_Responder_Result->fetch_assoc()) {
$responder_id = $this_row['ResponderID'];
$responder_array[$responder_id] = $this_row;
}
$DB_Responder_Result->free();
# Cache the subscribers that are currently subscribed
$query = "SELECT * FROM InfResp_subscribers WHERE Confirmed = '1' AND IsSubscribed = '1' ORDER BY ResponderID";
$DB_Subscriber_Result = $DB->query($query) or die("Invalid query: " . $DB->error);
while ($this_row = $DB_Subscriber_Result->fetch_assoc()) {
$subscriber_id = $this_row['SubscriberID'];
$subscriber_array[$subscriber_id] = $this_row;
}
$DB_Subscriber_Result->free();
# Cache the mail messages
$query = "SELECT * FROM InfResp_mail ORDER BY Mail_ID";
$DB_Mail_Result = $DB->query($query) or die("Invalid query: " . $DB->error);
while ($this_row = $DB_Mail_Result->fetch_assoc()) {
$mail_id = $this_row['Mail_ID'];
$mail_msg_array[$mail_id] = $this_row;
}
$DB_Mail_Result->free();
} else {
# Verbose
if ($silent != TRUE) {
echo "Daily throttle reached!<br>\n";
}
}
# - - - - - - - - - - - - - - - - - - -
# Handle the responder-style messages
# - - - - - - - - - - - - - - - - - - -
# Are we under the send counts?
if (($Send_Count <= $max_send_count) && ($config['daily_count'] <= $config['daily_limit']) && (count($subscriber_array) > 0)) {
# Verbose
if ($silent != TRUE) {
echo "<br>Checking responder messages...<br>\n";
}
# Yes, start going thru the subscribers to handle the responder-style msgs
foreach ($subscriber_array as $subscriber_id => $this_subscriber) {
if ($Send_Count <= $max_send_count) {
# Fetch the responder ID
$this_responder_id = $this_subscriber['ResponderID'];
# Get the message list for this subscriber's responder
$this_responder = @$responder_array[$this_responder_id];
if (empty($this_responder)) {
# This responder doesn't have any messages, continue on to the next one
continue;
}
# Split and process the list
$DB_MsgList = trim($this_responder['MsgList'], ",");
$DB_MsgList = trim($DB_MsgList);
$MsgList_Array = explode(',', $DB_MsgList);
$Max_Index = sizeof($MsgList_Array);
# Work thru the msg list
for ($msg_idx = 0; $msg_idx < $Max_Index; $msg_idx++) {
$msg_id = $MsgList_Array[$msg_idx];
$msg_data = $message_array[$msg_id];
# Check to see if the message ID is in the message list.
if ((!(isInList($this_subscriber['SentMsgs'], $msg_id))) && ($Send_Count <= $max_send_count) && (is_numeric($msg_id)) && ($config['daily_count'] <= $config['daily_limit'])) {
# Figure out the time that this subscriber should receive this message.
# Seconds math (mins, hours, days).
$message_send_time = $this_subscriber['TimeJoined'] + $msg_data['SecMinHoursDays'];
# Months math.
if ($msg_data['Months'] > 0) {
$month_str = "+" . $msg_data['Months'] . " months";
$message_send_time = strtotime($month_str, $message_send_time);
}
# Check bounds
if (!(is_numeric($msg_data['absHours']))) {
$msg_data['absHours'] = 0;
}
if (!(is_numeric($msg_data['absMins']))) {
$msg_data['absMins'] = 0;
}
# Calculate absolute positioning.
if (($msg_data['absDay'] != "") || ($msg_data['absHours'] > 0) || ($msg_data['absMins'] > 0)) {
# Reposition the clock to the day
$that_day = date('j F Y', $message_send_time);
$message_send_time = strtotime($that_day);
# Figure the next day
if (($msg_data['absDay'] == "Monday") || ($msg_data['absDay'] == "Tuesday") || ($msg_data['absDay'] == "Wednesday") || ($msg_data['absDay'] == "Thursday") || ($msg_data['absDay'] == "Friday") || ($msg_data['absDay'] == "Saturday") || ($msg_data['absDay'] == "Sunday")) {
# Get this day
$day_in_question = date('l', $message_send_time);
# Do we need to find the next day?
if ($day_in_question != $msg_data['absDay']) {
# Yes, reposition the day
$day_str = "next " . $msg_data['absDay'];
$message_send_time = strtotime($day_str, $message_send_time);
}
}
# Add the hours
if ($msg_data['absHours'] > 0) {
$message_send_time = strtotime("+" . $msg_data['absHours'] . " hours", $message_send_time);
}
# Add the minutes
if ($msg_data['absMins'] > 0) {
$message_send_time = strtotime("+" . $msg_data['absMins'] . " minutes", $message_send_time);
}
}
# Ok, we've constructed the correct send time, is it time yet?
if (time() >= $message_send_time) {
# Yes, it is.
# Make the new msg str
$NewMsgStr = $this_subscriber['SentMsgs'] . "," . $msg_id;
$NewMsgStr = trim($NewMsgStr, ",");
$NewMsgStr = trim($NewMsgStr);
$this_subscriber['SentMsgs'] = $NewMsgStr;
# Update a little more data
$Set_LastActivity = time();
$this_subscriber['LastActivity'] = $Set_LastActivity;
$subscriber_array[$subscriber_id]['LastActivity'] = $Set_LastActivity;
# Set the tag variables
$DB_TimeJoined = $this_subscriber['TimeJoined'];
$DB_Real_TimeJoined = $this_subscriber['Real_TimeJoined'];
$DB_EmailAddress = $this_subscriber['EmailAddress'];
$DB_LastActivity = $this_subscriber['LastActivity'];
$DB_FirstName = $this_subscriber['FirstName'];
$DB_LastName = $this_subscriber['LastName'];
$CanReceiveHTML = $this_subscriber['CanReceiveHTML'];
$DB_SubscriberID = $this_subscriber['SubscriberID'];
$DB_SentMsgs = $this_subscriber['SentMsgs'];
$DB_UniqueCode = $this_subscriber['UniqueCode'];
$DB_ResponderName = $this_responder['Name'];
$DB_OwnerEmail = $this_responder['OwnerEmail'];
$DB_OwnerName = $this_responder['OwnerName'];
$DB_ReplyToEmail = $this_responder['ReplyToEmail'];
$DB_ResponderDesc = $this_responder['ResponderDesc'];
$DB_MsgBodyHTML = $msg_data['BodyHTML'];
$DB_MsgBodyText = $msg_data['BodyText'];
$DB_MsgSub = $msg_data['Subject'];
$Responder_ID = $this_responder_id;
$Send_Subject = "$DB_MsgSub";
$subcode = "s" . $DB_UniqueCode;
$unsubcode = "u" . $DB_UniqueCode;
$UnsubURL = $siteURL . $ResponderDirectory . "/confirm_subscription.php?c=$unsubcode";
# Filter the email address of a few nasties
$DB_EmailAddress = stripNewlines(str_replace("|", "", $DB_EmailAddress));
$DB_EmailAddress = str_replace(">", "", $DB_EmailAddress);
$DB_EmailAddress = str_replace("<", "", $DB_EmailAddress);
$DB_EmailAddress = str_replace('/', "", $DB_EmailAddress);
$DB_EmailAddress = str_replace('..', "", $DB_EmailAddress);
# Process the tags
processMessageTags();
$mail = new PHPMailer();
$mail->setFrom($DB_ReplyToEmail, $DB_OwnerName);
$mail->addReplyTo($DB_ReplyToEmail);
$mail->addAddress($DB_EmailAddress);
$mail->Subject = $Send_Subject;
$mail->msgHTML($DB_MsgBodyHTML);
$mail->AltBody = $DB_MsgBodyText;
if (
!empty($msg_data['attachmentName']) &&
!empty($msg_data['attachmentStorageName']) &&
file_exists(__DIR__ . '/storage/' . $msg_data['attachmentStorageName'])
) {
$mail->addAttachment(__DIR__ . '/storage/' . $msg_data['attachmentStorageName'], $msg_data['attachmentName']);
}
if ($mail->send()) {
# Verbose
if ($silent != TRUE) {
echo "Responder msg to sub #" . $subscriber_id . "<br>\n";
}
# Update the DB
$query = "UPDATE InfResp_subscribers
SET SentMsgs = '$NewMsgStr',
LastActivity = '$Set_LastActivity'
WHERE SubscriberID = '$DB_SubscriberID'";
$DB_result = $DB->query($query) or die("Invalid query: " . $DB->error);
# Increment the send counts
$Send_Count++;
$config['daily_count']++;
} else {
// TODO: handle mail errors
}
}
}
}
}
}
} else {
if (count($subscriber_array) > 0) {
# Verbose
if ($silent != TRUE) {
echo "No responder messages sent - throttle limit reached.<br>\n";
}
} else {
# Verbose
if ($silent != TRUE) {
echo "No subscribers to send to yet.<br>\n";
}
}
}
# - - - - - - - - - - - - - - - - - - -
# Handle the newsletter-style messages
# - - - - - - - - - - - - - - - - - - -
# Are we under the send counts?
if (($Send_Count <= $max_send_count) && ($config['daily_count'] <= $config['daily_limit'])) {
# Verbose
if ($silent != TRUE) {
echo "<br>Checking newsletter messages...<br>\n";
}
# Check for unsent mail in the cache
$update_list = "";
$query = "SELECT * FROM InfResp_mail_cache WHERE Status = 'queued'";
$DB_Mail_Cache_Result = $DB->query($query) or die("Invalid query: " . $DB->error);
while ($this_entry = $DB_Mail_Cache_Result->fetch_assoc()) {
# Should we send?
if (($Send_Count <= $max_send_count) && ($config['daily_count'] <= $config['daily_limit']) && ($mail_msg_array[$mail_id]['Closed'] == "0") && ($mail_msg_array[$mail_id]['Time_To_Send'] <= time())) {
# Fetch the cache entry details
$mail_id = $this_entry['Mail_ID'];
$sub_id = $this_entry['SubscriberID'];
$cache_id = $this_entry['Cache_ID'];
# Get the other relevant data
$this_mail_msg = $mail_msg_array[$mail_id];
$responder_id = $this_mail_msg['ResponderID'];
$this_responder = $responder_array[$responder_id];
$this_subscriber = @$subscriber_array[$sub_id];
if (empty($this_subscriber)) {
// The subscriber has unsubscribed. Don't bother sending the newsletter to them
$DB->query("DELETE FROM InfResp_mail_cache WHERE SubscriberID = '$sub_id'") or die("Invalid query: " . $DB->error);
continue;
}
# Set the tag variables and send?
if (!(isEmptyArray($this_subscriber))) {
$DB_TimeJoined = $this_subscriber['TimeJoined'];
$DB_Real_TimeJoined = $this_subscriber['Real_TimeJoined'];
$DB_EmailAddress = $this_subscriber['EmailAddress'];
$DB_LastActivity = $this_subscriber['LastActivity'];
$DB_FirstName = $this_subscriber['FirstName'];
$DB_LastName = $this_subscriber['LastName'];
$CanReceiveHTML = $this_subscriber['CanReceiveHTML'];
$DB_SubscriberID = $this_subscriber['SubscriberID'];
$DB_SentMsgs = $this_subscriber['SentMsgs'];
$DB_UniqueCode = $this_subscriber['UniqueCode'];
$DB_ResponderName = $this_responder['Name'];
$DB_OwnerEmail = $this_responder['OwnerEmail'];
$DB_OwnerName = $this_responder['OwnerName'];
$DB_ReplyToEmail = $this_responder['ReplyToEmail'];
$DB_ResponderDesc = $this_responder['ResponderDesc'];
$DB_MsgBodyHTML = $this_mail_msg['HTML_msg'];
$DB_MsgBodyText = $this_mail_msg['TEXT_msg'];
$DB_MsgSub = $this_mail_msg['Subject'];
$Responder_ID = $this_responder_id;
$Send_Subject = "$DB_MsgSub";
$subcode = "s" . $DB_UniqueCode;
$unsubcode = "u" . $DB_UniqueCode;
$UnsubURL = $siteURL . $ResponderDirectory . "/confirm_subscription.php?c=$unsubcode";
# Filter the email address of a few nasties
$DB_EmailAddress = stripNewlines(str_replace("|", "", $DB_EmailAddress));
$DB_EmailAddress = str_replace(">", "", $DB_EmailAddress);
$DB_EmailAddress = str_replace("<", "", $DB_EmailAddress);
$DB_EmailAddress = str_replace('/', "", $DB_EmailAddress);
$DB_EmailAddress = str_replace('..', "", $DB_EmailAddress);
# Process the tags
processMessageTags();
$mail = new PHPMailer();
$mail->setFrom($DB_ReplyToEmail, $DB_OwnerName);
$mail->addReplyTo($DB_ReplyToEmail);
$mail->addAddress($DB_EmailAddress);
$mail->Subject = $Send_Subject;
$mail->msgHTML($DB_MsgBodyHTML);
$mail->AltBody = $DB_MsgBodyText;
if ($mail->send()) {
# Verbose
if ($silent != TRUE) {
echo "Newsletter msg to sub #" . $sub_id . "<br>\n";
}
# Update the last activity field
$Set_LastActivity = time();
$this_subscriber['LastActivity'] = $Set_LastActivity;
$subscriber_array[$sub_id]['LastActivity'] = $Set_LastActivity;
$query = "UPDATE InfResp_subscribers SET LastActivity = '$Set_LastActivity' WHERE SubscriberID = '$DB_SubscriberID'";
$DB_result = $DB->query($query) or die("Invalid query: " . $DB->error);
# Update the cache database
$query = "UPDATE InfResp_mail_cache SET Status = 'sent', LastActivity = '$Set_LastActivity' WHERE Cache_ID = '$cache_id'";
$DB_result = $DB->query($query) or die("Invalid query: " . $DB->error);
# Increment the send counts
$Send_Count++;
$config['daily_count']++;
} else {
// TODO: handle errors
}
}
}
}
} else {
# Verbose
if ($silent != TRUE) {
echo "No newsletter messages sent - throttle limit reached.<br>\n";
}
}
# - - - - - - - - - - - - - - - - - - -
# Update the daily count in the DB
# - - - - - - - - - - - - - - - - - - -
$query = "UPDATE InfResp_config SET daily_count = '" . $config['daily_count'] . "'";
$result = $DB->query($query) or die("Invalid query: " . $DB->error);
# Verbose
if ($Send_Count > 0) {
if ($silent != TRUE) {
echo "<br>Updating counts...<br>\n";
}
} else {
if ($silent != TRUE) {
echo "<br>No messages sent.<br>\n";
}
}
# - - - - - - - - - - - - - - - - - - -
# Handle last activity trim
# - - - - - - - - - - - - - - - - - - -
if (($last_activity_trim > 0) && ($this_subscriber['LastActivity'] != "") AND ($this_subscriber['LastActivity'] != NULL) AND ($this_subscriber['LastActivity'] != 0)) {
# Set some vars
$trim_str = "+" . $last_activity_trim . " months";
# Loop thru the subscribers
foreach ($subscriber_array as $subscriber_id => $this_subscriber) {
$trim_time = strtotime($trim_str, $this_subscriber['LastActivity']);
if (time() > $trim_time) {
$query = "DELETE FROM InfResp_subscribers WHERE SubscriberID = '" . $this_subscriber['SubscriberID'] . "'";
$DB_result = $DB->query($query) or die("Invalid query: " . $DB->error);
$query = "DELETE FROM InfResp_customfields WHERE user_attached = '" . $this_subscriber['SubscriberID'] . "'";
$result = $DB->query($query) or die("Invalid query: " . $DB->error);
}
}
}
# Verbose
if ($silent != TRUE) {
echo "Done!<br>\n";
}
# Reset var
$silent = FALSE;