From b5a33ceed967b89055fd48a6f2b68411e1ef4414 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 20 May 2016 10:13:54 -0400
Subject: [PATCH] Added more flexible recipient support.

---
 module/VuFind/src/VuFind/Mailer/Mailer.php    | 22 ++++++---
 .../src/VuFindTest/Mailer/MailerTest.php      | 46 ++++++++++++++++++-
 2 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/module/VuFind/src/VuFind/Mailer/Mailer.php b/module/VuFind/src/VuFind/Mailer/Mailer.php
index e5fb8f02a94..118c8fc5a12 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 ef1875f6bf4..11231b38d47 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.
      *
-- 
GitLab