-
-
Save benwerd/2370358 to your computer and use it in GitHub Desktop.
A Simple Postmark PHP Class with Attachments And Batches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require("postmark.php"); | |
$postmark = new Postmark("your-api-key","from-email","optional-reply-to-address"); | |
$result = $postmark->to("[email protected]") | |
->subject("Email Subject") | |
->plain_message("This is a plain text message.") | |
->attachment('File.pdf', $file_as_string, 'application/pdf') | |
->send(); | |
if($result === true) | |
echo "Message sent"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a simple library for sending emails with Postmark. | |
* Created by Matthew Loberg (http://mloberg.com), extended and modified by Drew Johnston (http://drewjoh.com). | |
*/ | |
class Postmark { | |
private $api_key; | |
private $attachment_count = 0; | |
private $data = array(); | |
function __construct($key, $from, $reply = '') | |
{ | |
$this->api_key = $key; | |
$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 | |
); | |
$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($this->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 sendBatch($to = array()){ | |
$headers = array( | |
"Accept: application/json", | |
"Content-Type: application/json", | |
"X-Postmark-Server-Token: {$this->api_key}" | |
); | |
$data = array(); | |
if (!empty($to)) | |
foreach($to as $email) { | |
$this->data['to'] = $email; | |
$data[] = $this->data; | |
} | |
$ch = curl_init('http://api.postmarkapp.com/email/batch'); | |
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->data['To'] = $to; | |
return $this; | |
} | |
function cc($cc) | |
{ | |
$this->data["Cc"] = $cc; | |
return $this; | |
} | |
function bcc($bcc) | |
{ | |
$this->data["Bcc"] = $bcc; | |
return $this; | |
} | |
function subject($subject) | |
{ | |
$this->data['Subject'] = $subject; | |
return $this; | |
} | |
function html_message($html) | |
{ | |
$this->data['HtmlBody'] = $html; | |
return $this; | |
} | |
function plain_message($msg) | |
{ | |
$this->data['TextBody'] = $msg; | |
return $this; | |
} | |
function tag($tag) | |
{ | |
$this->data['Tag'] = $tag; | |
return $this; | |
} | |
function attachment($name, $content, $content_type) | |
{ | |
$this->data['Attachments'][$this->attachment_count]['Name'] = $name; | |
$this->data['Attachments'][$this->attachment_count]['ContentType'] = $content_type; | |
// Check if our content is already base64 encoded or not | |
if( ! base64_decode($content, true)) | |
$this->data['Attachments'][$this->attachment_count]['Content'] = base64_encode($content); | |
else | |
$this->data['Attachments'][$this->attachment_count]['Content'] = $content; | |
// Up our attachment counter | |
$this->attachment_count++; | |
return $this; | |
} | |
} |
Yep, I'm using it in production. Are you always passing the 'to' field as an array of email addresses?
I don't know what i did, but now it's working, thanks, anyway.
On Thu, Apr 26, 2012 at 3:35 PM, Ben Werdmuller < ***@***.*** > wrote:
Yep, I'm using it in production. Are you always passing the 'to' field as
an array of email addresses?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2370358
##
Atenciosamente,
_Djalma Araújo_
Diretor de Tecnologia
www.pianolab.com.br - _81 3427.3660_
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man, this batch works?
I'm trying but is not working..