Mandrill, Devise, and Mailchimp Templates
nVisium is primarily known for its software security work. A lesser known and emerging piece of nVisium’s core business is software development (for nVisium products). As such, we will continue to discuss software security but will also provide blog posts on software development. This post represents a non-security, purely development focused, blog post.
This blog post assumes you are familiar with Ruby, Rails, and the Devise gem.
Mandrill is a fairly awesome transactional mailing service built by the folks that brought you MailChimp.
Before integrating Mandrill with Devise, there are a few things you may wish to do:
1) Authenticate and Verify your domain (DKIM + SPF)
2) Integrate MailChimp with your Mandrill account (in order to import MailChimp Templates)
3) Import your MailChimp Templates into Mandrill
Once these steps have been completed, you’ll no longer see “via
Devise has several methods it uses by default for email-related purposes. If you review the devise gem’s mailer, you can see their conventions:
So in order to override the default mailer, you must first specify your mailer class (and an email address) in the devise configuration file:
Now you need to override Devise’s Mailer with your own methods:
After doing this, you need to add some mailer code. Now, for the purposes of this post, I decided to use an existing template that I pulled from MailChimp into Mandrill. That means I had to add some variables into the template. The following is an example Forgot Password template:
As you can see, I used a marker or placeholder for the content to be substituted:
The password reset code is below. The key point to notice is that the options hash contains the name of the template as well as
global\_merge\_vars, an array of hashes that contains the name of the variable to replace
(password\_reset\_link) and the replacement content (“http://www.example.com/users/….). The options hash is sent to the
mandrill\_send method which then calls
This last piece is just making sure the configuration file is set up for Mandrill:
With all that done, you should be able to send email via Mandrill leveraging MailChimp and overriding Devise’s default mailer!