Skip to content
This repository
Browse code

Correcting actionmailer guide for Rails 2.3

  • Loading branch information...
commit 92fd824480dde9d80f67a5d1619fe329e58bb8b4 1 parent 6d91632
Mikel Lindsaar authored January 02, 2011
41  railties/guides/source/action_mailer_basics.textile
Source Rendered
@@ -218,7 +218,9 @@ If you set a default +:host+ for your mailers you need to pass +:only_path => fa
218 218
 
219 219
 h4. Sending Multipart Emails
220 220
 
221  
-Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have +welcome_email.text.plain.erb+ and +welcome_email.text.html.erb+ in +app/views/user_mailer+, Action Mailer will automatically send a multipart email with the HTML and text versions setup as different parts.
  221
+Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have +welcome_email.text.plain.erb+ and +welcome_email.text.html.erb+ in +app/views/user_mailer+, Action Mailer will automatically send a "multipart/alternative" email with the HTML and text versions setup as different parts.
  222
+
  223
+A "multipart/alternative" content type tells your email client that there are several representations of the same content available and that the email client is free to choose any one to display to the user.  In this case we are giving a plain text and HTML version of the same message.  But this could also be a text version, and a recording of someone speaking the the same message.  It is important to use "multipart/alternative" only when each part has the same content.
222 224
 
223 225
 To explicitly specify multipart messages, you can do something like:
224 226
 
@@ -242,7 +244,11 @@ end
242 244
 
243 245
 h4. Sending Emails with Attachments
244 246
 
245  
-Attachments can be added by using the +attachment+ method:
  247
+Attachments can be added by using the +attachment+ method.  The +attachment+ method has two variations, you can either pass the body in as an option, or create it within a block.
  248
+
  249
+Usually you will use the variation shown below for the "image/jpeg" attachment, here you just pass in the content type and body as a options hash to the attachment method. However, if you need to do some processing to create the attachment, such as with the PDF below, then the block variation can be used.
  250
+
  251
+This email uses the "multipart/mixed" content type because each part is a different block of content.  This indicates to the email client that it must show all the parts that it can display to the end user.
246 252
 
247 253
 <ruby>
248 254
 class UserMailer < ActionMailer::Base
@@ -250,13 +256,14 @@ class UserMailer < ActionMailer::Base
250 256
     recipients      user.email_address
251 257
     subject         "New account information"
252 258
     from            "system@example.com"
253  
-    content_type    "multipart/alternative"
  259
+    content_type    "multipart/mixed"
254 260
 
255 261
     attachment :content_type => "image/jpeg",
256 262
       :body => File.read("an-image.jpg")
257 263
 
258 264
     attachment "application/pdf" do |a|
259  
-      a.body = generate_your_pdf_here()
  265
+      pdf = generate_your_pdf_here(:name => user)
  266
+      a.body = pdf
260 267
     end
261 268
   end
262 269
 end
@@ -266,7 +273,11 @@ h4. Sending Multipart Emails with Attachments
266 273
 
267 274
 Once you use the +attachment+ method, ActionMailer will no longer automagically use the correct template based on the filename. You must declare which template you are using for each content type via the +part+ method.
268 275
 
269  
-In the following example, there would be two template files, +welcome_email_html.erb+ and +welcome_email_plain.erb+ in the +app/views/user_mailer+ folder.
  276
+Here we are making the email "multipart/mixed" with three top level parts, a "multipart/alternative", an "image/jpeg" and an "application/pdf".  Within the "multipart/alternative" we are nesting a "text/html" and "text/plain" version of the same message.
  277
+
  278
+This tells the email client that each of the top level parts should be shown to the end user, however, the first part has the content type "multipart/alternative" and provides two versions of the same message, a plain text and HTML version.
  279
+
  280
+In the following example, there would be two template files, +welcome_email.text.html.erb+ and +welcome_email.text.plain.erb+ in the +app/views/user_mailer+ folder.
270 281
 
271 282
 <ruby>
272 283
 class UserMailer < ActionMailer::Base
@@ -274,22 +285,28 @@ class UserMailer < ActionMailer::Base
274 285
     recipients      user.email_address
275 286
     subject         "New account information"
276 287
     from            "system@example.com"
277  
-    content_type    "multipart/alternative"
  288
+    content_type    "multipart/mixed"
278 289
 
279  
-    part "text/html" do |p|
280  
-      p.body = render_message("welcome_email_html", :message => "<h1>HTML content</h1>")
281  
-    end
  290
+    part "multipart/alternative" do |alternative|
  291
+
  292
+      alternative.part "text/html" do |html|
  293
+        html.body = render_message("welcome_email.text.html", :message => "<h1>HTML content</h1>")
  294
+      end
  295
+
  296
+      alternative.part "text/plain" do |plain|
  297
+        plain.body = render_message("welcome_email.text.plain", :message => "text content")
  298
+      end
282 299
 
283  
-    part "text/plain" do |p|
284  
-      p.body = render_message("welcome_email_plain", :message => "text content")
285 300
     end
286 301
 
287 302
     attachment :content_type => "image/jpeg",
288 303
       :body => File.read("an-image.jpg")
289 304
 
290 305
     attachment "application/pdf" do |a|
291  
-      a.body = generate_your_pdf_here()
  306
+      pdf = generate_your_pdf_here(:name => user)
  307
+      a.body = pdf
292 308
     end
  309
+
293 310
   end
294 311
 end
295 312
 </ruby>

0 notes on commit 92fd824

Please sign in to comment.
Something went wrong with that request. Please try again.