For web developers

PostageApp Blog

Mass mailing – quick start for Php

November 9th, 2009 by jack  |  5 Comments

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
<?php
  # Setup Postage configuration
 if(!defined('POSTAGE_HOSTNAME')) define ('POSTAGE_HOSTNAME', 'https://api.postageapp.com');
  if(!defined('POSTAGE_API_KEY')) define ('POSTAGE_API_KEY', 'ENTER YOUR API KEY HERE');
?>

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: , ,

  • Nayan Paul

    Hi Sir Thanks for share.
    I have installed and setup a new SMTP server.all is good,whem i test a thru php mail function,But when i use PHPMailer Class then it doesnot wokrs

    when i use simple php mail function then it looks

    $sent = PostageApp::mail($to, $subject, $mail_body, $header);

    When i use phpmailer class it looks(i don’t know is it wrong or ok)

    $sent = PostageApp::sendMail($toMail, $toName, $subject, $message, $fromMail, $fromName, $html=1);

    My Question is it works on phpmailclass ?

    Please help me….

  • Nayan Paul

    Sir Please look these script

    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
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    <?php
    // Display the Errore even php.ini error show is off. //
    //ini_set("display_errors","on");
    //error_reporting(E_ALL);
    require_once(&#039;postageapp_class.php&#039;);
    $var  = &#039;&#039;;
    //include(&#039;mail/mail.php&#039;);
    if(isset($_POST[&#039;Submit&#039;]))
    {


      $message = "Hello Administrator, Quotation Request  From ".$_POST[&#039;Name&#039;]."&nbsp;Submitted  at&nbsp;".date(&#039;d-M-Y h:i:s a&#039;);
     
      $message.="";
     
      $message.="";
      $message .="";
      $message .="Client Information Details";
      $message .="Booking Date :".$_REQUEST['BookingDate']."";
      $message .="Client Name :" . $_REQUEST['Name']. "";
      $message .="Address :" .$_REQUEST['Address']. "";
      $message .="Tel/Fax :" .$_REQUEST['TelFax']. "";
      $message .="Mobile No:" .$_REQUEST['Mobile']. "";
      $message .="Email Address :" .$_REQUEST['Email']. "";
     
      // for Radio Button //
        if($_REQUEST['Surprise']!="")
      {
        $message .="Surprise booking :" .$_POST['Surprise']. "";
      }
      // For Checkbox //
      if(is_array($_REQUEST['vehicle']))
      {
        foreach($_REQUEST['vehicle'] as $val)
        {
          $var .=$val.'&nbsp;,';
        }
      }
    $message .="Vehicle(s) :".$var. "";
     
      $message .="Total No of Passengers :" .$_REQUEST['Passengers']. "";
      $message .="Hire Details  :" .$_REQUEST['HireDetails']. "";
      $message .="Purpose of Hire :" .$_REQUEST['HirePurpose']. "";
      $message .="Other specified :" .$_REQUEST['Specify_other']. "";
      $message .="Special Requests :" .$_REQUEST['SpecialRequests']. "";
      $message .="Additional Information :" .$_REQUEST['AdditionalInfo']. "";
      $message .="Hear Sophisticars from :" .$_REQUEST['HowYouFoundUs']. "";
      $message .="Other Specified :" .$_REQUEST['SpecifyOther']. "";

      $message .="";
     
     
      $toMail= 'nayanmailbox@gmail.com'; // This Need to be changed.
      $subject = "Quotation Request about Sophisticars.";
      $toName = 'Admin';
      $fromMail = $_REQUEST['Email'];
      $fromName = $_REQUEST['Name'];
      //echo ('To : '.$toMail.' '.$toName.'From :'.$fromMail.' '.$fromName.' Subject : '.$subject.'Message :'.$message);
      //$sent = sendMail($toMail, $toName, $subject, $message, $fromMail, $fromName, $html=1);
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
      $headers .= 'To: Mary , Kelly ' . "\r\n";
      $headers .= 'From: Birthday Reminder ' . "\r\n";
      $headers .= 'Cc:ccccc@gmail.com' . "\r\n";
      $headers .= 'Bcc: ff@yahoo.com' . "\r\n";

      $ret = PostageApp::mail($toMail, $subject, $message,$headers);
      header("location:thanks.html");
    }
      # Checkout the response
    if ($ret->response->status == 'ok') {
       echo '<b>SUCCESS:</b>, An email was sent and the following response was received:';
      } else {
       echo '<b>Error sending your email:</b> '.$ret->response->message;
      }
      echo '';
      print_r ($ret);
      echo '';
    ?>
  • http://www.theworkinggroup.ca jack

    Hi Nayan, looking at your code I can see at least two differences in relation to the examples provided: the headers and the content of the message should be defined as an array, not as a string. You are defining them as if you were using PHPMailer.

    You can find a working Php example app at http://blog.postageapp.com/2009/11/php-example-app/

    Use that as a starting point.

  • Nayan Paul

    Sir Thanks for looking and reply.

    So if use PHPMailer then what sholud look your code ? is this ?

    $sent = PostageApp::sendMail($toMail, $toName, $subject, $message, $fromMail, $fromName, $html=1);

    but it is not working

    As a fresher i can’t understand.Sir Please put some code.

    Many Thanks again

  • http://www.theworkinggroup.ca jack

    Hi Nayan,

    You should use either PHPMailer OR the PostageApp::mail function. Here are the differences:
    1 – PHPMailer will contact your SMTP server directly; Your emails will be sent one by one.
    2 – PostageApp::mail function will contact PostageApp which will then contact your SMPT server. This allows you to send mass emails, reports on the emails sent, email templates and other tools.

    I hope this clarifies it for you.