Unfortunately we decided to not maintain this project anymore (see why). If you want to mark another package as a replacement for this one please send an email to hello@knplabs.com.
mailjet-api-php
is a PHP library for quick and simple consuming of Mailjet API.
It supports both RESTful and Event Tracking APIs.
This library provides OOP wrappers to most (all) API endpoints, that are located under Mailjet/Api/Request
namespace. These include:
Simple example:
<?php
// This file is generated by Composer
require_once 'vendor/autoload.php';
use Mailjet/Api/Client;
use Mailjet/Api/Request/User;
$user = new User(new Client('api_key', 'secret_key'));
$userInfo = $user->getInfo();
var_dump($userInfo);
//(
// [username] => KnpLabs
// [email] => hello@Knplabs.com
// [locale] => en_US
// [currency] => USD
// [timezone] => America/New_York
// [firstname] => KnpLabs
// ....
//)
In addition to using wrappers, you can obviously also use the client directly to make API requests on lower level. To ease consumption of RESTful API there's a RequestApi helper class, which lists all available queries. Check Mailjet documentation for a detailed list of queries.
Simple example:
<?php
// This file is generated by Composer
require_once 'vendor/autoload.php';
use Mailjet/Api/Client;
use Mailjet/Api/RequestApi;
$client = new Client('api_key', 'secret_key');
$userInfo = $client->get(RequestApi::USER_INFOS);
var_dump($userInfo);
//(
// [username] => KnpLabs
// [email] => hello@Knplabs.com
// [locale] => en_US
// [currency] => USD
// [timezone] => America/New_York
// [firstname] => KnpLabs
// [lastname] =>
// [company_name] => KnpLabs
// [contact_phone] =>
// [address_street] =>
// [address_postal_code] =>
// [address_city] =>
// [address_country] =>
// [tracking_openers] => 1
// [tracking_clicks] => 1
//)
Sending a POST request:
<?php
// ... initialize client
$result = $client->post(RequestApi::USER_DOMAIN_ADD, array(
'domain' => 'example.com'
));
var_dump($result);
//(
// [status] => 200
// [file_name] => empty_file
// [id] => 123
//)
mailjet-api-php
provides a clean OOP interface to interact with Event Tracking API.
<?php
use Mailjet/Event/EventFactory;
$factory = new EventFactory();
// fetch incoming data into $data array, example
//(
// [event] => open
// [email] => hello@Knplabs.com
// [mj_contact_id] => 123
// [mj_campaign_id] => 123
// [customcampaign] => Hello from KnpLabs
// [ip] => 127.0.0.1
// [geo] => US
// [agent] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0
//)
$event = $factory->createEvent($data);
echo get_class($event); // \Mailjet\Event\Events\OpenEvent
echo $event->getType(); // open
echo $event->getIp(); // 127.0.0.1
Note: this library is not responsible for validation of incoming data. However, assuming the data is coming from Mailjet servers, it will work correctly.
You don't need a special bundle to use the RESTful API with Symfony2, you can initialize the service with a simple config:
services:
knp_mailjet.api:
class: Mailjet\Api\Client
arguments: [<your_mailjet_api_key>, <your_mailjet_secret_key>]
And that's it, Mailjet RESTful API is now available via:
<?php
$this->container->get('knp_mailjet.api');
And to initialize higher level OOP wrappers:
services:
knp_mailjet.api.request.user:
class: Mailjet\Api\Request\User
arguments: [@knp_mailjet.api]
# ... other wrappers definitions
<?php
$this->container->get('knp_mailjet.api.request.user')->getInfo();
However, if you need both RESTful and Event API support, then there's KnpMailjetBundle.
The first step to use mailjet-api-php
is to download Composer:
$ curl -s http://getcomposer.org/installer | php
Now add mailjet-api-php
with Composer:
$ php composer.phar require knplabs/mailjet-api-php:1.*
And that's it! Composer will automatically handle the rest.
Alternatively, you can manually add the dependency to composer.json
file...
{
"require": {
"knplabs/mailjet-api-php": "1.*"
}
}
... and then install our dependencies using:
$ php composer.phar install
- PHP >= 5.3.8
- HTTP component of Guzzle library
- (optional) Symfony2 Debug Component
See CONTRIBUTING.md file.
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:
phpunit
OOP wrappers idea was originally implemented by David Guyon in his version of the client.
mailjet-api-php is released under the MIT License. See the bundled LICENSE file for details.