Sending emails with mandrill

jamesperet 9 years ago
parent
commit
08fdaf0e0d

+ 1 - 0
app/controllers/contact_messages_controller.rb

@@ -17,6 +17,7 @@ class ContactMessagesController < ApplicationController
17 17
     @contact_message.unread = true
18 18
     respond_to do |format|
19 19
       if @contact_message.save
20
+        UserMailer.contact_message(@contact_message).deliver 
20 21
         format.html { redirect_to @contact_message, notice: 'Contact message was successfully created.' }
21 22
         format.json { render action: 'show', status: :created, location: @contact_message }
22 23
       else

+ 1 - 0
app/helpers/contact_messages_helper.rb

@@ -1,2 +1,3 @@
1 1
 module ContactMessagesHelper
2
+
2 3
 end

+ 14 - 0
app/mailers/user_mailer.rb

@@ -0,0 +1,14 @@
1
+class UserMailer < ActionMailer::Base
2
+  default from: "from@example.com"
3
+  
4
+
5
+  def contact_message(contact_message)
6
+    @config = Info.first
7
+    @msg = contact_message
8
+    mail :subject => @msg.title,
9
+         :to      => @config.contact_email,
10
+         :from    => @msg.email,
11
+         :from_name => @msg.name
12
+  end
13
+  
14
+end

+ 10 - 0
app/models/contact_message.rb

@@ -1,3 +1,13 @@
1 1
 class ContactMessage < ActiveRecord::Base
2 2
   belongs_to :user
3
+  
4
+  def name
5
+    if user != nil
6
+      name = self.user.first_name.to_s + ' ' + self.user.last_name.to_s
7
+    else
8
+      name = self.email
9
+    end
10
+    return name
11
+  end
12
+  
3 13
 end

+ 231 - 0
app/views/user_mailer/contact_message.html.erb

