Integrate SparkPost in your Rails app

SparkPost (SP) is an advanced email delivery service. For a number of reasons including reputation based sending, high deliverability, scalability etc. SP is trusted by big senders like facebook, twitter, linkedin, pinterest, paypal and you name it.

While SP is suitable for most, SPE (Sparkpost Elite) is for senders who wants to run these services on dedicated boxes. SPE also comes with white gloves support.

Integrating SP with your current application is extremely easy. It’s because:

  • You can start FREE :). Its free plan comes with 10000 100,000 emails per month.
  • Unlike others, there is no manual activation required. You can just open account, verify your domain and start sending immediately.
  • Because SP supports both SMTP and API, integrating with your app is just simple.

If you’re still not sure how to integrate SP in your Rails app, keep reading.

Step 1: Open SP account

  • Visit signup page and complete the form. It’s a very tiny form which takes less a than a minute completeScreenshot 2015-10-13 11.31.50Upon signup, you need to verify your email address. Unless you verify your email, you can’t add sending domain that we’ll do in next step.

Step 2: Add and verify your sending domain

Before we add it, you might ask what is sending domain and why you need this for. Here is the answer from official doc.

A sending domain is a domain that is used to indicate who an email is from via the “From:” header. Using a custom sending domain enables you to control what recipients see as the From value in their email clients. DNS records can be configured for a sending domain, which allows recipient mail servers to authenticate your messages.

Screenshot 2015-10-13 12.24.47

Go to Account -> Sending Domains to add a sending domain (you can leave Linked Tracking Domains option as it is for now). Once you’ve added the domain, you need to verify the domain by adding SPF records. You can also follow the official doc for instructions to verify domain.

Step 3: Generate API Key

SP is really simple in terms of this. If you create an API key with “Send via SMTP”, you can use this same key for SMTP authentication. So simply create an API key from Account -> API & SMTP and select Send via SMTP from API Permission.

Screenshot 2015-10-13 12.36.31

If you’re still not sure, follow this doc for detail instruction.

Step 4: Integrate with Rails

So you’ve done all the required ground jobs. The last part is configuring Rails to use your fresh SP credentials. This part is very simple. All you need is to put the SMTP configuration, ideally, in an initializer script. You can simply copy the following gist in a file inside your Rails app’s initializer directory (config/initializers).

Be sure to replace password and from with appropriate values. Password is the API key that you’ve created with “Send via SMTP” permission in previous step.

Now What?

That’s all. You application now has all the goodness of world’s best email sending platform. Start sending now!  You’ve earned it to yourself :).

Definitely you can separate your development and production configuration and/or move this config to environment variables. Also, you can configure your development environment to raise mail sending exceptions. Set

config.action_mailer.raise_delivery_errors = true 

in config/environments/development.rb.

 

[disclaimer: I work for SparkPost with awesome people :)]