Welcome to SparkPost! This article will show how to migrate existing applications that send using SMTP from Mandrill to SparkPost. We’ll be adding some variables to your Heroku environment, and once that’s done, your app will need to be updated to use the new variables.
Domain Setup
SparkPost is setup with a sandbox domain which can be used to send up to 5 messages. To send more messages, create a sending domain via the SparkPost dashboard or the API.
In order to properly sign and authenticate messages sent through your account, there are some changes you’ll need to make up front. Verifying your sending domain is described in detail in our Support Center. This is one of the ways we protect your domain reputation, as well as our own reputation, from being tainted by e.g. phishing.
Installing the SparkPost add-on
First, lets install the add-on using the heroku CLI:
$ heroku addons:create sparkpost -----> Adding sparkpost to your-app-42... done, v18 (free)
Next, let’s confirm that the SparkPost API key is configured, and add it to the local environment variables:
$ heroku config:get SPARKPOST_API_KEY -s | tee -a .env SPARKPOST_API_KEY='YSBmYWtlIGFwaSBrZXksIG1hZGUgeW91IGxvb2sh' $ for s in PORT HOST USERNAME PASSWORD ; do heroku config:get SPARKPOST_SMTP_$s -s | tee -a .env ; done
Credentials and other sensitive configuration values should not be committed to source-control. In Git, you can exclude the
.env
file with
echo env >> .gitignore
Using the SparkPost config settings in your app
Here’s an ActionMailer example adapted from the Mandrill page:
ActionMailer::Base.smtp_settings = { port: ENV['SPARKPOST_SMTP_PORT'], address: ENV['SPARKPOST_SMTP_HOST'], user_name: ENV['SPARKPOST_SMTP_USERNAME'], password: ENV['SPARKPOST_SMTP_PASSWORD'], authentication: :login, enable_starttls_auto: true } ActionMailer::Base.delivery_method = :smtp
Check out the SparkPost add-on page for more examples, and our API docs for examples of how we can handle generating messages for you using our Transmission API.
Thanks for choosing SparkPost! If you have questions, feel free to contact us on Twitter @SparkPost, ask a question on StackOverflow, or join us on Slack.