Skip to content

Instantly share code, notes, and snippets.

Created March 10, 2011 19:27
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mloberg/864728 to your computer and use it in GitHub Desktop.
Save mloberg/864728 to your computer and use it in GitHub Desktop.
A simple Postmark PHP Class
$postmark = new Postmark("your-api-key","from-email","optional-reply-to-address");
if($postmark->to("")->subject("Email Subject")->plain_message("This is a plain text message.")->send()){
echo "Message sent";
* This is a simple library for sending emails with Postmark created by Matthew Loberg (
class Postmark{
private $api_key;
private $data = array();
function __construct($apikey,$from,$reply=""){
$this->api_key = $apikey;
$this->data["From"] = $from;
$this->data["ReplyTo"] = $reply;
function send(){
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"X-Postmark-Server-Token: {$this->api_key}"
$data = $this->data;
$ch = curl_init('');
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);
$return = curl_exec($ch);
$curl_error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// do some checking to make sure it sent
if($http_code !== 200){
return false;
return true;
function to($to){
$this->data["To"] = $to;
return $this;
function subject($subject){
$this->data["subject"] = $subject;
return $this;
function html_message($body){
$this->data["HtmlBody"] = "<html><body>{$body}</body></html>";
return $this;
function plain_message($msg){
$this->data["TextBody"] = $msg;
return $this;
function tag($tag){
$this->data["Tag"] = $tag;
return $this;
Copy link


Checkout my forked version, I have added support for Cc

Copy link

bergonom commented Aug 4, 2011

And I have forked obayesshelton's to add support for BCC. Seemed like a logical next step:

Copy link

Sending mail with html, corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment