diff --git a/README.md b/README.md index 944d870..7f7f210 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ -Postmark Transport with all the features of Postmark (cc, bcc, attachments) -Unit Tested \ No newline at end of file +# Postmark Transport + +[![Build Status](https://travis-ci.org/OpenBuildings/postmark.png?branch=master)](https://travis-ci.org/OpenBuildings/postmark) +[![Coverage Status](https://coveralls.io/repos/OpenBuildings/postmark/badge.png?branch=master)](https://coveralls.io/r/OpenBuildings/postmark?branch=master) +[![Latest Stable Version](https://poser.pugx.org/openbuildings/postmark/v/stable.png)](https://packagist.org/packages/openbuildings/postmark) + +A full featured postmark transport for Swiftmailer, allowing attachments, html emails / parts, cc, bcc and sending multiple emails with one api call. + +## Usage + +```php +$transport = Swift_PostmarkTransport::newInstance('your api key'); + +$mailer = Swift_Mailer::newInstance($transport); +$message = Swift_Message::newInstance(); + +// Add stuff to your message +$message->setFrom('test@example.com'); +$message->setTo('test2@example.com'); +$message->setSubject('Test'); +$message->setBody('Test Email'); + +$mailer->send($message); +``` + +## License + +Copyright (c) 2012-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 diff --git a/src/Openbuildings/Postmark/Api.php b/src/Openbuildings/Postmark/Api.php index 2d62d13..193e33f 100644 --- a/src/Openbuildings/Postmark/Api.php +++ b/src/Openbuildings/Postmark/Api.php @@ -5,9 +5,10 @@ /** * Class for manupulating a server * - * @author Ivan Kerin - * @copyright (c) 2011-2013 Despark Ltd. - * @license http://www.opensource.org/licenses/isc-license.txt + * @package openbuildings\postmark + * @author Ivan Kerin + * @copyright (c) 2013 OpenBuildings Ltd. + * @license http://spdx.org/licenses/BSD-3-Clause */ class Api { diff --git a/src/Openbuildings/Postmark/Swift/PostmarkTransport.php b/src/Openbuildings/Postmark/Swift/PostmarkTransport.php new file mode 100644 index 0000000..faafde8 --- /dev/null +++ b/src/Openbuildings/Postmark/Swift/PostmarkTransport.php @@ -0,0 +1,46 @@ + + * @copyright (c) 2013 OpenBuildings Ltd. + * @license http://spdx.org/licenses/BSD-3-Clause + */ +class Swift_PostmarkTransport extends Swift_Transport_PostmarkTransport +{ + /** + * Create a new PostmarkTransport. + */ + public function __construct($token = NULL) + { + \Swift_DependencyContainer::getInstance() + ->register('transport.postmark') + ->asNewInstanceOf('Openbuildings\Postmark\Swift_Transport_PostmarkTransport') + ->withDependencies(array('transport.eventdispatcher')); + + call_user_func_array( + array($this, 'Openbuildings\Postmark\Swift_Transport_PostmarkTransport::__construct'), + \Swift_DependencyContainer::getInstance() + ->createDependenciesFor('transport.postmark') + ); + + if ($token) + { + $this->api(new Api($token)); + } + } + + /** + * Create a new PostmarkTransport instance. + * + * @return Swift_PostmarkTransport + */ + public static function newInstance($token = NULL) + { + return new self($token); + } +} diff --git a/src/Openbuildings/Postmark/Postmark/Transport.php b/src/Openbuildings/Postmark/Swift/Transport/PostmarkTransport.php similarity index 72% rename from src/Openbuildings/Postmark/Postmark/Transport.php rename to src/Openbuildings/Postmark/Swift/Transport/PostmarkTransport.php index fb5cfb3..57b32f8 100644 --- a/src/Openbuildings/Postmark/Postmark/Transport.php +++ b/src/Openbuildings/Postmark/Swift/Transport/PostmarkTransport.php @@ -5,20 +5,19 @@ /** * Class for manupulating a server * - * @author Ivan Kerin - * @copyright (c) 2011-2013 Despark Ltd. - * @license http://www.opensource.org/licenses/isc-license.txt + * @package openbuildings\postmark + * @author Ivan Kerin + * @copyright (c) 2013 OpenBuildings Ltd. + * @license http://spdx.org/licenses/BSD-3-Clause */ -class Postmark_Transport implements \Swift_Transport { +class Swift_Transport_PostmarkTransport implements \Swift_Transport { protected $_api; + protected $_eventDispatcher; - public function __construct($token = NULL) + public function __construct(\Swift_Events_EventDispatcher $eventDispatcher) { - if ($token) - { - $this->api(new Api($token)); - } + $this->_eventDispatcher = $eventDispatcher; } public function api(Api $api = NULL) @@ -31,11 +30,6 @@ public function api(Api $api = NULL) return $this->_api; } - public static function newInstance($token) - { - return new Postmark_Transport($token); - } - public function isStarted() { return FALSE; @@ -59,9 +53,12 @@ public function stop() protected function getMIMEPart(\Swift_Mime_Message $message, $mime_type) { $part_content = NULL; - foreach ($message->getChildren() as $part) { + foreach ($message->getChildren() as $part) + { if (strpos($part->getContentType(), $mime_type) === 0) + { $part_content = $part; + } } return $part_content; } @@ -73,6 +70,13 @@ protected function getMIMEPart(\Swift_Mime_Message $message, $mime_type) */ public function send(\Swift_Mime_Message $message, & $failed_recipients = NULL) { + if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) { + $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); + if ($evt->bubbleCancelled()) { + return 0; + } + } + $data = array( 'From' => join(',', array_keys($message->getFrom())), 'To' => join(',', array_keys($message->getTo())), @@ -114,7 +118,7 @@ public function send(\Swift_Mime_Message $message, & $failed_recipients = NULL) $data['HtmlBody'] = $html->getBody(); } - if ($message->getChildren()) + if ($message->getChildren()) { $data['Attachments'] = array(); @@ -133,12 +137,16 @@ public function send(\Swift_Mime_Message $message, & $failed_recipients = NULL) $this->api()->send($data); + if ($evt) { + $evt->setResult(\Swift_Events_SendEvent::RESULT_SUCCESS); + $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); + } + return 1; } - + public function registerPlugin(\Swift_Events_EventListener $plugin) { - throw new \Exception('Postmark Transport does not support swiftmailer plugins'); - + $this->_eventDispatcher->bindEventListener($plugin); } } \ No newline at end of file diff --git a/tests/tests/ApiTest.php b/tests/tests/ApiTest.php index 7ec46f4..384414f 100644 --- a/tests/tests/ApiTest.php +++ b/tests/tests/ApiTest.php @@ -57,12 +57,12 @@ public function test_send() ) ) )); - - $this->assertEquals('Test job accepted', $response['Message']); + + $this->assertThat($response['Message'], $this->logicalOr($this->equalTo('Test job accepted'), $this->equalTo('Message accepted, but delivery may be delayed.'))); $this->setExpectedException('Exception'); - $api->send(array( + $response = $api->send(array( 'Wrong' => 'support@example.com', )); } diff --git a/tests/tests/Postmark/TransportTest.php b/tests/tests/Swift/PostmarkTransportTest.php similarity index 89% rename from tests/tests/Postmark/TransportTest.php rename to tests/tests/Swift/PostmarkTransportTest.php index 39a89cf..50877c1 100644 --- a/tests/tests/Postmark/TransportTest.php +++ b/tests/tests/Swift/PostmarkTransportTest.php @@ -1,16 +1,17 @@ assertInstanceOf('Openbuildings\Postmark\Api', $transport->api()); + $api = $this->getMock('Openbuildings\Postmark\Api', array(), array('POSTMARK_API_TEST')); $transport->api($api); $mailer = Swift_Mailer::newInstance($transport); @@ -53,7 +54,7 @@ public function test_send() ) ))); - $message = Swift_Message::newInstance($transport); + $message = Swift_Message::newInstance(); $message->setFrom('test@example.com'); $message->setTo('test2@example.com'); @@ -62,7 +63,7 @@ public function test_send() $mailer->send($message); - $message = Swift_Message::newInstance($transport); + $message = Swift_Message::newInstance(); $message->setFrom('test@example.com'); $message->setTo(array('test2@example.com', 'test3@example.com')); @@ -71,7 +72,7 @@ public function test_send() $mailer->send($message); - $message = Swift_Message::newInstance($transport); + $message = Swift_Message::newInstance(); $message->setFrom('test12@example.com'); $message->setTo('test13@example.com'); @@ -87,8 +88,6 @@ public function test_send() $transport->stop(); - $this->setExpectedException('Exception'); - $transport->registerPlugin($this->getMock('Swift_Events_EventListener')); } } \ No newline at end of file