Symfony2 Bundle for initializing Mailjet PHP API wrapper and handling Event API callbacks

Installs: 2 076

Dependents: 0

Stars: 7

Watchers: 27

Forks: 1

Open Issues: 1

Language: PHP

v1.0.0 2013-10-25 11:27 UTC


KnpMailjetBundle is a Symfony2 Bundle, mainly aimed at processing Mailjet Event Tracking API, but also provides service initialization for mailjet-api-php library.

Build Status SensioLabsInsight Scrutinizer Quality Score Latest Stable Version Composer Downloads


RESTful API as a service

Add api/secret keys in config.yml and it's up!

# app/config.yml
    api_key:    %mailjet_api_key%
    secret_key: %mailjet_secret_key%

Now you can access RESTful API via DIC by calling knp_mailjet.api service:


$client = $this->container->get('knp_mailjet.api');
$userInfo = $client->get(RequestApi::USER_INFOS);


//    [username] => KnpLabs
//    [email] =>
//    ...

Event Tracking Listener

KnpMailjetBundle handles Event Tracking API via EventListener service.

Configuring Mailjet

First, you need to provide Mailjet with a specific endpoint URL where all event callbacks will be sent. To do this, go to Event Tracking admin panel:

KnpMailjetBundle provides a helper command that you can use to quickly dump current endpoint URL:

$ php app/console mailjet:event-endpoint
Configuring Event Listener

To actually handle events, you need to create your own Event Listener class by implementing provided interface Knp\Bundle\MailjetBundle\Event\Listener\EventListenerInterface:


namespace Acme\DemoBundle\Listener;

use Knp\Bundle\MailjetBundle\Event\Listener\EventListenerInterface;

use Knp\Bundle\MailjetBundle\Event\Adapter\BlockedEvent;
use Knp\Bundle\MailjetBundle\Event\Adapter\BounceEvent;
// ...

class EventListener implements EventListenerInterface
    public function onOpenEvent(OpenEvent $event)
        // handle open events here

    public function onSpamEvent(SpamEvent $event)
        // handle close events here

    // ...

Now you need to configure it in DIC, but be sure to specify the tags you want to listen to:

        class:  Acme\DemoBundle\Listener\EventListener
            - { name: kernel.event_listener, event:,    method: onOpenEvent }
            - { name: kernel.event_listener, event:  knp_mailjet.blocked, method: onBlockedEvent }
            - { name: kernel.event_listener, event:  knp_mailjet.bounce,  method: onBounceEvent }
            - { name: kernel.event_listener, event:,   method: onClickEvent }
            - { name: kernel.event_listener, event:  knp_mailjet.spam,    method: onSpamEvent }
            - { name: kernel.event_listener, event:  knp_mailjet.typofix, method: onTypofixEvent }
            - { name: kernel.event_listener, event:  knp_mailjet.unsub,   method: onUnsubEvent }
        arguments: []

And that's it, your endpoint is ready for Event Tracking API consumption!

If you don't know where to start with Event Listener implementation, take a look at the demo listener and its configuration, which simply logs the events.

Securing Endpoint URL

It's a good idea to secure your endpoint URL with a special token that only you and Mailjet servers will know. That way you will avoid people abusing it should they discover.

With KnpMailjetBundle it's really easy - just specify your desired token in config.yml:

# app/config.yml
    event_endpoint_token: 123token

And now if you run the helper command you will see the secured URL:

$ php app/console mailjet:event-endpoint

Don't forget to update Mailjet admin panel with your new endpoint URL!


The first step to use KnpMailjetBundle is to download Composer:

$ curl -s | php

Now add KnpMailjetBundle with Composer:

$ php composer.phar require knplabs/knp-mailjet-bundle:1.*

And that's it! Composer will automatically handle the rest.

Alternatively, you can manually add the dependency to composer.json file...

    "require": {
        "knplabs/knp-mailjet-bundle": "1.*"

... and then install our dependencies using:

$ php composer.phar install

After that, you need to update your app/AppKernel.php file:

// app/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
    public function registerBundles()
        $bundles = array(
            // ...
            new Knp\Bundle\MailjetBundle\KnpMailjetBundle(),

For Event Tracking API you also need to import routing:

# app/routing.yml

    resource: "@KnpMailjetBundle/Resources/config/routing.yml"

And that's it!


  • PHP >= 5.3.8
  • knplabs/mailjet-api-php


See file.

Running the Tests

To run unit tests, you'll need a set of dev dependencies you can install using Composer:

php composer.phar install --dev

Once installed, just launch the following command:



Sponsored by

KnpLabs Team


KnpMailjetBundle is released under the MIT License. See the bundled LICENSE file for details.