PHPackages                             djagya/yii2-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. djagya/yii2-sparkpost

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

djagya/yii2-sparkpost
=====================

A library provides Yii2 integration with SparkPost mail service

0.4.2(9y ago)1816.3k9[3 issues](https://github.com/djagya/yii2-sparkpost/issues)MITPHPPHP &gt;=5.5

Since Feb 28Pushed 8y ago2 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (13)Used By (0)

SparkPost API Yii2 Mailer
=========================

[](#sparkpost-api-yii2-mailer)

SparkPost API Mailer for Yii2
[![Latest Stable Version](https://camo.githubusercontent.com/8c845872280eaf208e03e865618cf1becb2e76dd2b3f2695c86d0afa65d5da5d/68747470733a2f2f706f7365722e707567782e6f72672f646a616779612f796969322d737061726b706f73742f762f737461626c65)](https://packagist.org/packages/djagya/yii2-sparkpost) [![Build Status](https://camo.githubusercontent.com/df03eaa933804dc144b4570f7e71396ed41c6998a029ab54009d7037a576d143/68747470733a2f2f7472617669732d63692e6f72672f646a616779612f796969322d737061726b706f73742e737667)](https://travis-ci.org/djagya/yii2-sparkpost) [![Total Downloads](https://camo.githubusercontent.com/d457a6ac8d2c1f4168d7b3fd8189fe8f5eb17271d437628ea65834286110d333/68747470733a2f2f706f7365722e707567782e6f72672f646a616779612f796969322d737061726b706f73742f646f776e6c6f616473)](https://packagist.org/packages/djagya/yii2-sparkpost) [![License](https://camo.githubusercontent.com/43fbf350acbc4dbbb40a1358338ed317209d66c5e40e95cce6874eadb3527a43/68747470733a2f2f706f7365722e707567782e6f72672f646a616779612f796969322d737061726b706f73742f6c6963656e7365)](https://packagist.org/packages/djagya/yii2-sparkpost)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist djagya/yii2-sparkpost "*"

```

or add

```
"djagya/yii2-sparkpost": "*"

```

to the require section of your `composer.json` file.

Set Up
------

[](#set-up)

To use SparkPost you will need to have a [SparkPost Account](https://www.sparkpost.com/).

They have a free Developer account that includes up to 15K messages/month, as well as a sandbox for ongoing development and testing with the same built-in features and performance as their paid plans, with no time limitation.

Once you have an account you will need to create an **API Key**.
You can create as many API keys as you want, and it's best practice to create one for each website.

For testing purposes, while you're waiting for domain verification, you can use a sandbox mode, which can be enabled in the extension's config.

Usage
-----

[](#usage)

To use this extension, add the following code in your application configuration (default cUrl http adapter will be used):

```
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'viewPath' => '@common/mail',
            'defaultEmail' => 'sender@example.com', // optional if 'adminEmail' app param is specified or 'useDefaultEmail' is false
            'retryLimit' => 5, // optional
        ],
    ],
];
```

If you want to disable default "from" and "reply to" email address for messages you can set `useDefaultEmail` to `false` in Mailer config, but then you must specify "from" email address for every message you send.

Property `retryLimit` allows to make Mailer relatively failover. Mailer will try to send a transmission few times if it's getting an exception from Sparkpost, because sometimes Sparkpost API fails. When transmission send attempts count reaches specified `retryLimit` - last exception we got will be thrown. To disable that behavior and try to send transmission only once you can set `retryLimit` to `0`.

### Http Adapters

[](#http-adapters)

You can use different http adapters: cUrl (default), guzzle, etc. Full list is here: [available adapters](https://github.com/egeloen/ivory-http-adapter/blob/master/doc/adapters.md).
For detailed information refer to [ivory-http-adapter](https://github.com/egeloen/ivory-http-adapter).

To configure a http adapter that will be used by mailer you must specify `httpAdapter` attribute in the component configuration, you can use string, array or closure (`BaseYii::createObject()` is used to instantiate an adapter):

```
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'viewPath' => '@common/mail',
            'httpAdapter' => 'Ivory\HttpAdapter\Guzzle6HttpAdapter', // OR array or closure
        ],
    ],
];
```

### Send an email

[](#send-an-email)

You can then send an email as follows:

```
Yii::$app->mailer->compose('contact/html')
    ->setFrom('from@domain.com')
    ->setTo($to)
    ->setSubject($from)
    ->send();
```

After email was sent few properties are filled with last transmission information:

```
$mailer = Yii::$app->mailer;

