Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Simple Postmark PHP Class with Attachments
<?php
require("postmark.php");
$postmark = new Postmark("your-api-key","from-email","optional-reply-to-address");
$result = $postmark->to("reciver@example.com")
->subject("Email Subject")
->plain_message("This is a plain text message.")
->attachment('File.pdf', base64_encode(file_get_contents('sample.pdf')), 'application/pdf')
->send();
if($result === true)
echo "Message sent";
<?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 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;
}
}
@wrecck

This comment has been minimized.

Copy link

commented Feb 13, 2012

Hi Matthew, thanks for this great class, but do you mind if I ask you question? Anyway, I'lll proceed, when I attached PDF to send email it seems its sending corrupted file, somehow that the PDF being sent is ending up just about 1kb where it was supposed to be around 40k or something. I hope you can help me on this one. Thanks.

@vimokumar

This comment has been minimized.

Copy link

commented Aug 21, 2012

Good one !

@hgus

This comment has been minimized.

Copy link

commented Aug 29, 2012

Hi,

I don't understand, it doesn't work at all !

I've well uploaded postmark.php, put my api key, comment the attachment line (to simplify), and nothing happens !?...

Does I forgot something ?

Thanks for your precious help,

Ugo

@hgus

This comment has been minimized.

Copy link

commented Aug 29, 2012

and of course fill in needed information (from-email, ...)

curl is enabled in my php.ini ... is it sufficient ?

@horninc

This comment has been minimized.

Copy link

commented Jul 17, 2013

In case it complains about missing curl, you can get it by "apt-get install php5-curl". On non-Debian systems you can use the same package name with "yum" or "aptitude".

@pankajpanday

This comment has been minimized.

Copy link

commented Dec 19, 2013

Use this to send pdf
$result = $postmark->to("send-to-email")
->subject("Test Mail")
->plain_message("This is a plain text message.")
->attachment('sample.pdf', base64_encode(file_get_contents('sample.pdf')), 'application/pdf')
->send();

@netswagger

This comment has been minimized.

Copy link

commented Dec 30, 2013

I'm having the same issues as hgus. It's like $result is never === true. Is there some other setting I'm missing? I'm running php 5.4.4

@robstarbuck

This comment has been minimized.

Copy link

commented Jan 14, 2014

The API is sound, you need to set up a signature in Postmark the from address that you're using in the API will need to be the same as that signature. That should get it working.

https://postmarkapp.com/signatures

@nikhilwason

This comment has been minimized.

Copy link

commented Mar 4, 2014

Nice! so far so good. Thanks!

@benrodigas

This comment has been minimized.

Copy link

commented Apr 26, 2014

Thanks a lot for this code. Much appreciated!

@revalderc

This comment has been minimized.

Copy link

commented Jun 6, 2014

How should I send html email? Please help. Thanks!

@cyx-pravin

This comment has been minimized.

Copy link

commented Oct 8, 2018

anyone knows how to download attachments from Json response in django

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.