Skip to content

iangregory/php-sparkpost

 
 

Repository files navigation

Sign up for a SparkPost account and visit our Developer Hub for even more content.

SparkPost PHP Library

Travis CI Coverage Status Slack Status

The official PHP library for using the SparkPost REST API.

Note: We understand that the ivory-http-adapter we use in this library is deprecated in favor of httplug. We use Ivory internally to make it simple for you to use whatever HTTP library you want. The deprecation won't affect or limit our ongoing support of this PHP library.

Before using this library, you must have a valid API Key. To get an API Key, please log in to your SparkPost account and generate one in the Settings page.

Installation

The recommended way to install the SparkPost PHP Library is through composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the SparkPost PHP Library:

composer require sparkpost/php-sparkpost

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';
use SparkPost\SparkPost;

Setting up a Request Adapter

Because of dependency collision, we have opted to use a request adapter rather than requiring a request library. This means that your application will need to pass in a request adapter to the constructor of the SparkPost Library. We use the [Ivory HTTP Adapter] (https://github.com/egeloen/ivory-http-adapter) in SparkPost. Please visit their repo for a list of supported adapters. If you don't currently use a request library, you will need to require one and create an adapter from it and pass it along. The example below uses the GuzzleHttp Client Library.

An Adapter can be setup like so:

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;

$httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']);

Getting Started: Your First Mailing

For this example to work as is, Guzzle 6 will need to be installed. Otherwise another adapter can be used for your specific setup. See "Setting up a Request Adapter" above.

require 'vendor/autoload.php';

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;

$httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']);

try {
    // Build your email and send it!
    $results = $sparky->transmission->