Originally published: July 16 2010

Postmark CakePHP Component

I recently discovered the excellent email delivery service Postmark. We had been running into some trouble sending email from one of our web apps. The SMTP server on our Dreamhost hosting account has a limit of 100 messages per hour. Additionally, many of our messages were bouncing and we had poor visibility into our delivery statistics.

There are some existing PHP libraries for Postmark available, but none that integrated elegantly with our CakePHP based application. So I took a few minutes and whipped up a simple Postmark CakePHP component which I’ve made available on Github.

Configuration

Add the following keys to your configuration:

Configure::write('Postmark.uri', 'http://api.postmarkapp.com/email');
Configure::write('Postmark.key', '810de23c-5ffb-44d9-a424-7a4a5c0fba1a');

If you want your connection to Postmark to be encrypted, simply change the uri to use https.

Make sure to modified the API key to match the credentials for your Postmark server rack instance.

Usage

This component extends the base CakePHP email component, and works virtually the same.

Place the postmark.php file in your app/controllers/components/ folder.

In your controller, make sure the component is available:

public $components = array('Postmark');

Then, simply send messages like this:

$this->Postmark->delivery = 'postmark';
$this->Postmark->from = 'sender@domain.com';
$this->Postmark->to = 'recipient@domain.com';
$this->Postmark->subject = 'this is the subject';
$messageBody = 'this is the message body';
$this->Postmark->send($messageBody);

You can also use the following optional attributes:

$this->Postmark->cc = array('recipient@domain.com');
$this->Postmark->bcc = array('recipient@domain.com');
$this->Postmark->replyTo = 'sender@domain.com';

The syntax of all parameters is the same as the default CakePHP email component.

There is one additional attribute which can be used for setting the Postmark tag property:

$this->Postmark->tag = 'contact';

Debugging

You can see the response from Postmark in the return value when you send a message:

$result = $this->Postmark->send($messageBody);
$this->log($result, 'debug');

If there are any errors, they’ll be included in the response. See the Postmark API documentation for error code detail.

Conclusion

Hopefully others will find this useful. If you have any questions, problems, or suggestions, please let me know! Here’s a link to the code: Postmark CakePHP Component on GitHub.