Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Postmark PHP Library
<?php
/**
* Send emails with Postmark (http://postmarkapp.com)
*
* Author: Matthew Loberg
* Author Twitter: @mloberg
* Author Website: http://mloberg.com
*/
class Postmark{
private $api_key;
private $from;
private $reply_to;
private $data = array();
const EMAIL_ENDPOINT = 'http://api.postmarkapp.com/email';
const BATCH_ENDPOINT = 'http://api.postmarkapp.com/email/batch';
function __construct($apikey, $from, $reply=""){
$this->api_key = $apikey;
$this->from = $from;
$this->reply_to = $reply;
}
protected function __send($endpoint, $data){
if(!is_array($data) && empty($data)){
throw new LogicException('Data has not been setup!');
}else{
if(empty($endpoint)) $endpoint = self::EMAIL_ENDPOINT;
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"X-Postmark-Server-Token: {$this->api_key}"
);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$resp = new PostmarkResponse($http_code, $error, $response);
return $resp;
}
}
protected function data($data = null){
if(!is_null($data)){
$this->data = $data;
}else{
return $this->data;
}
}
private function __plain($msg){
$this->data['TextBody'] = $msg;
return $this;
}
private function __html($msg){
$this->data['HtmlBody'] = "<html><body>{$body}</body></html>";
return $this;
}
public function send(){
if(empty($this->data['To'])){
throw new Exception('Postmark::to() has not been set!');
}else{
$this->data['From'] = $this->from;
$this->data['ReplyTo'] = $this->reply_to;
$resp = $this->__send(self::EMAIL_ENDPOINT, $this->data);
$this->data = array();
return $resp;
}
}
public function to($to){
$this->data['To'] = $to;
return $this;
}
public function cc($cc){
$this->data['Cc'] = $cc;
return $this;
}
public function bcc($bcc){
$this->data['Bcc'] = $bcc;
return $this;
}
public function subject($subject){
$this->data['subject'] = $subject;
return $this;
}
public function message($msg, $type = 'text'){
if($type == 'html'){
return $this->__html($msg);
}else{
return $this->__plain($msg);
}
}
public function tag($tag){
$this->data['Tag'] = $tag;
return $this;
}
}
class PostmarkBatch extends Postmark{
private $batch = array();
private $info = array();
function __construct($apikey, $from, $reply = ''){
$this->info = array(
'from' => $from,
'reply' => $reply
);
parent::__construct($apikey, $from, $reply);
}
public function send(){
if(empty($this->batch)){
throw new Exception('No information has not been set!');
}else{
$resp = $this->__send(self::BATCH_ENDPOINT, $this->batch);
$this->batch = array();
return $resp;
}
}
public function add(){
$email = $this->data();
if(empty($email['To'])){
throw new Exception('PostmarkBatch::to() has not been set!');
}else{
$email['From'] = $this->info['from'];
$email['ReplyTo'] = $this->info['reply'];
array_push($this->batch, $email);
$this->data(array());
}
return $this;
}
}
class PostmarkResponse extends Postmark{
private $code;
private $error;
private $response;
function __construct($code, $error, $response){
$this->code = $code;
$this->error = $error;
$this->response = $response;
}
public function sent(){
return ($this->code !== 200) ? false : true;
}
public function http_code(){
return $this->code;
}
public function error(){
return $this->error;
}
public function response(){
return json_decode($this->response);
}
}
<?php
/**
* Postmark is not designed for transactional emails, however if you need to send a batch email where the link may be different,
* or run a large site where if you did transactional emails as they happen would make a ton of requests, batch might be a good option.
* Read more at http://developer.postmarkapp.com/developer-build.html#batching-messages
*/
include_once('postmark.php');
$postmark = new PostmarkBatch('api-key', 'from', 'optional_reply_to');
$emails = array(
'mail@example.com' => 'The email message.',
'mail2@example.com' => 'Their email message.'
);
foreach($emails as $email => $message){
$postmark->to($email)->subject('Email Subject')->message($message)->add();
}
try{
$resp = $postmark->send();
if($resp->sent()){
echo 'Messages sent!';
}else{
print_r($resp->error());
print_r($resp->response());
}
}catch(Exception $e){
echo $e->getMessage();
}
<?php
include_once('postmark.php');
$postmark = new Postmark('api-key', 'from', 'optional_reply_to');
// plain text message
$postmark->to('mail@example.com')->subject('Email Subject')->message('Email plain content.')->tag('email-tag-for-postmark');
try{
$resp = $postmark->send();
if($resp->sent()){
echo 'Message sent!';
}else{
print_r($resp->error());
print_r($resp->response());
}
}catch(Exception $e){
echo $e->getMessage();
}
// html message
$postmark->to('mail@example.com')->subject('Email Subject')->message('<p>HTML email.</p>', 'html');
try{
$resp = $postmark->send();
if($resp->sent()){
echo 'Message sent!';
}else{
print_r($resp->error());
print_r($resp->response());
}
}catch(Exception $e){
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.