diff --git a/module/VuFind/src/VuFind/Mailer/Mailer.php b/module/VuFind/src/VuFind/Mailer/Mailer.php index e5fb8f02a94f4f144539d0c4e0b96d4c23d3e160..118c8fc5a12d3e7a4f8d6e2149d1583020c7910a 100644 --- a/module/VuFind/src/VuFind/Mailer/Mailer.php +++ b/module/VuFind/src/VuFind/Mailer/Mailer.php @@ -27,6 +27,7 @@ */ namespace VuFind\Mailer; use VuFind\Exception\Mail as MailException, + Zend\Mail\Address, Zend\Mail\AddressList, Zend\Mail\Message, Zend\Mail\Header\ContentType; @@ -131,19 +132,26 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface /** * Send an email message. * - * @param string $to Recipient email address (or + * @param string|Address|AddressList $to Recipient email address (or * delimited list) - * @param string|\Zend\Mail\Address $from Sender name and email address - * @param string $subject Subject line for message - * @param string $body Message body - * @param string $cc CC recipient (null for none) + * @param string|Address $from Sender name and email address + * @param string $subject Subject line for message + * @param string $body Message body + * @param string $cc CC recipient (null for none) * * @throws MailException * @return void */ public function send($to, $from, $subject, $body, $cc = null) { - $recipients = $this->stringToAddressList($to); + if ($to instanceof AddressList) { + $recipients = $to; + } else if ($to instanceof Address) { + $recipients = new AddressList(); + $recipients->add($to); + } else { + $recipients = $this->stringToAddressList($to); + } // Validate email addresses: if ($this->maxRecipients > 0 @@ -160,7 +168,7 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface throw new MailException('Invalid Recipient Email Address'); } } - $fromEmail = ($from instanceof \Zend\Mail\Address) + $fromEmail = ($from instanceof Address) ? $from->getEmail() : $from; if (!$validator->isValid($fromEmail)) { throw new MailException('Invalid Sender Email Address'); diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php index ef1875f6bf405a9a571efd0ce4ed8d1525abb804..11231b38d471ae8703359f920eab38e0c1e18f75 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php @@ -28,6 +28,7 @@ namespace VuFindTest\Mailer; use VuFind\Mailer\Mailer; use Zend\Mail\Address; +use Zend\Mail\AddressList; /** * Mailer Test Class @@ -64,7 +65,7 @@ class MailerTest extends \VuFindTest\Unit\TestCase * * @return void */ - public function testSendWithAddressObject() + public function testSendWithAddressObjectInSender() { $callback = function ($message) { $fromString = $message->getFrom()->current()->toString(); @@ -80,6 +81,49 @@ class MailerTest extends \VuFindTest\Unit\TestCase $mailer->send('to@example.com', $address, 'subject', 'body'); } + /** + * Test sending an email using an address object for the To field. + * + * @return void + */ + public function testSendWithAddressObjectInRecipient() + { + $callback = function ($message) { + $fromString = $message->getFrom()->current()->toString(); + return 'Recipient TextName <to@example.com>' == $message->getTo()->current()->toString() + && '<from@example.com>' == $message->getFrom()->current()->toString() + && 'body' == $message->getBody() + && 'subject' == $message->getSubject(); + }; + $transport = $this->getMock('Zend\Mail\Transport\TransportInterface'); + $transport->expects($this->once())->method('send')->with($this->callback($callback)); + $address = new Address('to@example.com', 'Recipient TextName'); + $mailer = new Mailer($transport); + $mailer->send($address, 'from@example.com', 'subject', 'body'); + } + + /** + * Test sending an email using an address list object for the To field. + * + * @return void + */ + public function testSendWithAddressListObjectInRecipient() + { + $callback = function ($message) { + $fromString = $message->getFrom()->current()->toString(); + return 'Recipient TextName <to@example.com>' == $message->getTo()->current()->toString() + && '<from@example.com>' == $message->getFrom()->current()->toString() + && 'body' == $message->getBody() + && 'subject' == $message->getSubject(); + }; + $transport = $this->getMock('Zend\Mail\Transport\TransportInterface'); + $transport->expects($this->once())->method('send')->with($this->callback($callback)); + $list = new AddressList(); + $list->add(new Address('to@example.com', 'Recipient TextName')); + $mailer = new Mailer($transport); + $mailer->send($list, 'from@example.com', 'subject', 'body'); + } + /** * Test bad to address. *