PostageApp is the simplest way of sending mass emails and setting it up with Php can be done in a matter of minutes. We think PostageApp is a great app that will make your life easier. We use it in our own apps which mean we keep improving it all the time and you get to benefit from it.
If you’re already have an application that sends emails using the Php mail function you’ll find that you get to keep much of the code you already have which is great. So let’s start!
1 – Begin by creating an account and a project if you haven’t done that already. Each project has it’s own API key which will be used to communicate with PostageApp.
2 – Create a file with some configuration options
postageapp_config.inc
1 2 3 4 5 |
Grab the API key from you PostageApp project and place it on this file.
3 – Opening a connection to PostageApp.com
To access PostageApp API we’ll use the Php cURL library.
1 2 3 4 5 6 7 8 9 | $ch = curl_init(POSTAGE_HOSTNAME.'/v.1.0/'.$api_method.'.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $output = curl_exec($ch); curl_close($ch); |
On line 1 we’re opening a connection to the PostageApp API and calling a specific $api_method that we’ll define later. In line 2 we’re sending some $content that we’re defining as JSON in line 5.
The $content variable is where we’ll put the data we want to send to PostageApp. So to make things clean and easy we’ll create a file where we can aggregate this code nicely.
postageapp_class.inc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php // Replace 'postageapp_config.inc' with the name of the PostageApp // config file you have created require_once('postageapp_config.inc'); class PostageApp { // Sends a message to Postage App function mail($recipient, $subject, $mail_body, $header, $variables=NULL) { $content = array( 'recipients' => $recipient, 'headers' => array_merge($header, array('Subject' => $subject)), 'variables' => $variables, 'uid' => time() ); if (is_string($mail_body)) { $content['template'] = $mail_body; } else { $content['content'] = $mail_body; } return PostageApp::post( 'send_message', json_encode( array( 'api_key' => POSTAGE_API_KEY, 'arguments' => $content ) ) ); } // Makes a call to the Postage App API function post($api_method, $content) { $ch = curl_init(POSTAGE_HOSTNAME.'/v.1.0/'.$api_method.'.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $output = curl_exec($ch); curl_close($ch); return json_decode($output); } } ?> |
In this file we begin by including our configuration file. Next we declare a class that will help us abstract the API calls to PostageApp by defining two functions: mail and post.
The post function contains the cURL call shown previously. Notice that it returns the result of the call to PostageApp. This will be usefull to debug our code. We won’t need to call this function directly from outside this class. Instead we’ll use our own PostageApp::mail function.
The mail function is responsible for organizing the content we want to send to PostageApp. It’s very similar to the Php mail function so you don’t have to change things too much if you already have some working code.
So here’s an example on how to call the PostageApp mail function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php require_once('postageapp_class.inc'); $to = 'me@test.com'; # The subject of the message $subject = 'Postage App test email'; # Setup some headers $header = array( 'From' => 'my_test@somewhere.com', 'Reply-to' => 'my_test@somewhere.com' ); # The body of the message $mail_body = array( 'text/plain' => 'Hello world in plain text', 'text/html' => '<h1>Hello world</h1><p>in <b>HTML</b></p>' ); # Send it all $ret = PostageApp::mail($to, $subject, $mail_body, $header); # Checkout the response if ($ret->response->status == 'ok') { echo '<br/><b>SUCCESS:</b>, An email was sent and the following response was received:'; } else { echo '<br/><b>Error sending your email:</b> '.$ret->response->message; } echo '<pre style="text-align:left;">'; print_r ($ret); echo '</pre>'; ?> |
For more details checkout the documentation at http://help.postageapp.com/faqs/api/send_message
Tags: Bulk Mailing, mass mailing, Php
