|
| 1 | +<h3>Using Mandrill Email API</h3> |
| 2 | + |
| 3 | +I was working on a hackathon project last weekend, and we need a quick way to send emails out from our Sinatra application. At first we tried to integrate the <a href="https://rubygems.org/gems/gmail" target="_blank">gmail gem</a>, but after realizing that is an easy way to get that account flagged at spam, we decided to turn to Mandrill by Mailchimp. |
| 4 | + |
| 5 | +I'll show you how to |
| 6 | +<ul> |
| 7 | + <li>integrate the Mandrill API</li> |
| 8 | + <li>how to send mail with the Mandrill API</li> |
| 9 | + <li>and some quick advanced features in the mandrill API</li> |
| 10 | + <ul> |
| 11 | + <li>How to send with multiple users in To and other fields</li> |
| 12 | + <li>How to suppress users from the To field</li> |
| 13 | + <li>How to use the Mandrill Test Key to make the email call but not actually push out an email to count against your monthly API call limit.</li> |
| 14 | + <li>How to use "Merge Fields" in Mandrill to merge in custom URL's and data into the email about the user you are sending to.</li> |
| 15 | + </ul> |
| 16 | +</ul> |
| 17 | + |
| 18 | +__________________________ |
| 19 | + |
| 20 | +<h4>Example APP: My Penpal</h4> |
| 21 | + |
| 22 | +We are going to build a quick application to send emails out to users who subscribe to your mailing list. |
| 23 | + |
| 24 | +I created a bare bones Rails App for this tutorial on using Mandrill. |
| 25 | + |
| 26 | +__But first, lets get a Mandrill account.__ |
| 27 | + |
| 28 | +<h4>Get API Key:</h4> |
| 29 | +Go to [https://mandrill.com/signup/](https://mandrill.com/signup/) and *set up a quick FREE acount*. You just got 12,000 emails per month! |
| 30 | + |
| 31 | +After you create an account, *Login*. |
| 32 | + |
| 33 | +(Take note of your login info somewhere.) |
| 34 | + |
| 35 | +__________________________ |
| 36 | + |
| 37 | +Open up this link: [Most useful page for getting started with the Ruby Gem](http://help.mandrill.com/entries/23257181-Using-the-Mandrill-Ruby-Gem). This was a good basic look at the Mandrill API send email function with the <code>gem 'madrill-api'</code> which will go in your gem file.</code> |
| 38 | + |
| 39 | +__STEP 1:__ put <code>gem 'madrill-api'</code> in your <code>Gemfile</code> |
| 40 | + |
| 41 | +__STEP 2:__ run bundle install |
| 42 | + |
| 43 | +<pre> |
| 44 | +MANDRILL_APIKEY = "alskdfjzixcvxkjzlvc" #test or the #real one |
| 45 | +</pre> |
| 46 | + |
| 47 | +<pre> |
| 48 | +require 'mandrill' |
| 49 | +m = Mandrill::API.new |
| 50 | +message = { |
| 51 | + :subject=> "Hello from the Mandrill API", |
| 52 | + :from_name=> "Your name", |
| 53 | + :text=>"Hi message, how are you?", |
| 54 | + :to=>[ |
| 55 | + { |
| 56 | + :email=> "recipient@theirdomain.com", |
| 57 | + :name=> "Recipient1" |
| 58 | + } |
| 59 | + ], |
| 60 | + :html=>"<html><h1>Hi <strong>message</strong>, how are you?</h1></html>", |
| 61 | + :from_email=>"sender@yourdomain.com" |
| 62 | +} |
| 63 | +sending = m.messages.send message |
| 64 | +puts sending |
| 65 | +</pre> |
| 66 | + |
| 67 | +[Mandrill: Full Message Calls Documentation](https://mandrillapp.com/api/docs/messages.JSON.html) |
| 68 | + |
| 69 | +[Where to view API Logs](https://mandrillapp.com/settings/api) |
| 70 | + |
| 71 | +__________________________ |
| 72 | + |
| 73 | +If you go to the branch in the repo you forked above called <code>$ git checkout final-project;</code>. This is my finished code from this tutorial, so you can check that it is all there compared to your work. |
0 commit comments