Using SparkPost for Transactional emails with Laravel

Posted on April 26, 2016 | By Matt Stauffer

Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

Recently, Mandrill announced that they'd be sunsetting their transactional email service and instead rolling it in to a secondary service for paid MailChimp users. That's fine for them, but many of us were using it for small one-off apps and weren't interested in all of a sudden paying money to send 100 emails a month.

Mailgun and Sendgrid have been standby transactional email providers for a while, and there's also Amazon's SES, CampaignMonitor, and higher-cost-high-uptime premium Postmark.

But right when Mandrill announced their pricing change, a new transactional email provider came out of nowhere: SparkPost. They claim they've been around for years and power most of the Internet, but that's not entirely true. Rather, they are a new transactional service built on top of an old and powerful infrastructure, called "Momentum" by MessageSystems. But Momentum itself is definitely proven:

Momentum-powers-the-web

So let's walk through the process of signing up and moving Giscus, my app for notifying you of comments on your gists, from Mandrill to SparkPost.

Note: Most of the stuff in this article is easy to do. I'm writing it to give you a sense of what SparkPost is like in case you want to compare it with another provider, not because I think you need instructions for how to sign up. :)

Signing up for SparkPost

First, let's go sign up.

SparkPost sign-up

100k free emails a month for the lifetime of the account? Yes please.

SparkPost welcome screen

Now, I enter my domain. Sadly I don't have access to either of these email addresses, so let's see what else we can do.

SparkPost SMTP vs REST

I'll choose REST. We could technically use either, but I prefer using an API if possible. This gets me an API key, so I'll copy it down and then head over to the dashboard.

Note: If you want it done fast, or if you're using Laravel prior to 5.2, just use SMTP. You can copy the credentials, paste them into your .env file, update your app config to use SMTP, and then you're done.

SparkPost dashboard

Well ain't this pretty! This daily limit was 500 until I verified my email address, but now it's 10,000. What's next? Verify my sending domain. Let's do it.

SparkPost Domain Verification

Just like any other email provider, I'll need to set up DKIM and SPF records to verify ownership of the domain. Your experience may vary based on your DNS provider, but with DNSimple this is easy as pie. Once I set up the SPF and DKIM records, I was marked "ready to send."

Setting up SparkPost with Laravel

So our SparkPost account is up and running. Let's now connect it to Giscus.

Like I mentioned earlier, the fastest option is SMTP. But I want to try the full API integration, so I'm going to upgrade Giscus to 5.2 using Laravel Shift and then that'll get me access to the SparkPost driver.

Make sure you're on Laravel 5.2.29 or later. I wasn't, so I upgraded, and now I need to add a sparkpost array to my config/services.php file:

'sparkpost' => [
    'secret' => env('SPARKPOST_SECRET'),
],

I'll grab my API key that I stored earlier, head to my .env file, and put it in there as SPARKPOST_SECRET:

SPARKPOST_SECRET=1509812piu4nlkjadhfo98qwrw

Finally, I'll update .env to show it that I'm using the sparkpost driver:

MAIL_DRIVER=sparkpost

And that's it! Mail's now coming through via my new SparkPost driver.

The dashboard

One of the main reasons I wrote this post was to show folks what SparkPost feels like, so here are a few screenshots of the dashboard:

User accounts

SparkPost User Accounts

Webhooks

SparkPost Webhooks

Reports

SparkPost Reports

SparkPost Events

Miscellany

SparkPost also offers lists and templates, if you want to use their API directly. Check out the PHP-SparkPost package on Packagist.

Important caveat:

One thing I've noticed is that something that I would often do on other providers (I can't remember who does and doesn't have it, sadly, but I know Mailgun does) is inspect the contents of an email that was sent. I'll often use it in debugging or helping folks with spam issues.

I haven't yet found out how to do it in SparkPost, and I'm worried it may not be possible. I've asked SparkPost on twitter, so we'll see what the response is. If they don't have it, that would be reason enough for me to use Mailgun instead. I hope I'm wrong, though, and it's just hiding in there somewhere!

Conclusion

That's all. It's very simple to set up and verify a SparkPost account; it's a proven platform; they have a generous free plan; and the dashboard is very easy to use, save the possible lack of the ability to inspect individual messages.

Have you had good or bad experiences with SparkPost? Let me know on twitter.


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5.2  •  sparkpost  •  email