Random Linux Garble.

Using Postmark API With Kohana 3

Postmark gives API access to their service, which rocks. People have been kind enought to develop libraries to access the API as well. There is a lot of info out there on different implementations of the API, but none that I could find specifically for Kohana 3. So I threw together a little post for it.

I had to work with Postmark and Kohana 3 this past week. There is no library for Kohana 3 alone, but there is a nifty one for php. Ill run through an example of how to implement that library into a Kohana 3 application.

So first we have to import the library into our application. First create a directory in called “vendors”, so “application/vendors”. Create another directory inside vendors for postmarkapp.

Download postmark php library from Github and place those files in the application/vendors/postmarkapp directory.

Also, if you haven’t signed up with postmark, you will want to do that.

Now that thats in place, next step is to actually tell Kohana that we are using that library, and where to find it.

In the application/bootstrap.php add this line above the section that starts with “//– Configuration and initialization”

//import postmarkapp api  
$path = Kohana::find_file('vendors', 'postmarkapp/Postmark');  
if($path) {  
    [ini_get][2]('include_path') . PATH_SEPARATOR . [dirname][3]([dirname][3]($path)));  
    require_once 'postmarkapp/Postmark.php';  

Awesome, now its ready to start using it. In whatever controller you want to use it in (I’m going to do it in my controller/main.php file) create a function to send mail.

I am setting up some constants for postmark like From Name and From Email, as well as including the API key I got from Postmark.

Now you have the function setup that does the heavy lifting for you. Typically you would call this function to send a confirmation or a submitted form. Below is a very simplified example. If nothing is posted yet, it loads the form which is in views/contact/form. If the form has posted, then it processes the data. Keep in mind I have done very little error handling and data filtering. Lots of good info on the unofficial wiki about that.