CodeIgniter and Postmark Integration

832 views Asked by At

I am very new to codeigniter and postmark, is there a way on how to integrate the two?

I created a controller called cron.php and put the postmark code inside the method inbound, here's my code:

public function inbound()
{   
  try {
        require_once '../lib/Postmark/Autoloader.php';
        \Postmark\Autoloader::register();

        // this file should be the target of the callback you set in your postmark account
        $inbound = new \Postmark\Inbound(file_get_contents('php://input'));


        $this->data['client'] = $this->client_model->get_where($m_where);
        if(!empty($this->data['client']))
        {
            $m_insert = array('cme_username' => $inbound->FromName(), 
                              'cme_title' => $inbound->Subject(),
                              'cme_message' => $inbound->TextBody(),
                              'cme_client' => $this->data['client']['ccl_id'],
                              'cme_from' => 'client',
                              'cme_through' => 'email',
                              'cme_date_sent' => date('Y-m-d H:i:s'),
                              'cme_admin' => 0);
            $this->message_model->insert($m_insert);
        }
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }}

I'm also wondering if I am doing it correctly? Many thanks!

1

There are 1 answers

1
macinnir On

You can simply pull one of the example classes from the PostMarkApp.com website (I used this one and renamed it simply postmark) in the application/libraries/ directory and use it like any other CI Library. Make sure you have the proper globals defined (I defined them in application/config/config.php).

application/config/config.php

define('POSTMARKAPP_API_KEY', 'YOUR-POSTMARKAPP-API-KEY');    
define('POSTMARKAPP_MAIL_FROM_ADDRESS', '[email protected]');

application/controllers/somecontroller.php

class somecontroller extends CI_Controller { 

  function someControllerMethod(){


    $this->load->library('postmark');

    $this->postmark->addTo('[email protected]', 'John Doe')
                  ->subject('My email subject')
                  ->messagePlain('Here is a new message to John!')
                  ->tag('some-tag')
                  ->send();

  }

}

Hope that helps!