$mailer->lastTransmissionId; // string, id of the last transmission
$mailer->lastError; // APIResponseException we got from Sparkpost library with detailed information from the response
$mailer->sentCount; // int, amount of successfuly sent messages
$mailer->rejectedCount; // int, amount of rejected messages
```

### Sandbox mode

[](#sandbox-mode)

To test email sending while your domain is not verified by Sparkpost you can use sandbox mode by setting `sandbox` option to true or use `setSandbox` directly for message.
Besides that you must set "from" email address to sandbox Sparkpost domain.

```
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'sandbox' => true,
            'defaultEmail' => 'test@SPARKPOST_SANDBOX_DOMAIN',
        ],
    ],
];
```

OR

```
Yii::$app->mailer->compose('contact/html')
    ->setSandbox(true)
    ->setFrom('test@SPARKPOST_SANDBOX_DOMAIN')
    ->setTo($to)
    ->setSubject($subject)
    ->send();
```

### Templates

[](#templates)

Sparkpost templates are supported.
When you use a template, template's "from" and "subject" params will be used and override specified by you values.

To set message template and it's substitution data (template context) you should either give an array with specified `template` key and template data as a second param:

```
Yii::$app->mailer->compose(['template' => 'sparkpost_template_id'], ['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();
```

OR use `setTemplateId()` and `setSubstitutionData()` Message methods:

```
Yii::$app->mailer->compose()
    ->setTemplateId('sparkpost_template_id')
    ->setSubstitutionData(['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();
```

Besides that you can specify different set of template data, metadata, tags. You can specify these data for `To`, `Cc`, `Bcc` recipients. To do that you need to pass an array of addresses, where `key` is email and `value` is an array with specified `metadata`, `tags` and/or recipient `name`:

```
$to = [
    'example@mail.com' => [
        'name' => 'Recipient #1',
        'metadata' => [
            'key' => 'value',
        ],
        'substitution_data' => [
            'template_key' => 'value',
        ],
        'tags' => ['tag1', 'tag2'],
    ],
    // ... other possible addresses
];

Yii::$app->mailer->compose(['template' => 'sparkpost_template_id'], ['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();
```

Unit Testing
------------

[](#unit-testing)

You must have Codeception installed to be able to run unit tests.

To run tests:

```
php /vendor/bin/codecept run

```

If you want to try to send a real message, you should add APIKEY environment variable (it should be a real API key from SparkPost).
Example:

```
APIKEY=your_api_key php /vendor/bin/codecept run

```

Logs
----

[](#logs)

All mailer log messages are logged by `\Yii::error()`, `\Yii::warning()`, `\Yii::info()` under special category - `sparkpost-mailer`.

License
-------

[](#license)

The code for yii2-sparkpost is distributed under the terms of the MIT license (see [LICENSE](LICENSE)).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Recently: every ~66 days

Total

12

Last Release

3430d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b83b3d7391a67d45c77e910edd28b31f448e592cf434054c9bf27cbf706b8ac7?d=identicon)[djagya](/maintainers/djagya)

---

Top Contributors

[![alexander-emelyanov](https://avatars.githubusercontent.com/u/1540608?v=4)](https://github.com/alexander-emelyanov "alexander-emelyanov (3 commits)")[![bariew](https://avatars.githubusercontent.com/u/827508?v=4)](https://github.com/bariew "bariew (2 commits)")[![mary-grace](https://avatars.githubusercontent.com/u/8039851?v=4)](https://github.com/mary-grace "mary-grace (1 commits)")[![vitaliykoziy](https://avatars.githubusercontent.com/u/1904996?v=4)](https://github.com/vitaliykoziy "vitaliykoziy (1 commits)")

---

Tags

mailersparkpostyii2yii2-sparkpostemailmaileryii2extensionyiisparkpost

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/djagya-yii2-sparkpost/health.svg)

```
[![Health](https://phpackages.com/badges/djagya-yii2-sparkpost/health.svg)](https://phpackages.com/packages/djagya-yii2-sparkpost)
```

###  Alternatives

[nickcv/yii2-mandrill

Mandrill Api Integration for Yii2

29554.2k2](/packages/nickcv-yii2-mandrill)[boundstate/yii2-mailgun

Mailgun integration for the Yii framework

28160.6k](/packages/boundstate-yii2-mailgun)[nterms/yii2-mailqueue

Email queue component for yii2 that works with yii2-swiftmailer.

87129.2k2](/packages/nterms-yii2-mailqueue)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
