Skip to content

Commit c145adb

Browse files
committed
Update README for github tutorial
1 parent bf20f99 commit c145adb

20 files changed

+1077
-23
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.env
1+
.env
2+
3+
vendor/bundle # for bundle in .rbenv

README.md

+143-22
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,159 @@ __________________________
3636

3737
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>
3838

39-
__STEP 1:__ put <code>gem 'madrill-api'</code> in your <code>Gemfile</code>
39+
__STEP 1:__ add <code>gem 'madrill-api'</code> in your <code>Gemfile</code>
40+
41+
<pre>
42+
source 'https://rubygems.org'
43+
44+
gem 'sinatra'
45+
gem 'sinatra-contrib'
46+
gem 'thin'
47+
gem 'mandrill-api'
48+
</pre>
49+
50+
__STEP 2:__ run <code>$ bundle install</code>
51+
52+
__STEP 3:__ run <code>$ touch .env</code> to create a <code>.env</code> file.
53+
54+
__STEP 4:__ Go to [https://mandrillapp.com/settings/index](https://mandrillapp.com/settings/index) -> the __"SMTP & API Credentials"__ page. Create a new API key.
55+
56+
Add the following to your <code>.env</code> file:
57+
58+
<pre>
59+
MANDRILL_APIKEY = 'xxxxxxxxxxxxxxxxxxxxx' #test or the #real one
60+
</pre>
61+
62+
As [this getting going page](http://help.mandrill.com/entries/23257181-Using-the-Mandrill-Ruby-Gem) says: <code>The gem assumes your API key is stored as an environment variable called MANDRILL_APIKEY.</code>. So it goes in the <code>.env</code> file and requires no setup beyond that.
63+
64+
__STEP 5:__ add a new folder called <code>lib/</code> with <code>$ mkdir lib</code>
65+
66+
__STEP 6:__ add a new file in there called <code>lib/email.rb</code>
67+
68+
__STEP 7:__ make a basic ruby class called <code>class MyEmailer</code> in <code>email.rb</code> and also add the '<code>requires</code>' statements that match the <code>Gemfile</code> at the top. We will also add an empty <code>MyEmailer#send</code> method to the new class.
69+
70+
__-------STRANGE THING TO NOTE HERE:__ The gem is <code>gem 'madrill-api'</code> and the require is <code>require 'mandrill'</code>. Not the same. Confusing.
71+
72+
73+
<code>email.rb</code> at this point:
74+
<pre>
75+
require 'rubygems'
76+
require 'bundler/setup'
77+
require 'thin'
78+
require 'mandrill'
79+
80+
class MyEmailer
81+
def send
82+
end
83+
end
84+
</pre>
85+
86+
Also add the Mandrill require line to your main Sinatra file: <code>mandrill-tutorial.rb</code>:
87+
88+
<pre>
89+
require 'rubygems'
90+
require 'bundler/setup'
91+
require 'sinatra'
92+
require 'sinatra/reloader'
93+
require 'thin' ## ALSO THIS FOR FOREMAN
94+
require 'mandrill' ## ADD THIS LINE HERE
4095

41-
__STEP 2:__ run bundle install
96+
97+
get '/' do
98+
'Welcome to your Mail Sender app!'
99+
end
100+
</pre>
101+
102+
__STEP 7:__ Run <code>$ bundle install</code>.
103+
104+
__STEP 8:__ Add this code to your <code>MyEmailer#send</code> method:
105+
106+
<pre>
107+
require 'rubygems'
108+
require 'bundler/setup'
109+
require 'mandrill'
110+
111+
class MyEmailer
112+
def send(email)
113+
<strong>
114+
m = Mandrill::API.new
115+
message = {
116+
:subject=> "Hello from the Mandrill API",
117+
:from_name=> "Your name",
118+
:text=>"Hi message, how are you?",
119+
:to=>[
120+
{
121+
:email=> "#{email}", ## this uses the email argument passed into this method
122+
:name=> "Recipient1"
123+
}
124+
],
125+
:html=>"<html>&lt;h1&gt;Hi &lt;strong&gt;message&lt;/strong&gt;, how are you?&lt;/h1&gt;</html>",
126+
:from_email=>"sender@yourdomain.com"
127+
}
128+
sending = m.messages.send message
129+
puts sending
130+
</strong>
131+
end
132+
end
133+
</pre>
134+
135+
__STEP 9:__ Go back to the main Sinatra file <code>mandrill-tutorial.rb</code>:
136+
137+
<pre>
138+
require 'rubygems'
139+
require 'bundler/setup'
140+
require 'sinatra'
141+
require 'sinatra/reloader'
142+
require 'mandrill'
143+
144+
<strong>require_relative 'lib/email' ## THIS IS A NEW LINE AT THIS POINT</strong>
145+
146+
get '/' do
147+
'Welcome to your Penpal app!'
148+
end
149+
150+
<strong>
151+
get '/send/:email' do
152+
m = MyEmailer.new
153+
m.send(params[:email])
154+
"Email Sent" ## Sinatra likes to print something out .. so this
155+
end
156+
</strong>
157+
</pre>
158+
159+
This will allow you to put in an email address in your browser URI like <code>http://localhost:4567/send/something%40domain.com</code>, and this will send your email into the <code>#send</code> method using GET params.
160+
161+
__STEP 10:__ Run <code>foreman</code> webserver to get going:
42162

43163
<pre>
44-
MANDRILL_APIKEY = "alskdfjzixcvxkjzlvc" #test or the #real one
164+
$ foreman run ruby mandrill-tutorial.rb
45165
</pre>
46166

167+
__STEP 11:__ Go visit your neat new homepage:
168+
47169
<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>&lt;h1&gt;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
170+
http://localhost:4567/
65171
</pre>
66172

67-
[Mandrill: Full Message Calls Documentation](https://mandrillapp.com/api/docs/messages.JSON.html)
173+
__STEP 12:__ Go send yourself an email:
174+
175+
<pre>
176+
http://localhost:4567/email/youremailprefix%40something.com
177+
</pre>
178+
179+
__NOTE:__ Fill in your email for: <code>youremailprefix%40something.com</code> in the above URL. Also, <code>%40</code> is <code>@</code> in URL encoding.
180+
181+
__FINAL:__ If you go to the branch in the repo you forked above called <code>$ git checkout final;</code>. This is my finished code from this tutorial, so you can check that it is all there compared to your work.
182+
183+
___________________________
184+
185+
<h3>Resources:</h3>
186+
187+
[Mandrill: Full Message Calls Documentation](https://mandrillapp.com/api/docs/messages.JSON.html) - for full message calls like adding a more people to the TO field, suppressing recipients, etc.
68188

69189
[Where to view API Logs](https://mandrillapp.com/settings/api)
70190

191+
__NOTE:__ I had to use FOREMAN by heroku instead of thin or webbrick webserver to get env files to work
71192
__________________________
72193

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.
194+

0 commit comments

Comments
 (0)