Skip to content

Instantly share code, notes, and snippets.

@andrebaptista
Forked from mloberg/example.php
Last active October 28, 2016 19:05
Show Gist options
  • Save andrebaptista/5615013 to your computer and use it in GitHub Desktop.
Save andrebaptista/5615013 to your computer and use it in GitHub Desktop.

Revisions

  1. andrebaptista revised this gist May 20, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.php
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,6 @@

    $postmark = new Postmark("your-api-key","from-email","optional-reply-to-address");

    if($postmark->to("[email protected]")->subject("Email Subject")->plain_message("This is a plain text message.")->send()){
    if($postmark->to("[email protected]")->subject("Email Subject")->html_message("<p style='color: red'>Test mail</p>")->send()){
    echo "Message sent";
    }
  2. andrebaptista revised this gist May 20, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions postmark.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function send(){
    $ch = curl_init('http://api.postmarkapp.com/email');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, html_entity_decode(json_encode($data)));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $return = curl_exec($ch);
    $curl_error = curl_error($ch);
    @@ -50,7 +50,7 @@ function subject($subject){
    }

    function html_message($body){
    $this->data["HtmlBody"] = "<html><body>{$body}</body></html>";
    $this->data["HtmlBody"] = htmlentities("<html><body>".$body."</body></html>");
    return $this;
    }

  3. @mloberg mloberg revised this gist Mar 10, 2011. 1 changed file with 10 additions and 39 deletions.
    49 changes: 10 additions & 39 deletions postmark.php
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,18 @@
    <?php

    /**
    * This is a simple library for sending emails with Postmark created by Matthew Loberg (http://mloberg.com)
    */

    class Postmark{

    private $api_key;
    private $from;
    private $reply;
    private $message;
    private $message_type;
    private $to;
    private $subject;
    private $data;
    private $tag;
    private $data = array();

    function __construct($apikey,$from,$reply=""){
    $this->api_key = $apikey;
    $this->from = $from;
    $this->reply = $reply;
    $this->data["From"] = $from;
    $this->data["ReplyTo"] = $reply;
    }

    function send(){
    @@ -28,7 +21,7 @@ function send(){
    "Content-Type: application/json",
    "X-Postmark-Server-Token: {$this->api_key}"
    );
    $data = $this->prepare_data();
    $data = $this->data;
    $ch = curl_init('http://api.postmarkapp.com/email');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    @@ -47,50 +40,28 @@ function send(){
    }

    function to($to){
    $this->to = $to;
    $this->data["To"] = $to;
    return $this;
    }

    function subject($subject){
    $this->subject = $subject;
    $this->data["subject"] = $subject;
    return $this;
    }

    function html_message($body){
    $this->message = "<html><body>{$body}</body></html>";
    $this->message_type = 'html';
    $this->data["HtmlBody"] = "<html><body>{$body}</body></html>";
    return $this;
    }

    function plain_message($msg){
    $this->message = $msg;
    $this->message_type = 'text';
    $this->data["TextBody"] = $msg;
    return $this;
    }

    function tag($tag){
    $this->tag = $tag;
    $this->data["Tag"] = $tag;
    return $this;
    }

    private function prepare_data(){
    $data = array(
    'Subject' => $this->subject
    );
    $data['From'] = $this->from;
    $data['To'] = $this->to;
    if($this->reply){
    $data['ReplyTo'] = $this->reply;
    }
    if($this->tag){
    $data['Tag'] = $this->tag;
    }
    if($this->message_type == 'text'){
    $data['TextBody'] = $this->message;
    }else{
    $data['HtmlBody'] = $this->message;
    }
    return $data;
    }

    }
  4. @mloberg mloberg revised this gist Mar 10, 2011. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions postmark.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    <?php

    /**
    * This is a simple library for sending emails with Postmark
    *
    * Usage: $this->postmark->to(email)->subject(subject)->message_type(message)->send();
    * This is a simple library for sending emails with Postmark created by Matthew Loberg (http://mloberg.com)
    */

    class Postmark{
  5. @mloberg mloberg created this gist Mar 10, 2011.
    9 changes: 9 additions & 0 deletions example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?php

    require("postmark.php");

    $postmark = new Postmark("your-api-key","from-email","optional-reply-to-address");

    if($postmark->to("[email protected]")->subject("Email Subject")->plain_message("This is a plain text message.")->send()){
    echo "Message sent";
    }
    98 changes: 98 additions & 0 deletions postmark.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    <?php

    /**
    * This is a simple library for sending emails with Postmark
    *
    * Usage: $this->postmark->to(email)->subject(subject)->message_type(message)->send();
    */

    class Postmark{

    private $api_key;
    private $from;
    private $reply;
    private $message;
    private $message_type;
    private $to;
    private $subject;
    private $data;
    private $tag;

    function __construct($apikey,$from,$reply=""){
    $this->api_key = $apikey;
    $this->from = $from;
    $this->reply = $reply;
    }

    function send(){
    $headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "X-Postmark-Server-Token: {$this->api_key}"
    );
    $data = $this->prepare_data();
    $ch = curl_init('http://api.postmarkapp.com/email');
    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);
    curl_close($ch);
    // do some checking to make sure it sent
    if($http_code !== 200){
    return false;
    }else{
    return true;
    }
    }

    function to($to){
    $this->to = $to;
    return $this;
    }

    function subject($subject){
    $this->subject = $subject;
    return $this;
    }

    function html_message($body){
    $this->message = "<html><body>{$body}</body></html>";
    $this->message_type = 'html';
    return $this;
    }

    function plain_message($msg){
    $this->message = $msg;
    $this->message_type = 'text';
    return $this;
    }

    function tag($tag){
    $this->tag = $tag;
    return $this;
    }

    private function prepare_data(){
    $data = array(
    'Subject' => $this->subject
    );
    $data['From'] = $this->from;
    $data['To'] = $this->to;
    if($this->reply){
    $data['ReplyTo'] = $this->reply;
    }
    if($this->tag){
    $data['Tag'] = $this->tag;
    }
    if($this->message_type == 'text'){
    $data['TextBody'] = $this->message;
    }else{
    $data['HtmlBody'] = $this->message;
    }
    return $data;
    }

    }