From 4ca6cc7cc6f1dc4a588921693a8e547b2bafb009 Mon Sep 17 00:00:00 2001 From: Ivan Kerin Date: Mon, 11 Aug 2014 15:18:22 +0300 Subject: [PATCH] Fix for empty recipients 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 --- LICENSE | 2 +- README.md | 4 +- composer.lock | 18 +++++--- phpunit.xml | 2 +- .../Swiftmailer/FilterPlugin.php | 45 +++++++++++-------- tests/bootstrap.php | 13 ++++++ tests/classes/TestListener.php | 32 +++++++++++++ tests/tests/FilterPluginTest.php | 35 ++++++++++++++- 8 files changed, 119 insertions(+), 32 deletions(-) create mode 100644 tests/bootstrap.php create mode 100644 tests/classes/TestListener.php diff --git a/LICENSE b/LICENSE index 0c32042..f74208c 100644 --- a/LICENSE +++ b/LICENSE @@ -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. \ No newline at end of file +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. diff --git a/README.md b/README.md index 6b3a029..01ff5c7 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. \ No newline at end of file +Under BSD-3-Clause license, read LICENSE file. diff --git a/composer.lock b/composer.lock index 1b6424c..f1dd40c 100644 --- a/composer.lock +++ b/composer.lock @@ -1,7 +1,8 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" ], "hash": "4a0b819914581f2275e16bddcaf18de0", "packages": [ @@ -10,16 +11,16 @@ "packages-dev": [ { "name": "swiftmailer/swiftmailer", - "version": "v5.0.1", + "version": "v5.0.3", "source": { "type": "git", "url": "https://github.com/swiftmailer/swiftmailer.git", - "reference": "v5.0.1" + "reference": "32edc3b0de0fdc1b10f5c4912e8677b3f411a230" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/v5.0.1", - "reference": "v5.0.1", + "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/32edc3b0de0fdc1b10f5c4912e8677b3f411a230", + "reference": "32edc3b0de0fdc1b10f5c4912e8677b3f411a230", "shasum": "" }, "require": { @@ -43,7 +44,9 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com" + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" }, { "name": "Chris Corbyn" @@ -55,7 +58,7 @@ "mail", "mailer" ], - "time": "2013-06-17 13:32:32" + "time": "2013-12-03 13:33:24" } ], "aliases": [ @@ -65,6 +68,7 @@ "stability-flags": [ ], + "prefer-stable": false, "platform": { "php": ">=5.3.2" }, diff --git a/phpunit.xml b/phpunit.xml index 27f8ad1..a1dcc21 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,4 +1,4 @@ - + tests/tests diff --git a/src/Openbuildings/Swiftmailer/FilterPlugin.php b/src/Openbuildings/Swiftmailer/FilterPlugin.php index 116b74f..6736e24 100644 --- a/src/Openbuildings/Swiftmailer/FilterPlugin.php +++ b/src/Openbuildings/Swiftmailer/FilterPlugin.php @@ -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); @@ -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) { @@ -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 @@ -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]); } @@ -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) { @@ -107,7 +107,7 @@ public function setWhitelist($whitelist) /** * Getter - * @return array + * @return array */ public function getWhitelist() { @@ -116,7 +116,7 @@ public function getWhitelist() /** * Setter, array or string - * @param array|string $blacklist + * @param array|string $blacklist */ public function setBlacklist($blacklist) { @@ -127,7 +127,7 @@ public function setBlacklist($blacklist) /** * Getter - * @return array + * @return array */ public function getBlacklist() { @@ -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(); + } } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..d78988c --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,13 @@ + + * @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) + { + + } +} diff --git a/tests/tests/FilterPluginTest.php b/tests/tests/FilterPluginTest.php index db5c6fe..b8f68bf 100644 --- a/tests/tests/FilterPluginTest.php +++ b/tests/tests/FilterPluginTest.php @@ -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(); @@ -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() @@ -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); @@ -167,4 +198,4 @@ public function test_filterEmailArray($whitelist, $blacklist, $array, $expected) { $this->assertEquals($expected, FilterPlugin::filterEmailArray($whitelist, $blacklist, $array)); } -} \ No newline at end of file +}