Skip to content

Commit

Permalink
Added Postmark support.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tthumphrey committed Apr 22, 2012
1 parent f1a936a commit 746dd6f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
37 changes: 35 additions & 2 deletions README.md
Expand Up @@ -18,10 +18,11 @@ The following list of providers are currently supported, feel free to fork and a
* Last.fm
* LinkedIn
* MailChimp
* Postmark
* Tumblr
* Twitter
* Vimeo
* [Twtmore](http://dev.twtmore.com/)
* Vimeo

Usage
-----
Expand Down Expand Up @@ -225,8 +226,40 @@ Asana (HTTP Auth Basic)
return $user;
}

Postmark
--------

<?php

function send_email()
{
$provider = 'postmark';
if (($options = Config::load($provider)) === false) {
$options = Config::get($provider);
}

$postmark = Api::forge($provider, $options);

try
{
$message = $postmark->post('send', array(
'To' => 'user@example.com',
'From' => 'send@example.com',
'Subject' => 'Hi there!',
'TextBody' => 'Hi!'
));
}
catch (Api\ApiException $e)
{
$message = null;
logger(\Fuel::L_ERROR, $e->getMessage(), __METHOD__);
}

return $message;
}

Twtmore
-----------------------
-------

<?php

Expand Down
5 changes: 3 additions & 2 deletions bootstrap.php
Expand Up @@ -23,9 +23,10 @@
'Api\\Api_HTTP_Auth' => __DIR__.'/classes/provider/httpauth.php',
'Api\\Api_HTTP_Auth_Basic' => __DIR__.'/classes/provider/httpauth/basic.php',
'Api\\Api_Asana' => __DIR__.'/classes/provider/httpauth/basic/asana.php',

'Api\\Api_Mailchimp' => __DIR__.'/classes/provider/mailchimp.php',
'Api\\Api_Lastfm' => __DIR__.'/classes/provider/lastfm.php',
'Api\\Api_Twtmore' => __DIR__.'/classes/provider/twtmore.php',
'Api\\Api_Postmark' => __DIR__.'/classes/provider/postmark.php',
'Api\\Api_Twtmore' => __DIR__.'/classes/provider/twtmore.php',

));
75 changes: 75 additions & 0 deletions classes/provider/postmark.php
@@ -0,0 +1,75 @@
<?php

namespace Api;

class Api_Postmark extends Api
{
protected $api_key = null;

public function __construct($options = array())
{
$this->api_key = $options['api_key'];
}

public function api_url()
{
return 'http://api.postmarkapp.com/email';
}

public function build_request($path, $params = array(), $type = 'POST')
{
$params = \Format::forge($params)->to_json();
$request = \Request::forge($this->api_url(), array('driver' => 'curl'), $type)

->set_header('X-Postmark-Server-Token', $this->api_key)
->set_header('Accept', 'application/json')
->set_header('Content-Type', 'application/json')
->set_options(array(
'SSL_VERIFYPEER' => false,
'SSL_VERIFYHOST' => false,
'CUSTOMREQUEST' => $type,
'POSTFIELDS' => $params
));

return $request;
}

public function callback($request)
{
$message = 'An error occurred connecting to the Postmark servers';
$code = 0;

try
{
$data = $request->execute();
$data = json_decode(json_encode($data->response()->body));

if (isset($data->ErrorCode))
{
throw new ApiException($data->Message, $data->ErrorCode);
}

return $data;
}
catch (\RequestStatusException $e)
{
$data = json_decode($e->getMessage());

if (isset($data->ErrorCode))
{
$code = $data->ErrorCode;
$message = $data->Message;
}
}
catch (ApiException $e)
{
throw $e;
}
catch (\Exception $e)
{
throw new ApiException($e->getMessage());
}

throw new ApiException($message, $code);
}
}
6 changes: 6 additions & 0 deletions config/postmark.php
@@ -0,0 +1,6 @@
<?php

return array(
'api_key' => 'POSTMARK_API_TEST',

);

0 comments on commit 746dd6f

Please sign in to comment.