@@ -0,0 +1,231 @@
1
+<!-- ***************************************************
2
+********************************************************
3
+
4
+HOW TO USE: Use these code examples as a guideline for formatting your HTML email. You may want to create your own template based on these snippets or just pick and choose the ones that fix your specific rendering issue(s). There are two main areas in the template: 1. The header (head) area of the document. You will find global styles, where indicated, to move inline. 2. The body section contains more specific fixes and guidance to use where needed in your design.
5
+
6
+DO NOT COPY OVER COMMENTS AND INSTRUCTIONS WITH THE CODE to your message or risk spam box banishment :).
7
+
8
+It is important to note that sometimes the styles in the header area should not be or don't need to be brought inline. Those instances will be marked accordingly in the comments.
9
+
10
+********************************************************
11
+**************************************************** -->
12
+
13
+<!-- Using the xHTML doctype is a good practice when sending HTML email. While not the only doctype you can use, it seems to have the least inconsistencies. For more information on which one may work best for you, check out the resources below.
14
+
15
+UPDATED: Now using xHTML strict based on the fact that gmail and hotmail uses it.  Find out more about that, and another great boilerplate, here: http://www.emailology.org/#1
16
+
17
+More info/Reference on doctypes in email:
18
+Campaign Monitor - http://www.campaignmonitor.com/blog/post/3317/correct-doctype-to-use-in-html-email/
19
+Email on Acid - http://www.emailonacid.com/blog/details/C18/doctype_-_the_black_sheep_of_html_email_design
20
+-->
21
+
22
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
23
+
24
+<html xmlns="http://www.w3.org/1999/xhtml">
25
+<head>
26
+	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
27
+	<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
28
+	<title>Your Message Subject or Title</title>
29
+	<style type="text/css">
30
+
31
+		/***********
32
+		Originally based on The MailChimp Reset from Fabio Carneiro, MailChimp User Experience Design
33
+		More info and templates on Github: https://github.com/mailchimp/Email-Blueprints
34
+		http://www.mailchimp.com &amp; http://www.fabio-carneiro.com
35
+
36
+		INLINE: Yes.
37
+		***********/
38
+		/* Client-specific Styles */
39
+		#outlook a {padding:0;} /* Force Outlook to provide a "view in browser" menu link. */
40
+		body{width:100% !important; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0;}
41
+		/* Prevent Webkit and Windows Mobile platforms from changing default font sizes, while not breaking desktop design. */
42
+		.ExternalClass {width:100%;} /* Force Hotmail to display emails at full width */
43
+		.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} /* Force Hotmail to display normal line spacing.  More on that: http://www.emailonacid.com/forum/viewthread/43/ */
44
+		#backgroundTable {margin:0; padding:0; width:100% !important; line-height: 100% !important;}
45
+		/* End reset */
46
+
47
+		/* Some sensible defaults for images
48
+		1. "-ms-interpolation-mode: bicubic" works to help ie properly resize images in IE. (if you are resizing them using the width and height attributes)
49
+		2. "border:none" removes border when linking images.
50
+		3. Updated the common Gmail/Hotmail image display fix: Gmail and Hotmail unwantedly adds in an extra space below images when using non IE browsers. You may not always want all of your images to be block elements. Apply the "image_fix" class to any image you need to fix.
51
+
52
+		Bring inline: Yes.
53
+		*/
54
+		img {outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;}
55
+		a img {border:none;}
56
+		.image_fix {display:block;}
57
+
58
+		/** Yahoo paragraph fix: removes the proper spacing or the paragraph (p) tag. To correct we set the top/bottom margin to 1em in the head of the document. Simple fix with little effect on other styling. NOTE: It is also common to use two breaks instead of the paragraph tag but I think this way is cleaner and more semantic. NOTE: This example recommends 1em. More info on setting web defaults: http://www.w3.org/TR/CSS21/sample.html or http://meiert.com/en/blog/20070922/user-agent-style-sheets/
59
+
60
+		Bring inline: Yes.
61
+		**/
62
+		p {margin: 1em 0;}
63
+
64
+		/** Hotmail header color reset: Hotmail replaces your header color styles with a green color on H2, H3, H4, H5, and H6 tags. In this example, the color is reset to black for a non-linked header, blue for a linked header, red for an active header (limited support), and purple for a visited header (limited support).  Replace with your choice of color. The !important is really what is overriding Hotmail's styling. Hotmail also sets the H1 and H2 tags to the same size.
65
+
66
+		Bring inline: Yes.
67
+		**/
68
+		h1, h2, h3, h4, h5, h6 {color: black !important;}
69
+
70
+		h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {color: blue !important;}
71
+
72
+		h1 a:active, h2 a:active,  h3 a:active, h4 a:active, h5 a:active, h6 a:active {
73
+			color: red !important; /* Preferably not the same color as the normal header link color.  There is limited support for psuedo classes in email clients, this was added just for good measure. */
74
+		 }
75
+
76
+		h1 a:visited, h2 a:visited,  h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited {
77
+			color: purple !important; /* Preferably not the same color as the normal header link color. There is limited support for psuedo classes in email clients, this was added just for good measure. */
78
+		}
79
+
80
+		/** Outlook 07, 10 Padding issue: These "newer" versions of Outlook add some padding around table cells potentially throwing off your perfectly pixeled table.  The issue can cause added space and also throw off borders completely.  Use this fix in your header or inline to safely fix your table woes.
81
+
82
+		More info: http://www.ianhoar.com/2008/04/29/outlook-2007-borders-and-1px-padding-on-table-cells/
83
+		http://www.campaignmonitor.com/blog/post/3392/1px-borders-padding-on-table-cells-in-outlook-07/
84
+
85
+		H/T @edmelly
86
+
87
+		Bring inline: No.
88
+		**/
89
+		table td {border-collapse: collapse;}
90
+
91
+		/** Remove spacing around Outlook 07, 10 tables
92
+
93
+		More info : http://www.campaignmonitor.com/blog/post/3694/removing-spacing-from-around-tables-in-outlook-2007-and-2010/
94
+
95
+		Bring inline: Yes
96
+		**/
97
+		table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }
98
+
99
+		/* Styling your links has become much simpler with the new Yahoo.  In fact, it falls in line with the main credo of styling in email, bring your styles inline.  Your link colors will be uniform across clients when brought inline.
100
+
101
+		Bring inline: Yes. */
102
+		a {color: orange;}
103
+
104
+		/* Or to go the gold star route...
105
+		a:link { color: orange; }
106
+		a:visited { color: blue; }
107
+		a:hover { color: green; }
108
+		*/
109
+
110
+		/***************************************************
111
+		****************************************************
112
+		MOBILE TARGETING
113
+
114
+		Use @media queries with care.  You should not bring these styles inline -- so it's recommended to apply them AFTER you bring the other stlying inline.
115
+
116
+		Note: test carefully with Yahoo.
117
+		Note 2: Don't bring anything below this line inline.
118
+		****************************************************
119
+		***************************************************/
120
+
121
+		/* NOTE: To properly use @media queries and play nice with yahoo mail, use attribute selectors in place of class, id declarations.
122
+		table[class=classname]
123
+		Read more: http://www.campaignmonitor.com/blog/post/3457/media-query-issues-in-yahoo-mail-mobile-email/
124
+		*/
125
+		@media only screen and (max-device-width: 480px) {
126
+
127
+			/* A nice and clean way to target phone numbers you want clickable and avoid a mobile phone from linking other numbers that look like, but are not phone numbers.  Use these two blocks of code to "unstyle" any numbers that may be linked.  The second block gives you a class to apply with a span tag to the numbers you would like linked and styled.
128
+
129
+			Inspired by Campaign Monitor's article on using phone numbers in email: http://www.campaignmonitor.com/blog/post/3571/using-phone-numbers-in-html-email/.
130
+
131
+			Step 1 (Step 2: line 224)
132
+			*/
133
+			a[href^="tel"], a[href^="sms"] {
134
+						text-decoration: none;
135
+						color: black; /* or whatever your want */
136
+						pointer-events: none;
137
+						cursor: default;
138
+					}
139
+
140
+			.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
141
+						text-decoration: default;
142
+						color: orange !important; /* or whatever your want */
143
+						pointer-events: auto;
144
+						cursor: default;
145
+					}
146
+		}
147
+
148
+		/* More Specific Targeting */
149
+
150
+		@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
151
+			/* You guessed it, ipad (tablets, smaller screens, etc) */
152
+
153
+			/* Step 1a: Repeating for the iPad */
154
+			a[href^="tel"], a[href^="sms"] {
155
+						text-decoration: none;
156
+						color: blue; /* or whatever your want */
157
+						pointer-events: none;
158
+						cursor: default;
159
+					}
160
+
161
+			.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
162
+						text-decoration: default;
163
+						color: orange !important;
164
+						pointer-events: auto;
165
+						cursor: default;
166
+					}
167
+		}
168
+
169
+		@media only screen and (-webkit-min-device-pixel-ratio: 2) {
170
+			/* Put your iPhone 4g styles in here */
171
+		}
172
+
173
+		/* Following Android targeting from:
174
+		http://developer.android.com/guide/webapps/targeting.html
175
+		http://pugetworks.com/2011/04/css-media-queries-for-targeting-different-mobile-devices/  */
176
+		@media only screen and (-webkit-device-pixel-ratio:.75){
177
+			/* Put CSS for low density (ldpi) Android layouts in here */
178
+		}
179
+		@media only screen and (-webkit-device-pixel-ratio:1){
180
+			/* Put CSS for medium density (mdpi) Android layouts in here */
181
+		}
182
+		@media only screen and (-webkit-device-pixel-ratio:1.5){
183
+			/* Put CSS for high density (hdpi) Android layouts in here */
184
+		}
185
+		/* end Android targeting */
186
+	</style>
187
+
188
+	<!-- Targeting Windows Mobile -->
189
+	<!--[if IEMobile 7]>
190
+	<style type="text/css">
191
+
192
+	</style>
193
+	<![endif]-->
194
+
195
+	<!-- ***********************************************
196
+	****************************************************
197
+	END MOBILE TARGETING
198
+	****************************************************
199
+	************************************************ -->
200
+
201
+	<!--[if gte mso 9]>
202
+	<style>
203
+		/* Target Outlook 2007 and 2010 */
204
+	</style>
205
+	<![endif]-->
206
+</head>
207
+<body>
208
+	<!-- Wrapper/Container Table: Use a wrapper table to control the width and the background color consistently of your email. Use this approach instead of setting attributes on the body tag. -->
209
+	<table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
210
+	<tr>
211
+		<td>
212
+
213
+		<!-- Tables are the most common way to format your email consistently. Set your table widths inside cells and in most cases reset cellpadding, cellspacing, and border to zero. Use nested tables as a way to space effectively in your message. -->
214
+		<table cellpadding="0" cellspacing="0" border="0" align="center">
215
+			<tr>
216
+				<td width="200" valign="top"></td>
217
+				<td width="200" valign="top"></td>
218
+				<td width="200" valign="top"></td>
219
+			</tr>
220
+		</table>
221
+
222
+		<!-- End example table -->
223
+
224
+		<p><%= @msg.content %></p>
225
+		
226
+		</td>
227
+	</tr>
228
+	</table>
229
+	<!-- End of wrapper table -->
230
+</body>
231
+</html>

