forked from e107inc/e107
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_manager_class.php
1383 lines (1234 loc) · 44.7 KB
/
mail_manager_class.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* e107 website system
*
* Copyright (C) 2008-2010 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* e107 Mailout - mail database API and utility routines
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/mail_manager_class.php,v $
* $Revision$
* $Date$
* $Author$
*/
/**
*
* @package e107
* @subpackage e107_handlers
* @version $Id$;
*
* @todo - consider whether to extract links in text-only emails
This class isolates the caller from the underlying database used to buffer and send emails.
Also includes a number of useful routines
This is the 'day to day' module - there's an admin class which extends this one.
There are two parts to the database:
a) Email body (including attachments etc)
b) Target recipients - potentially including target-specific values to substitute
There is an option to override the style information sent if the email is to include
theme-related information. Create file 'emailstyle.css' in the current theme directory, and this
will be included in preference to the current theme style.
Event Triggers generated
------------------------
mailbounce - when an email bounce is received
maildone - when the sending of a complete bulk email is complete (also does 'Notify' event)
Database tables
---------------
mail_recipients - Details of individual recipients (targets) of an email
mail_target_id Unique ID for this target/email combination
mail_recipient_id User ID (if registered user), else zero
mail_recipient_email Email address of recipient
mail_recipient_name Name of recipient
mail_status Status of this entry - see define() statements below
mail_detail_id Email body link
mail_send_date Earliest date/time when email may be sent. Once mail sent, actual time/date of sending (or time of failure to send)
mail_target_info Array of target-specific info for substitution into email. Key is the code in the email body, value is the substitution
mail_content - Details of the email to be sent to a number of people
mail_source_id
mail_content_status Overall status of mailshot record - See define() statements below
mail_togo_count Number of recipients to go
mail_sent_count Number of successful sends (including bounces)
mail_fail_count Number of unsuccessful sends
mail_bounce_count Number of bounced emails
mail_start_send Time/date of sending first email
mail_end_send Time/date of sending last email
mail_create_date
mail_creator User ID
mail_create_app ID string for application/plugin creating mail
mail_e107_priority Our internal priority - generally high for single emails, low for bulk emails
mail_notify_complete Notify options when email complete
mail_last_date Don't send after this date/time
mail_title A description of the mailout - not sent
mail_subject Subject line
mail_body Body text
mail_other Evaluates to an array of misc info - cc, bcc, attachments etc
Within internal arrays, a flat structure is adopted. Variables relating to DB values all begin 'mail_' - others are internal (volatile) control variables
*/
if (!defined('e107_INIT')) { exit; }
define('MAIL_STATUS_SENT', 0); // Mail sent. Email handler happy, but may have bounced (or may be yet to bounce)
define('MAIL_STATUS_BOUNCED', 1);
define('MAIL_STATUS_CANCELLED', 2);
define('MAIL_STATUS_PARTIAL', 3); // A run which was abandoned - errors, out of time etc
define('MAIL_STATUS_FAILED', 5); // Failure on initial send - rejected by selected email handler
// This must be the numerically highest 'processing complete' code
define('MAIL_STATUS_PENDING', 10); // Mail which is in the sending list (even if outside valid sending window)
// This must be the numerically lowest 'not sent' code
// E107_EMAIL_MAX_TRIES values used in here for retry counting
define('MAIL_STATUS_MAX_ACTIVE', 19); // Highest allowable 'not sent or processed' code
define('MAIL_STATUS_SAVED', 20); // Identifies an email which is just saved (or in process of update)
define('MAIL_STATUS_HELD',21); // Held pending release
define('MAIL_STATUS_TEMP', 22); // Tags entries which aren't yet in any list
class e107MailManager
{
const E107_EMAIL_PRIORITY_LOW = 1; // 'E107' priorities, to determine what to do next.
const E107_EMAIL_PRIORITY_MED = 3; // Distinct from the priority which can be assigned to the...
const E107_EMAIL_PRIORITY_HIGH = 5; // actual email when sending. Use LOW or MED for bulk mail, HIGH for individual emails.
const E107_EMAIL_MAX_TRIES = 3; // Maximum number of tries by us (mail server may do more)
// - max allowable value is MAIL_STATUS_MAX_ACTIVE - MAIL_STATUS_PENDING
private $debugMode = 0;
protected $e107;
protected $db = NULL; // Use our own database object - this one for reading data
protected $db2 = NULL; // Use our own database object - this one for updates
protected $queryActive = FALSE; // Keeps track of unused records in currently active query
protected $mailCounters = array(); // Counters to track adding recipients
protected $queryCount = array(); // Stores total number of records if SQL_CALC_ROWS is used (index = db object #)
protected $currentBatchInfo = array(); // Used during batch send to hold info about current mailout
protected $currentMailBody = ''; // Buffers current mail body
protected $mailer = NULL; // Mailer class when required
// Array defines DB types to be used
protected $dbTypes = array(
'mail_recipients' => array
(
'mail_target_id' => 'int',
'mail_recipient_id' => 'int',
'mail_recipient_email' => 'todb',
'mail_recipient_name' => 'todb',
'mail_status' => 'int',
'mail_detail_id' => 'int',
'mail_send_date' => 'int',
'mail_target_info' => 'string' // Don't want entities here!
),
'mail_content' => array(
'mail_source_id' => 'int',
'mail_content_status' => 'int',
'mail_togo_count' => 'int',
'mail_sent_count' => 'int',
'mail_fail_count' => 'int',
'mail_bounce_count' => 'int',
'mail_start_send' => 'int',
'mail_end_send' => 'int',
'mail_create_date' => 'int',
'mail_creator' => 'int',
'mail_create_app' => 'todb',
'mail_e107_priority' => 'int',
'mail_notify_complete' => 'int',
'mail_last_date' => 'int',
'mail_title' => 'todb',
'mail_subject' => 'todb',
'mail_body' => 'todb',
'mail_other' => 'string' // Don't want entities here!
)
);
// Array defines defaults for 'NOT NULL' fields where a default can't be set in the field definition
protected $dbNull = array('mail_recipients' => array
(
'mail_target_info' => ''
),
'mail_content' => array(
'mail_body' => '',
'mail_other' => ''
)
);
// List of fields which are combined into the 'mail_other' field of the email
protected $dbOther = array(
'mail_sender_email' => 1,
'mail_sender_name' => 1,
'mail_copy_to' => 1,
'mail_bcopy_to' => 1,
'mail_attach' => 1,
'mail_send_style' => 1,
'mail_selectors' => 1, // Only used internally
'mail_include_images' => 1 // Used to determine whether to embed images, or link to them
);
// List of fields which are the status counts of an email, and their titles
protected $mailCountFields = array(
'mail_togo_count' => LAN_MAILOUT_83,
'mail_sent_count' => LAN_MAILOUT_82,
'mail_fail_count' => LAN_MAILOUT_128,
'mail_bounce_count' => LAN_MAILOUT_144,
);
/**
* Constructor
*
*
* @return void
*/
public function __construct()
{
$this->e107 = e107::getInstance();
}
/**
* Generate an array of data which can be passed directly to the DB routines.
* Only valid DB fields are copied
* Combining/splitting of fields is done as necessary
* (This is essentially the translation between internal storage format and db storage format. If
* the DB format changes, only this routine and its counterpart should need changing)
*
* @param $data - array of email-related data in internal format
* @param $addMissing - if TRUE, undefined fields are added
*
* @return void
*/
public function mailToDb(&$data, $addMissing = FALSE)
{
$res = array();
$res1 = array();
// Generate the 'mail_other' array first
foreach ($this->dbOther as $f => $v)
{
if (isset($data[$f]))
{
$res1[$f] = $data[$f];
}
elseif ($addMissing)
{
$res1[$f] = '';
}
}
// Now do the main email array
foreach ($this->dbTypes['mail_content'] as $f => $v)
{
if (isset($data[$f]))
{
$res[$f] = $data[$f];
}
elseif ($addMissing)
{
$res[$f] = '';
}
}
$array = new ArrayData;
$res['mail_other'] = $array->WriteArray($res1, TRUE); // Ready to write to DB
return $res;
}
/**
* Given an array (row) of data retrieved from the DB table, converts to internal format.
* Combining/splitting of fields is done as necessary
* (This is essentially the translation between internal storage format and db storage format. If
* the DB format changes, only this routine and its counterpart should need changing)
*
* @param $data - array of DB-sourced email-related data
* @param $addMissing - if TRUE, undefined fields are added
*
* @return array of data
*/
public function dbToMail(&$data, $addMissing = FALSE)
{
$res = array();
foreach ($this->dbTypes['mail_content'] as $f => $v)
{
if (isset($data[$f]))
{
$res[$f] = $data[$f];
}
elseif ($addMissing)
{
$res[$f] = '';
}
}
if (isset($data['mail_other']))
{
$array = new ArrayData;
$tmp = $array->ReadArray($data['mail_other']);
if (is_array($tmp))
{
$res = array_merge($res,$tmp);
}
unset($res['mail_other']);
}
elseif ($addMissing)
{
foreach ($this->dbOther as $f => $v)
{
$res[$f] = '';
}
}
return $res;
}
/**
* Generate an array of mail recipient data which can be passed directly to the DB routines.
* Only valid DB fields are copied
* Combining/splitting of fields is done as necessary
* (This is essentially the translation between internal storage format and db storage format. If
* the DB format changes, only this routine and its counterpart should need changing)
*
* @param $data - array of email target-related data in internal format
* @param $addMissing - if TRUE, undefined fields are added
*
* @return void
*/
public function targetToDb(&$data, $addMissing = FALSE)
{ // Direct correspondence at present (apart from needing to convert potential array $data['mail_target_info']) - but could change
$res = array();
foreach ($this->dbTypes['mail_recipients'] as $f => $v)
{
if (isset($data[$f]))
{
$res[$f] = $data[$f];
}
elseif ($addMissing)
{
$res[$f] = '';
}
}
if (isset($data['mail_target_info']) && is_array($data['mail_target_info']))
{
$array = new ArrayData;
$tmp = $array->WriteArray($data['mail_target_info'], TRUE);
$res['mail_target_info'] = $tmp;
}
return $res;
}
/**
* Given an array (row) of data retrieved from the DB table, converts to internal format.
* Combining/splitting of fields is done as necessary
* (This is essentially the translation between internal storage format and db storage format. If
* the DB format changes, only this routine and its counterpart should need changing)
*
* @param $data - array of DB-sourced target-related data
* @param $addMissing - if TRUE, undefined fields are added
*
* @return void
*/
public function dbToTarget(&$data, $addMissing = FALSE)
{ // Direct correspondence at present - but could change
$res = array();
foreach ($this->dbTypes['mail_recipients'] as $f => $v)
{
if (isset($data[$f]))
{
$res[$f] = $data[$f];
}
elseif ($addMissing)
{
$res[$f] = '';
}
}
if (isset($data['mail_target_info']))
{
$array = new ArrayData;
$tmp = $array->ReadArray($data['mail_target_info']);
$res['mail_target_info'] = $tmp;
}
return $res;
}
/**
* Given an array (row) of data retrieved from the DB table, converts to internal format.
* Combining/splitting of fields is done as necessary
* This version intended for 'Joined' reads which have both recipient and content data
*
* @param $data - array of DB-sourced target-related data
* @param $addMissing - if TRUE, undefined fields are added
*
* @return void
*/
public function dbToBoth(&$data, $addMissing = FALSE)
{
$res = array();
$oneToOne = array_merge($this->dbTypes['mail_content'], $this->dbTypes['mail_recipients']); // List of valid elements
// Start with simple 'one to one' fields
foreach ($oneToOne as $f => $v)
{
if (isset($data[$f]))
{
$res[$f] = $data[$f];
}
elseif ($addMissing)
{
$res[$f] = '';
}
}
// Now array fields
$array = new ArrayData;
if (isset($data['mail_other']))
{
$tmp = $array->ReadArray($data['mail_other']);
if (is_array($tmp))
{
$res = array_merge($res,$tmp);
}
unset($res['mail_other']);
}
elseif ($addMissing)
{
foreach ($this->dbOther as $f => $v)
{
$res[$f] = '';
}
}
if (isset($data['mail_target_info']))
{
$tmp = $array->ReadArray($data['mail_target_info']);
$res['mail_target_info'] = $tmp;
}
return $res;
}
/**
* Set the internal debug/logging level
*
* @return void
*/
public function controlDebug($level = 0)
{
$this->debugMode = $level;
}
// Internal function to create a db object for our use if none exists
protected function checkDB($which = 1)
{
if (($which == 1) && ($this->db == NULL))
{
$this->db = new db;
}
if (($which == 2) && ($this->db2 == NULL))
{
$this->db2 = new db;
}
}
// Internal function to create a mailer object for our use if none exists
protected function checkMailer()
{
if ($this->mailer != NULL) return;
if (!class_exists('e107Email'))
{
require_once(e_HANDLER.'mail.php');
}
$this->mailer = new e107Email; // Could add in overrides here
}
/**
* Convert numeric represntation of mail status to a text string
*
* @param integer $status - numeric value of status
* @return string text value
*/
public function statusToText($status)
{
switch (intval($status))
{
case MAIL_STATUS_SENT :
return LAN_MAILOUT_211;
case MAIL_STATUS_BOUNCED :
return LAN_MAILOUT_213;
case MAIL_STATUS_CANCELLED :
return LAN_MAILOUT_218;
case MAIL_STATUS_PARTIAL :
return LAN_MAILOUT_219;
case MAIL_STATUS_FAILED :
return LAN_MAILOUT_212;
case MAIL_STATUS_PENDING :
return LAN_MAILOUT_214;
case MAIL_STATUS_SAVED :
return LAN_MAILOUT_215;
case MAIL_STATUS_HELD :
return LAN_MAILOUT_217;
default :
if (($status > MAIL_STATUS_PENDING) && ($status <= MAIL_STATUS_ACTIVE)) return LAN_MAILOUT_214;
}
return LAN_MAILOUT_216.' ('.$status.')'; // General coding error
}
/**
* Select the next $count emails in the send queue
* $count gives the maximum number. '*' does 'select all'
* @return boolean|handle Returns FALSE on error.
* Returns a 'handle' on success (actually the ID in the DB of the email)
*/
public function selectEmails($count = 1)
{
if (is_numeric($count))
{
if ($count < 1) $count = 1;
$count = ' LIMIT '.$count;
}
else
{
$count = '';
}
$this->checkDB(1); // Make sure DB object created
$query = "SELECT mt.*, ms.* FROM `#mail_recipients` AS mt
LEFT JOIN `#mail_content` AS ms ON mt.`mail_detail_id` = ms.`mail_source_id`
WHERE ms.`mail_content_status` = ".MAIL_STATUS_PENDING."
AND mt.`mail_status` >= ".MAIL_STATUS_PENDING."
AND mt.`mail_status` <= ".MAIL_STATUS_MAX_ACTIVE."
AND mt.`mail_send_date` <= ".time()."
AND (ms.`mail_last_date` >= ".time()." OR ms.`mail_last_date`=0)
ORDER BY ms.`mail_e107_priority` DESC {$count}";
// echo $query.'<br />';
$result = $this->db->db_Select_gen($query);
if ($result !== FALSE)
{
$this->queryActive = $result; // Note number of emails to go
}
return $result;
}
/**
* Get next email from selection
* @return Returns array of email data if available - FALSE if no further data, no active query, or other error
*/
public function getNextEmail()
{
if (!$this->queryActive)
{
return FALSE;
}
if ($result = $this->db->db_Fetch(MYSQL_ASSOC))
{
$this->queryActive--;
return $this->dbToBoth($result);
// return array_merge($this->dbToMail($result), $this->dbToTarget($result));
}
else
{
$this->queryActive = FALSE; // Make sure no further attempts to read emails
return FALSE;
}
}
/**
* Call to see whether any emails left to try in current selection
* @return Returns number left unread in query - FALSE if no active query
*/
public function emailsToGo()
{
return $this->queryActive; // Just return saved number
}
/**
* Call to send next email from selection
*
* @return Returns TRUE if successful, FALSE on fail (or no more to go)
*/
public function sendNextEmail()
{
$counterList = array('mail_source_id','mail_togo_count', 'mail_sent_count', 'mail_fail_count', 'mail_start_send');
if (($email = $this->getNextEmail()) === FALSE)
{
return FALSE;
}
if (count($this->currentBatchInfo))
{
//print_a($this->currentBatchInfo);
if ($this->currentBatchInfo['mail_source_id'] != $email['mail_source_id'])
{ // New email body etc started
//echo "New email body: {$this->currentBatchInfo['mail_source_id']} != {$email['mail_source_id']}<br />";
$this->currentBatchInfo = array(); // New source email - clear stored info
$this->currentMailBody = ''; // ...and clear cache for message body
}
}
if (count($this->currentBatchInfo) == 0)
{
//echo "First email of batch: {$email['mail_source_id']}<br />";
foreach ($counterList as $k)
{
$this->currentBatchInfo[$k] = $email[$k]; // This copies across all the counts
}
}
if (($this->currentBatchInfo['mail_sent_count'] > 0) || ($this->currentBatchInfo['mail_fail_count'] > 0))
{ // Only send these on first email - otherwise someone could get inundated!
unset($email['mail_copy_to']);
unset($email['mail_bcopy_to']);
}
$targetData = array(); // Arrays for updated data
$this->checkMailer(); // Make sure we have a mailer object to play with
if ($this->currentBatchInfo['mail_start_send'] == 0)
{
$this->currentBatchInfo['mail_start_send'] = time(); // Log when we started processing this email
}
if (!$this->currentMailBody)
{
$this->currentMailBody = $this->makeEmailBody($email['mail_body'], $email['mail_send_style'], varset($email['mail_include_images'], FALSE));
}
// Do any substitutions
$search = array();
$replace = array();
foreach ($email['mail_target_info'] as $k => $v)
{
$search[] = '|'.$k.'|';
$replace[] = $v;
}
$email['mail_body'] = str_replace($search, $replace, $this->currentMailBody);
$email['send_html'] = ($email['mail_send_style'] != 'textonly');
// Set up any extra mailer parameters that need it
if (!vartrue($email['e107_header']))
{
$temp = intval($email['mail_recipient_id']).'/'.intval($email['mail_source_id']).'/'.intval($email['mail_target_id']).'/';
$email['e107_header'] = $temp.md5($temp); // Set up an ID
}
if (isset($email['mail_attach']) && (trim($email['mail_attach']) || is_array($email['mail_attach'])))
{
$downDir = realpath(e_ROOT.$this->e107->getFolder('downloads'));
if (is_array($email['mail_attach']))
{
foreach ($email['mail_attach'] as $k => $v)
{
$email['mail_attach'][$k] = $downDir.$v;
}
}
else
{
$email['mail_attach'] = $downDir.$email['mail_attach'];
}
}
// print_a($email);
// Try and send
$result = $this->mailer->sendEmail($email['mail_recipient_email'], $email['mail_recipient_name'], $email, TRUE);
// return; // ************************************************** Temporarily stop DB being updated when line active *****************************
$this->checkDB(2); // Make sure DB object created
// Now update email status in DB. We just create new arrays of changed data
if ($result === TRUE)
{ // Success!
$targetData['mail_status'] = MAIL_STATUS_SENT;
$targetData['mail_send_date'] = time();
$this->currentBatchInfo['mail_togo_count']--;
$this->currentBatchInfo['mail_sent_count']++;
}
else
{ // Failure
// If fail and still retries, downgrade priority
if ($targetData['mail_status'] > MAIL_STATUS_PENDING)
{
$targetData['mail_status'] = max($targetData['mail_status'] - 1, MAIL_STATUS_PENDING); // One off retry count
$targetData['mail_e107_priority'] = max($email['mail_e107_priority'] - 1, 1); // Downgrade priority to avoid clag-ups
}
else
{
$targetData['mail_status'] = MAIL_STATUS_FAILED;
$this->currentBatchInfo['mail_togo_count'] = max($this->currentBatchInfo['mail_togo_count'] - 1, 0);
$this->currentBatchInfo['mail_fail_count']++;
$targetData['mail_send_date'] = time();
}
}
if (isset($this->currentBatchInfo['mail_togo_count']) && ($this->currentBatchInfo['mail_togo_count'] == 0))
{
$this->currentBatchInfo['mail_end_send'] = time();
$this->currentBatchInfo['mail_content_status'] = MAIL_STATUS_SENT;
}
// Update DB record, mail record with status (if changed). Must use different sql object
if (count($targetData))
{
//print_a($targetData);
$this->db2->db_Update('mail_recipients', array('data' => $targetData,
'_FIELD_TYPES' => $this->dbTypes['mail_recipients'],
'WHERE' => '`mail_target_id` = '.intval($email['mail_target_id'])));
}
if (count($this->currentBatchInfo))
{
//print_a($this->currentBatchInfo);
$this->db2->db_Update('mail_content', array('data' => $this->currentBatchInfo,
'_FIELD_TYPES' => $this->dbTypes['mail_content'],
'WHERE' => '`mail_source_id` = '.intval($email['mail_source_id'])));
}
if (($this->currentBatchInfo['mail_togo_count'] == 0) && ($email['mail_notify_complete'] > 0))
{ // Need to notify completion
$email = array_merge($email, $this->currentBatchInfo); // This should ensure the counters are up to date
$mailInfo = LAN_MAILOUT_247.'<br />'.LAN_MAILOUT_135.': '.$email['mail_title'].'<br />'.LAN_MAILOUT_248.$this->statusToText($email['mail_content_status']).'<br />';
$mailInfo .= '<br />'.LAN_MAILOUT_249.'<br />';
foreach ($this->mailCountFields as $f => $t)
{
$mailInfo .= $t.' => '.$email[$f].'<br />';
}
$mailInfo .= LAN_MAILOUT_250;
$message = array( // Use same structure for email and notify
'mail_subject' => LAN_MAILOUT_244.$email['mail_subject'],
'mail_body' => $mailInfo.'<br />'
);
if ($email['mail_notify_complete'] & 1)
{ // Notify email initiator
if ($this->db2->db_Select('user', 'user_name, user_email', '`user_id`='.intval($email['mail_creator'])))
{
$row = $this->db2->db_Fetch(MYSQL_ASSOC);
require_once(e_HANDLER.'mail.php');
$mailer = new e107Email();
$mailer->sendEmail($row['user_name'], $row['user_email'], $message,FALSE);
}
}
if ($email['mail_notify_complete'] & 2)
{ // Do e107 notify
notify_maildone($message);
}
e107::getEvent()->trigger('maildone', $email);
}
return $result;
}
/**
* Call to do a number of 'units' of email processing - from a cron job, for example
* Each 'unit' sends one email from the queue - potentially it could do some other task.
* @param $limit - number of units of work to do - zero to clear the queue (or do maximum allowed by a hard-coded limit)
* @return None
*/
public function doEmailTask($limit = 0)
{
if ($count = $this->selectEmails($limit))
{
while ($count > 0)
{
$this->sendNextEmail();
$count--;
}
if ($this->mailer)
{
$this->mailer->allSent(); // Tidy up on completion
}
}
}
/**
* Saves an email to the DB
* @param $emailData
* @param $isNew - TRUE if a new email, FALSE if editing
*
*
* @return mail ID for success, FALSE on error
*/
public function saveEmail($emailData, $isNew = FALSE)
{
$this->checkDB(2); // Make sure we have a DB object to use
$dbData = $this->mailToDB($emailData, FALSE); // Convert array formats
// print_a($dbData);
if ($isNew)
{
unset($dbData['mail_source_id']); // Just in case - there are circumstances where might be set
$result = $this->db2->db_Insert('mail_content', array('data' => $dbData,
'_FIELD_TYPES' => $this->dbTypes['mail_content'],
'_NOTNULL' => $this->dbNull['mail_content']));
}
else
{
if (isset($dbData['mail_source_id']))
{
$result = $this->db2->db_Update('mail_content', array('data' => $dbData,
'_FIELD_TYPES' => $this->dbTypes['mail_content'],
'WHERE' => '`mail_source_id` = '.intval($dbData['mail_source_id'])));
if ($result !== FALSE) { $result = $dbData['mail_source_id']; }
}
else
{
echo "Programming bungle! No mail_source_id in function saveEmail()<br />";
$result = FALSE;
}
}
return $result;
}
/**
* Retrieve an email from the DB
* @param $mailID - number for email (assumed to be integral)
* @param $addMissing - if TRUE, any unset fields are added
*
* @return FALSE on error. Array of data on success.
*/
public function retrieveEmail($mailID, $addMissing = FALSE)
{
if (!is_numeric($mailID) || ($mailID == 0))
{
return FALSE;
}
$this->checkDB(2); // Make sure we have a DB object to use
if ($this->db2->db_Select('mail_content', '*', '`mail_source_id`='.$mailID) === FALSE)
{
return FALSE;
}
$mailData = $this->db2->db_Fetch(MYSQL_ASSOC);
return $this->dbToMail($mailData, $addMissing); // Convert to 'flat array' format
}
/**
* Delete an email from the DB, including (potential) recipients
* @param $mailID - number for email (assumed to be integral)
* @param $actions - allows selection of which DB to delete from
*
* @return FALSE on code error. Array of results on success.
*/
public function deleteEmail($mailID, $actions='all')
{
$result = array();
if ($actions == 'all') $actions = 'content,recipients';
$actArray = explode(',', $actions);
if (!is_numeric($mailID) || ($mailID == 0))
{
return FALSE;
}
$this->checkDB(2); // Make sure we have a DB object to use
if (isset($actArray['content']))
{
$result['content'] = $this->db2->db_Delete('mail_content', '`mail_source_id`='.$mailID);
}
if (isset($actArray['recipients']))
{
$result['recipients'] = $this->db2->db_Delete('mail_recipients', '`mail_detail_id`='.$mailID);
}
return $result;
}
/**
* Initialise a set of counters prior to adding
* @param $handle - as returned by makeEmail()
* @return none
*/
public function mailInitCounters($handle)
{
$this->mailCounters[$handle] = array('add' => 0, 'dups' => 0, 'dberr' => 0);
}
/**
* Add a recipient to the DB, provide that email not already on the list.
* @param $handle - as returned by makeEmail()
* @param $mailRecip is an array of relevant info
* @param $priority - 'E107' priority for email (different to the priority included in the email)
* @return mixed - FALSE if error
* 'dup' if duplicate of existing email
* integer - number of email recipient in DB
*/
public function mailAddNoDup($handle, $mailRecip, $initStatus = MAIL_STATUS_TEMP, $priority = self::E107_EMAIL_PRIORITY_LOW)
{
if (($handle <= 0) || !is_numeric($handle)) return FALSE;
if (!isset($this->mailCounters[$handle])) return 'nocounter';
$this->checkDB(1); // Make sure DB object created
$result = $this->db->db_Select('mail_recipients', 'mail_target_id', "`mail_detail_id`={$handle} AND `mail_recipient_email`='{$mailRecip['mail_recipient_email']}'");
if ($result === FALSE)
{
return FALSE;
}
elseif ($result != 0)
{
$this->mailCounters[$handle]['dups']++;
return 'dup';
}
$mailRecip['mail_status'] = $initStatus;
$mailRecip['mail_detail_id'] = $handle;
$mailRecip['mail_send_date'] = time();
$data = $this->targetToDb($mailRecip); // Convert internal types
if ($this->db->db_Insert('mail_recipients', array('data' => $data, '_FIELD_TYPES' => $this->dbTypes['mail_recipients'])))
{
$this->mailCounters[$handle]['add']++;
}
else
{
$this->mailCounters[$handle]['dberr']++;
return FALSE;
}
}
/**
* Update the mail record with the number of recipients as per counters
* @param $handle - as returned by makeEmail()
* @return mixed - FALSE if error
* - number set into counter if success
*/
public function mailUpdateCounters($handle)
{
if (($handle <= 0) || !is_numeric($handle)) return FALSE;
if (!isset($this->mailCounters[$handle])) return 'nocounter';
$this->checkDB(2); // Make sure DB object created
$query = '`mail_togo_count`='.intval($this->mailCounters[$handle]['add']).' WHERE `mail_source_id`='.$handle;
if ($this->db2->db_Update('mail_content', $query))
{
return $this->mailCounters[$handle]['add'];
}
return FALSE;
}
/**
* Retrieve the counters for a mail record
* @param $handle - as returned by makeEmail()
* @return boolean - FALSE if error
* - array of counters if success
*/
public function mailRetrieveCounters($handle)
{
if (isset($this->mailCounters[$handle]))
{
return $this->mailCounters[$handle];
}
return FALSE;
}
/**
* Update status for email, including all recipient entries (called once all recipients added)
* @param int $handle - as returned by makeEmail()
* @param $hold boolean - TRUE to set status to held, false to release for sending
* @param $notify - value to set in the mail_notify_complete field:
* 0 - no action on run complete
* 1 - notify admin who sent email only
* 2 - notify through e107 notify system only
* 3 - notify both
* @param $firstTime int - only valid if $hold === FALSE - earliest time/date when email may be sent
* @param $lastTime int - only valid if $hold === FALSE - latest time/date when email may be sent
* @return boolean TRUE on no errors, FALSE on errors
*/
public function activateEmail($handle, $hold = FALSE, $notify = 0, $firstTime = 0, $lastTime = 0)
{
if (($handle <= 0) || !is_numeric($handle)) return FALSE;
$this->checkDB(1); // Make sure DB object created
$ft = '';
$lt = '';
if (!$hold)
{ // Sending email - set sensible first and last times
if ($lastTime < (time() + 3600)) // Force at least an hour to send emails
{
if ($firstTime < time())
{
$lastTime = time() + 86400; // Standard delay - 24 hours
}
else
{
$lastTime = $firstTime + 86400;
}
}
if ($firstTime > 0) $ft = ', `mail_send_date` = '.$firstTime;
$lt = ', `mail_end_send` = '.$lastTime;
}
$query = '';
if (!$hold) $query = '`mail_creator` = '.USERID.', `mail_create_date` = '.time().', '; // Update when we send - might be someone different
$query .= '`mail_notify_complete`='.intval($notify).', `mail_content_status` = '.($hold ? MAIL_STATUS_HELD : MAIL_STATUS_PENDING).$lt.' WHERE `mail_source_id` = '.intval($handle);
// echo "Update mail body: {$query}<br />";
// Set status of email body first
if (!$this->db->db_Update('mail_content',$query))
{
$this->e107->admin_log->e_log_event(10,-1,'MAIL','Activate/hold mail','mail_content: '.$query.'[!br!]Fail: '.$this->db->mySQLlastErrText,FALSE,LOG_TO_ROLLING);
return FALSE;
}
// Now set status of individual emails