Skip to content
Snippets Groups Projects
Commit b5a33cee authored by Demian Katz's avatar Demian Katz
Browse files

Added more flexible recipient support.

parent 20f1cf73
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
*/ */
namespace VuFind\Mailer; namespace VuFind\Mailer;
use VuFind\Exception\Mail as MailException, use VuFind\Exception\Mail as MailException,
Zend\Mail\Address,
Zend\Mail\AddressList, Zend\Mail\AddressList,
Zend\Mail\Message, Zend\Mail\Message,
Zend\Mail\Header\ContentType; Zend\Mail\Header\ContentType;
...@@ -131,19 +132,26 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface ...@@ -131,19 +132,26 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
/** /**
* Send an email message. * Send an email message.
* *
* @param string $to Recipient email address (or * @param string|Address|AddressList $to Recipient email address (or
* delimited list) * delimited list)
* @param string|\Zend\Mail\Address $from Sender name and email address * @param string|Address $from Sender name and email address
* @param string $subject Subject line for message * @param string $subject Subject line for message
* @param string $body Message body * @param string $body Message body
* @param string $cc CC recipient (null for none) * @param string $cc CC recipient (null for none)
* *
* @throws MailException * @throws MailException
* @return void * @return void
*/ */
public function send($to, $from, $subject, $body, $cc = null) 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: // Validate email addresses:
if ($this->maxRecipients > 0 if ($this->maxRecipients > 0
...@@ -160,7 +168,7 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface ...@@ -160,7 +168,7 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
throw new MailException('Invalid Recipient Email Address'); throw new MailException('Invalid Recipient Email Address');
} }
} }
$fromEmail = ($from instanceof \Zend\Mail\Address) $fromEmail = ($from instanceof Address)
? $from->getEmail() : $from; ? $from->getEmail() : $from;
if (!$validator->isValid($fromEmail)) { if (!$validator->isValid($fromEmail)) {
throw new MailException('Invalid Sender Email Address'); throw new MailException('Invalid Sender Email Address');
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
namespace VuFindTest\Mailer; namespace VuFindTest\Mailer;
use VuFind\Mailer\Mailer; use VuFind\Mailer\Mailer;
use Zend\Mail\Address; use Zend\Mail\Address;
use Zend\Mail\AddressList;
/** /**
* Mailer Test Class * Mailer Test Class
...@@ -64,7 +65,7 @@ class MailerTest extends \VuFindTest\Unit\TestCase ...@@ -64,7 +65,7 @@ class MailerTest extends \VuFindTest\Unit\TestCase
* *
* @return void * @return void
*/ */
public function testSendWithAddressObject() public function testSendWithAddressObjectInSender()
{ {
$callback = function ($message) { $callback = function ($message) {
$fromString = $message->getFrom()->current()->toString(); $fromString = $message->getFrom()->current()->toString();
...@@ -80,6 +81,49 @@ class MailerTest extends \VuFindTest\Unit\TestCase ...@@ -80,6 +81,49 @@ class MailerTest extends \VuFindTest\Unit\TestCase
$mailer->send('to@example.com', $address, 'subject', 'body'); $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. * Test bad to address.
* *
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment