Skip to content

Commit

Permalink
Add readme, copyrights, make it a proper transport
Browse files Browse the repository at this point in the history
Added event handlers for the transport itself
  • Loading branch information
Ivan Kerin committed Sep 9, 2013
1 parent 58c642d commit 6bc3746
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 37 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
Postmark Transport with all the features of Postmark (cc, bcc, attachments)
Unit Tested
# 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('[email protected]');
$message->setTo('[email protected]');
$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.
7 changes: 4 additions & 3 deletions src/Openbuildings/Postmark/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @copyright (c) 2013 OpenBuildings Ltd.
* @license http://spdx.org/licenses/BSD-3-Clause
*/
class Api {

Expand Down
46 changes: 46 additions & 0 deletions src/Openbuildings/Postmark/Swift/PostmarkTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Openbuildings\Postmark;

/**
* Class for manupulating a server
*
* @package openbuildings\postmark
* @author Ivan Kerin <[email protected]>
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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)
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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())),
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions tests/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

use Openbuildings\Postmark\Postmark_Transport;
use Openbuildings\Postmark\Swift_PostmarkTransport;

/**
* @group api
* @group swift.postmark-transport
*/
class Postmark_TransportTest extends PHPUnit_Framework_TestCase {
class Swift_PostmarkTransportTest extends PHPUnit_Framework_TestCase {

public function test_send()
{
$transport = Postmark_Transport::newInstance('POSTMARK_API_TEST');

$transport = Swift_PostmarkTransport::newInstance('POSTMARK_API_TEST');
$this->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);
Expand Down Expand Up @@ -53,7 +54,7 @@ public function test_send()
)
)));

$message = Swift_Message::newInstance($transport);
$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
Expand All @@ -62,7 +63,7 @@ public function test_send()

$mailer->send($message);

$message = Swift_Message::newInstance($transport);
$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo(array('[email protected]', '[email protected]'));
Expand All @@ -71,7 +72,7 @@ public function test_send()

$mailer->send($message);

$message = Swift_Message::newInstance($transport);
$message = Swift_Message::newInstance();

$message->setFrom('[email protected]');
$message->setTo('[email protected]');
Expand All @@ -87,8 +88,6 @@ public function test_send()

$transport->stop();

$this->setExpectedException('Exception');

$transport->registerPlugin($this->getMock('Swift_Events_EventListener'));
}
}

0 comments on commit 6bc3746

Please sign in to comment.