Skip to content

Commit 979536e

Browse files
committed
Releasing 1.0.2
2 parents 2063ac6 + ce4c72b commit 979536e

File tree

5 files changed

+95
-49
lines changed

5 files changed

+95
-49
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ $bccConfig = BccConfig::from($jsonStringOrObjectOrAssocArray);
377377
$emailConfig = EmailConfig::from($jsonStringOrObjectOrAssocArray);
378378
```
379379

380+
## Sending and queueing multiple e-mails
381+
382+
It is possible to pass an array of `Email` objects to `send()` and `queue()` functions. However, especially for sending
383+
e-mails immediately you should be aware that this can take some time. A better strategy is to queue mass mailings.
384+
380385
# Development Notes
381386

382387
Most PHPUnit tests will not be executed when there is no SMTP server or database available. The unit tests will check

src/TgEmail/EmailAddress.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,24 @@ public function __toString() {
5555
public static function from($s, $name = NULL) {
5656
if (is_string($s)) {
5757
if ($name == NULL) {
58-
$pos = strpos($s, '<');
59-
if ($pos !== FALSE) {
60-
$name = $pos > 0 ? trim(substr($s, 0, $pos)) : NULL;
61-
if ($name == '') $name = NULL;
58+
if (substr($s, 0, 1) == '{') {
59+
return self::from(json_decode($s));
60+
} else {
61+
$pos = strpos($s, '<');
62+
if ($pos !== FALSE) {
63+
$name = $pos > 0 ? trim(substr($s, 0, $pos)) : NULL;
64+
if ($name == '') $name = NULL;
6265

63-
$email = substr($s, $pos + 1);
64-
$pos = strpos($email, '>');
65-
if ($pos !== FALSE) {
66-
$email = trim(substr($email, 0, $pos));
67-
}
66+
$email = substr($s, $pos + 1);
67+
$pos = strpos($email, '>');
68+
if ($pos !== FALSE) {
69+
$email = trim(substr($email, 0, $pos));
70+
}
6871

69-
return new EmailAddress($email, $name);
70-
}
71-
return new EmailAddress($s);
72+
return new EmailAddress($email, $name);
73+
}
74+
return new EmailAddress($s);
75+
}
7276
} else {
7377
return new EmailAddress($s, $name);
7478
}

src/TgEmail/EmailQueue.php

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,6 @@ public function setMailMode($mailMode, $config = NULL) {
6464
$this->config->setMailMode($mailMode, $config);
6565
}
6666

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-
9667
protected function getMailer() {
9768
if ($this->mailer == null) {
9869
$this->mailer = new PHPMailer();
@@ -226,10 +197,25 @@ public function getReconfiguredEmail(Email $email) {
226197
return $rc;
227198
}
228199

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+
}
233219
}
234220

235221
/**
@@ -298,10 +284,53 @@ protected function _send(Email $email) {
298284
return $rc;
299285
}
300286

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+
}
305334
}
306335

307336
/**

test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
vendor/phpunit/phpunit/phpunit tests/
3+

tests/TgEmail/EmailAddressTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function testFromWithString(): void {
3737
$this->assertEquals('John Doe <john.doe@example.com>', $addr->__toString());
3838
}
3939

40+
public function testFromWithJsonString(): void {
41+
$addr = EmailAddress::from('{"name":"John Doe","email":"john.doe@example.com"}');
42+
$this->assertEquals('John Doe <john.doe@example.com>', $addr->__toString());
43+
}
44+
4045
public function testFromWithEmailName(): void {
4146
$addr = EmailAddress::from('john.doe@example.com', 'John Doe');
4247
$this->assertEquals('John Doe <john.doe@example.com>', $addr->__toString());

0 commit comments

Comments
 (0)