Skip to content

Commit

Permalink
Fix for empty recipients
Browse files Browse the repository at this point in the history
Some transports do not check for empty recipient lists.
This way no email will be sent if there are no people to send it to,
without throwing any exceptions
  • Loading branch information
ivank committed Aug 11, 2014
1 parent 47c22e3 commit 4ca6cc7
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 32 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Redistribution and use in source and binary forms, with or without modification,
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the OpenBuildings nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Swiftmailer Filter Plugin
# Swiftmailer Filter Plugin

[![Build Status](https://travis-ci.org/OpenBuildings/swiftmailer-filter.png?branch=master)](https://travis-ci.org/OpenBuildings/swiftmailer-filter)
[![Coverage Status](https://coveralls.io/repos/OpenBuildings/swiftmailer-filter/badge.png?branch=master)](https://coveralls.io/r/OpenBuildings/swiftmailer-filter?branch=master)
Expand Down Expand Up @@ -27,4 +27,4 @@ There are additional getters / setters that you might use:

Copyright (c) 2013, OpenBuildings Ltd. Developed by Ivan Kerin as part of [clippings.com](http://clippings.com)

Under BSD-3-Clause license, read LICENSE file.
Under BSD-3-Clause license, read LICENSE file.
18 changes: 11 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit colors="true" bootstrap="vendor/autoload.php">
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite>
<directory>tests/tests</directory>
Expand Down
45 changes: 26 additions & 19 deletions src/Openbuildings/Swiftmailer/FilterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class FilterPlugin implements \Swift_Events_SendListener
{
/**
* Check if an email matches a given other email or domain
* @param string $email
* @param string $email
* @param string $match email or domain
* @return boolean
* @return boolean
*/
public static function emailMatches($email, $match)
{
if ( ! filter_var($email, FILTER_VALIDATE_EMAIL))
throw new \Exception("Cannot match with '{$match}': '{$email}' is not a valid email");

if (strpos($match, '@') === FALSE)
{
list($email_name, $email_domain) = explode('@', $email);
Expand All @@ -35,9 +35,9 @@ public static function emailMatches($email, $match)

/**
* Check if a given email matches an array of emails or domains
* @param string $email
* @param array $match_array
* @return boolean
* @param string $email
* @param array $match_array
* @return boolean
*/
public static function emailMatchesArray($email, array $match_array)
{
Expand All @@ -53,7 +53,7 @@ public static function emailMatchesArray($email, array $match_array)
/**
* Filter a swiftmailer email array, e.g. [email => name] with a whitelist and blacklist array (email or domains)
* First the whitelist is applied, then the blacklist
*
*
* @param array $whitelist array of emails or domains
* @param array $blacklist array of emails or domains
* @param array $array Swiftmailer array of emails
Expand All @@ -63,20 +63,20 @@ public static function filterEmailArray(array $whitelist, array $blacklist, arra
{
if ($whitelist)
{
foreach ($array as $email => $name)
foreach ($array as $email => $name)
{
if ( ! FilterPlugin::emailMatchesArray($email, $whitelist))
if ( ! FilterPlugin::emailMatchesArray($email, $whitelist))
{
unset($array[$email]);
}
}
}

if ($blacklist)
if ($blacklist)
{
foreach ($array as $email => $name)
foreach ($array as $email => $name)
{
if (FilterPlugin::emailMatchesArray($email, $blacklist))
if (FilterPlugin::emailMatchesArray($email, $blacklist))
{
unset($array[$email]);
}
Expand All @@ -96,7 +96,7 @@ function __construct($whitelist = NULL, $blacklist = NULL)

/**
* Setter, array or string
* @param array|string $whitelist
* @param array|string $whitelist
*/
public function setWhitelist($whitelist)
{
Expand All @@ -107,7 +107,7 @@ public function setWhitelist($whitelist)

/**
* Getter
* @return array
* @return array
*/
public function getWhitelist()
{
Expand All @@ -116,7 +116,7 @@ public function getWhitelist()

/**
* Setter, array or string
* @param array|string $blacklist
* @param array|string $blacklist
*/
public function setBlacklist($blacklist)
{
Expand All @@ -127,7 +127,7 @@ public function setBlacklist($blacklist)

/**
* Getter
* @return array
* @return array
*/
public function getBlacklist()
{
Expand All @@ -143,11 +143,18 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
{
$message = $evt->getMessage();

$message->setTo(FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getTo()));
$to = FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getTo());
$cc = FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getCc());
$bcc = FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getBcc());

$message->setCc(FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getCc()));
$message->setTo($to);
$message->setCc($cc);
$message->setBcc($bcc);

$message->setBcc(FilterPlugin::filterEmailArray($this->getWhitelist(), $this->getBlacklist(), (array) $message->getBcc()));
if ( ! ($to + $cc + $bcc))
{
$evt->cancelBubble();
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once __DIR__.'/../vendor/autoload.php';

spl_autoload_register(function($class)
{
$file = __DIR__.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.str_replace('_', '/', $class).'.php';

if (is_file($file))
{
require_once $file;
}
});
32 changes: 32 additions & 0 deletions tests/classes/TestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @package openbuildings\swiftmailer-filter
* @author Ivan Kerin <ikerin@gmail.com>
* @copyright (c) 2013 OpenBuildings Ltd.
* @license http://spdx.org/licenses/BSD-3-Clause
*/
class TestListener implements \Swift_Events_SendListener
{
private $event;

public function event()
{
return $this->event;
}

public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
{
$this->event = $evt;
}

/**
* Do nothing
*
* @param Swift_Events_SendEvent $evt
*/
public function sendPerformed(\Swift_Events_SendEvent $evt)
{

}
}
35 changes: 33 additions & 2 deletions tests/tests/FilterPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function test_integration_with_empty_cc_and_bcc()
{
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());

$test_listener = new TestListener();

$mailer->registerPLugin($test_listener);
$mailer->registerPLugin(new FilterPlugin('example.com', 'test4@example.com'));

$message = Swift_Message::newInstance();
Expand All @@ -68,6 +71,34 @@ public function test_integration_with_empty_cc_and_bcc()
$this->assertEquals(array('test2@example.com' => ''), $message->getTo());
$this->assertEquals(array(), $message->getCc());
$this->assertEquals(array(), $message->getBcc());

$this->assertInstanceOf('Swift_Events_SendEvent', $test_listener->event());
$this->assertEquals(\Swift_Events_SendEvent::RESULT_SUCCESS, $test_listener->event()->getResult());
}

public function test_integration_filtered()
{
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
$test_listener = new TestListener();

$mailer->registerPLugin($test_listener);
$mailer->registerPLugin(new FilterPlugin('example.com', 'test2@example.com'));

$message = Swift_Message::newInstance();

$message->setFrom('test@example.com');
$message->setTo('test2@example.com');
$message->setSubject('Test');
$message->setBody('Test Email');

$mailer->send($message);

$this->assertEquals(array(), $message->getTo());
$this->assertEquals(array(), $message->getCc());
$this->assertEquals(array(), $message->getBcc());

$this->assertInstanceOf('Swift_Events_SendEvent', $test_listener->event());
$this->assertEquals(\Swift_Events_SendEvent::RESULT_PENDING, $test_listener->event()->getResult());
}

public function data_emailMatches()
Expand All @@ -88,7 +119,7 @@ public function data_emailMatches()
*/
public function test_emailMatches($email, $match, $expected, $exception)
{
if ($exception)
if ($exception)
{
$this->setExpectedException('Exception', $exception);
FilterPlugin::emailMatches($email, $match);
Expand Down Expand Up @@ -167,4 +198,4 @@ public function test_filterEmailArray($whitelist, $blacklist, $array, $expected)
{
$this->assertEquals($expected, FilterPlugin::filterEmailArray($whitelist, $blacklist, $array));
}
}
}

0 comments on commit 4ca6cc7

Please sign in to comment.