@@ -64,35 +64,6 @@ public function setMailMode($mailMode, $config = NULL) {
64
64
$ this ->config ->setMailMode ($ mailMode , $ config );
65
65
}
66
66
67
- /**
68
- * TODO: smth like queue($email, $recipients) ?
69
- * Sends multiple emails.
70
- *
71
- * @param
72
- * array of
73
- * mixed recipients - array of recipients or single recipient to send to
74
- * string templateName - mail template name to be used, located in <component>/site/email/<lang>/<name>.[html|txt].php
75
- * string subject - subject
76
- * mixed params - parameters to be given to email template
77
- * string domain - the domain this mail belongs to
78
- * @return array of
79
- * boolean success - overall success
80
- * array errors - individual boolean return codes for each mail
81
- *
82
- public function queueMails($mailInfos) {
83
- $rc = array(
84
- 'success' => true,
85
- 'errors' => array(),
86
- );
87
- foreach ($mailInfos as $mail) {
88
- $c = $this->queueMail($mail['recipients'], $mail['templateName'], $mail['subject'], $mail['params']);
89
- $rc['error'][] = $c;
90
- if (! $c) $rc['success'] = false;
91
- }
92
- return $rc;
93
- }
94
- */
95
-
96
67
protected function getMailer () {
97
68
if ($ this ->mailer == null ) {
98
69
$ this ->mailer = new PHPMailer ();
@@ -226,10 +197,25 @@ public function getReconfiguredEmail(Email $email) {
226
197
return $ rc ;
227
198
}
228
199
229
- public function send (Email $ email ) {
230
- // Modify mail according to sending mode
231
- $ email = $ this ->getReconfiguredEmail ($ email );
232
- return $ this ->_send ($ email );
200
+ /**
201
+ * Sends a single email or multiple emails.
202
+ * @param mixed $email - single Email object or array of Email objects
203
+ * @return TRUE when email was sent or number of emails sent successfully
204
+ */
205
+ public function send ($ email ) {
206
+ if (is_a ($ email , 'TgEmail \\Email ' )) {
207
+ // Modify mail according to sending mode
208
+ $ email = $ this ->getReconfiguredEmail ($ email );
209
+ return $ this ->_send ($ email );
210
+ } else if (is_array ($ email )) {
211
+ $ sent = 0 ;
212
+ foreach ($ email AS $ m ) {
213
+ if ($ this ->send ($ m )) $ sent ++;
214
+ }
215
+ return $ sent ;
216
+ } else {
217
+ throw new EmailException ('Cannot send: $email must be array of Email or single Email object ' );
218
+ }
233
219
}
234
220
235
221
/**
@@ -298,10 +284,53 @@ protected function _send(Email $email) {
298
284
return $ rc ;
299
285
}
300
286
301
- public function queue (Email $ email ) {
302
- // Modify mail according to sending mode
303
- $ email = $ this ->getReconfiguredEmail ($ email );
304
- return $ this ->_queue ($ email );
287
+ /**
288
+ * Queues a single email or multiple emails.
289
+ * <p>The second parameter $recipients can be used with single Email object only.</p>
290
+ * <p>Example of $recpients:</p>
291
+ * <ul>
292
+ * <li>list of list of recipients: <code>[ ["john.doe@example.com","john@example.com"], ["jane.doe@example.com"] ]</code></li>
293
+ * <li>list of recipient objects: <code>[ {"to":"john.doe@example.com", "cc":"jane.doe@example.com"}, ... ]</code></li>
294
+ * </ul>
295
+ * @param mixed $email - single Email object or array of Email objects
296
+ * @param array $recipients - list of recipients to send the same email. Can be a list of lists (TO addresses)
297
+ * or a list of objects with to, cc or bcc attributes that define the recipients.
298
+ * @return TRUE when email was queued or number of emails queued successfully
299
+ */
300
+ public function queue ($ email , $ recipients = NULL ) {
301
+ if (is_a ($ email , 'TgEmail \\Email ' )) {
302
+ if ($ recipients == NULL ) {
303
+ // Single Email to be sent
304
+ // Modify mail according to sending mode
305
+ $ email = $ this ->getReconfiguredEmail ($ email );
306
+ return $ this ->_queue ($ email );
307
+ }
308
+ // Single email with multiple recipient definitions
309
+ $ queued = 0 ;
310
+ foreach ($ recipients AS $ def ) {
311
+ if (is_array ($ def )) {
312
+ // All TO addresses
313
+ $ email ->recipients = NULL ;
314
+ $ email ->addTo ($ def );
315
+ if ($ this ->queue ($ email )) $ queued ++;
316
+ } else {
317
+ $ email ->recipients = NULL ;
318
+ if (isset ($ def ->to )) $ email ->addTo ($ def ->to );
319
+ if (isset ($ def ->cc )) $ email ->addCc ($ def ->cc );
320
+ if (isset ($ def ->bcc )) $ email ->addBcc ($ def ->bcc );
321
+ if ($ this ->queue ($ email )) $ queued ++;
322
+ }
323
+ }
324
+ return $ queued ;
325
+ } else if (is_array ($ email )) {
326
+ $ queued = 0 ;
327
+ foreach ($ email AS $ m ) {
328
+ if ($ this ->queue ($ m )) $ queued ++;
329
+ }
330
+ return $ queued ;
331
+ } else {
332
+ throw new EmailException ('Cannot queue: $email must be array of Email or single Email object ' );
333
+ }
305
334
}
306
335
307
336
/**
0 commit comments