PHPackages                             sparkpost/sparkpost - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. sparkpost/sparkpost

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

sparkpost/sparkpost
===================

Client library for interfacing with the SparkPost API.

2.3.0(5y ago)1914.0M—6.3%92[10 issues](https://github.com/SparkPost/php-sparkpost/issues)[4 PRs](https://github.com/SparkPost/php-sparkpost/pulls)17MITPHPPHP ^7.1 || ^8.0

Since Nov 10Pushed 2y ago58 watchersCompare

[ Source](https://github.com/SparkPost/php-sparkpost)[ Packagist](https://packagist.org/packages/sparkpost/sparkpost)[ RSS](/packages/sparkpost-sparkpost/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (18)Used By (17)

[![](https://camo.githubusercontent.com/d5c6bc65e534d09294717749ebfd210ea19c32804678375cfd92327fd5e9445e/68747470733a2f2f7777772e737061726b706f73742e636f6d2f73697465732f64656661756c742f66696c65732f6174746163686d656e74732f537061726b506f73745f4c6f676f5f322d436f6c6f725f477261792d4f72616e67655f5247422e737667)](https://www.sparkpost.com)

[Sign up](https://app.sparkpost.com/join?plan=free-0817?src=Social%20Media&sfdcid=70160000000pqBb&pc=GitHubSignUp&utm_source=github&utm_medium=social-media&utm_campaign=github&utm_content=sign-up) for a SparkPost account and visit our [Developer Hub](https://developers.sparkpost.com) for even more content.

SparkPost PHP Library
=====================

[](#sparkpost-php-library)

[![Travis CI](https://camo.githubusercontent.com/f4936387fcd008dac0396602a9d32b2214863b3147726238f1b572a5a2bf261c/68747470733a2f2f7472617669732d63692e6f72672f537061726b506f73742f7068702d737061726b706f73742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/SparkPost/php-sparkpost)[![Coverage Status](https://camo.githubusercontent.com/9a1af33f4857bd9a221bd6c70ac7811a06c75bb8d7a07c1d01cabee0b4387474/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f537061726b506f73742f7068702d737061726b706f73742f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/SparkPost/php-sparkpost?branch=master)[![Downloads](https://camo.githubusercontent.com/ce4e797c2e741fdb1231145e375526ac0fab52534d972b3e352a7568e296957e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f737061726b706f73742f737061726b706f73742e7376673f6d61784167653d33363030)](https://packagist.org/packages/sparkpost/sparkpost)[![Packagist](https://camo.githubusercontent.com/284f39dbfa9b0c2e59a289c393615b21cb32073cedd614489b298513b362c78d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737061726b706f73742f737061726b706f73742e7376673f6d61784167653d33363030)](https://packagist.org/packages/sparkpost/sparkpost)

The official PHP library for using [the SparkPost REST API](https://developers.sparkpost.com/api/).

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
------------

[](#installation)

**Please note: The composer package `sparkpost/php-sparkpost` has been changed to `sparkpost/sparkpost` starting with version 2.0.**

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

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

```

Sparkpost requires php-http client (see [Setting up a Request Adapter](#setting-up-a-request-adapter)). There are several [providers](https://packagist.org/providers/php-http/client-implementation) available. If you were using guzzle6 your install might look like this.

```
composer require php-http/guzzle6-adapter "^1.1"
composer require guzzlehttp/guzzle "^6.0"

```

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

```
composer require sparkpost/sparkpost

```

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

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

**Note:** Without composer the costs outweigh the benefits of using the PHP client library. A simple function like the one in [issue #164](https://github.com/SparkPost/php-sparkpost/issues/164#issuecomment-289888237) wraps the SparkPost API and makes it easy to use the API without resolving the composer dependencies.

Running with IDEs
-----------------

[](#running-with-ides)

When running with `xdebug` under an IDE such as VS Code, you may see an exception is thrown in file `vendor/php-http/discovery/src/Strategy/PuliBetaStrategy.php`:

```
Exception has occurred.
Http\Discovery\Exception\PuliUnavailableException: Puli Factory is not available

```

[This is usual](http://docs.php-http.org/en/latest/discovery.html#puli-factory-is-not-available). Puli is not required to use the library. You can resume running after the exception.

You can prevent the exception, by setting the discovery strategies, prior to creating the adapter object:

```
// Prevent annoying "Puli exception" during work with xdebug / IDE
// See https://github.com/getsentry/sentry-php/issues/801
\Http\Discovery\ClassDiscovery::setStrategies([
        // \Http\Discovery\Strategy\PuliBetaStrategy::class, // Deliberately disabled
        \Http\Discovery\Strategy\CommonClassesStrategy::class,
        \Http\Discovery\Strategy\CommonPsr17ClassesStrategy::class,
]);
```

Setting up a Request Adapter
----------------------------

[](#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 [HTTPlug](https://github.com/php-http/httplug) in SparkPost. Please visit their repo for a list of supported [clients and adapters](http://docs.php-http.org/en/latest/clients.html). If you don't currently use a request library, you will need to require one and create a client from it and pass it along. The example below uses the GuzzleHttp Client Library.

A Client can be setup like so:

```
