2017 © Pedro Peláez
 

module sendgrid-webhook

ZF2 Module to receive Webhooks sent by SendGrid

image

netglue/sendgrid-webhook

ZF2 Module to receive Webhooks sent by SendGrid

  • Wednesday, June 27, 2018
  • by netglue
  • Repository
  • 1 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 36 % Grown

The README.md

ZF2 Module: Send Grid Events Webhook

A simple module for a ZF2 app that will receive event webhooks from SendGrid and trigger a ZF2 event for each of the batched events received. You can then write your own listeners to do whatever you need to do with bounce processing etc., (*1)

Install

Install with composer using "netglue/sendgrid-webhook", enable the module in your application.config.php using the module name 'NetglueSendgrid' and add custom configuration to change the route url perhaps or set up Basic HTTP Auth (Recommended)., (*2)

Test

cd to wherever the module is installed, issue a composer install followed by a phpunit., (*3)

Setup Basic Auth

Create a configuration file in your autoload directory named something like sendgrid.local.php and enter values for username and password as outlined in ./config/module.config.php., (*4)

Set an Alternate Webhook Endpoint/URL

Somewhere in your configuration files where you like to look after your routes, add a new one with configuration similar to this:, (*5)

'router' => [
    'routes' => [
        'sendgrid-webhook' => [
            'options' => [
                'route' => '/somewhere/this-is-your-endpoint',
            ]
        ]
    ]
]

Setup SendGrid

Navigate to your account at SendGrid and access the Settings -> Mail Settings section. Click the Event Notification item and enter the url. By default, the URL should be something like: https://yourdomain.com/netglue-sendgrid-events., (*6)

Effectively, nothing will happen, but you should start to see something in your web server logs. You'll need to write some listeners to do something with the events triggered., (*7)

Write A Listener

For example, in your main app's Module.php you could setup a listener to log the events with something like this:, (*8)

public function onBoostrap(EventInterface $e)
{
    // Get a logger instance - change this…
    $services = $e->getApplication()->getServiceManager();
    $logger = $services->get('TheNameOfMyLogger');

    // Listen to all events sent by the EventEmitter
    $em = $e->getApplication()->getEventManager();
    $sharedEm = $em->getSharedManager();
    $sharedEm->attach(
        'NetglueSendgrid\Service\EventEmitter',
        '*',
        function($event) use ($logger) {
            $params = $event->getParams();
            $logger->debug(sprintf(
                'Send Grid Event "%s" was triggered with the event name %s',
                $params['data']['event'],
                $event->getName()
            ));
        }
    );
}

Changelog

1.0.0

Initial release, (*9)

The Versions

26/05 2016

1.0.0

1.0.0.0

ZF2 Module to receive Webhooks sent by SendGrid

  Sources   Download

MIT

The Requires

 

The Development Requires