+ 12 - 2
config/environments/development.rb

@@ -27,7 +27,17 @@ RailsWebsiteTemplate::Application.configure do
27 27
   # number of complex assets.
28 28
   config.assets.debug = true
29 29
   
30
-  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
31
-  config.action_mailer.delivery_method = :letter_opener
30
+  #config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
31
+  #config.action_mailer.delivery_method = :letter_opener
32
+  
33
+  config.action_mailer.smtp_settings = {
34
+      :address   => "smtp.mandrillapp.com",
35
+      :port      => 587, # ports 587 and 2525 are also supported with STARTTLS
36
+      :enable_starttls_auto => true, # detects and uses STARTTLS
37
+      :user_name => "james.peret@gmail.com",
38
+      :password  => "UZIxm2vCtBr66yWRpC_Big", # SMTP password is any valid API key
39
+      :authentication => 'login', # Mandrill supports 'plain' or 'login'
40
+      :domain => 'rails_website_template.dev', # your domain to identify your server when connecting
41
+    }
32 42
   
33 43
 end

+ 3 - 0
readme.md

@@ -27,7 +27,10 @@ Other features are still under development:
27 27
 
28 28
 ## Todo's
29 29
 
30
+* contact message sed email
31
+* production emails with mandrill
30 32
 * edit account layout
33
+* contact message layout
31 34
 * devise forgot pasword layout
32 35
 * devise change password layout
33 36
 

+ 7 - 0
test/mailers/user_mailer_test.rb

@@ -0,0 +1,7 @@
1
+require 'test_helper'
2
+
3
+class UserMailerTest < ActionMailer::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end