From cfddbcd8e7a1d965c937c04bbf6112b60d1bb962 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 31 Jan 2009 21:25:15 +0000 Subject: Regenerate guides html --- railties/doc/guides/html/action_mailer_basics.html | 237 ++++++++++++++++++--- 1 file changed, 209 insertions(+), 28 deletions(-) (limited to 'railties/doc/guides/html/action_mailer_basics.html') diff --git a/railties/doc/guides/html/action_mailer_basics.html b/railties/doc/guides/html/action_mailer_basics.html index c788591f72..22b575af45 100644 --- a/railties/doc/guides/html/action_mailer_basics.html +++ b/railties/doc/guides/html/action_mailer_basics.html @@ -47,8 +47,12 @@
  • Action Mailer Layouts
  • +
  • Generating URL’s in Action Mailer views
  • +
  • Sending multipart emails
  • +
  • Sending emails with attachments
  • +
  • @@ -75,9 +79,6 @@
  • Epilogue
  • -
  • - Changelog -
  • @@ -85,19 +86,19 @@

    Action Mailer Basics

    -

    This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.

    +

    This guide should provide you with all you need to get started in sending and receiving emails from/to your application, and many internals of the ActionMailer class. It will also cover how to test your mailers.

    1. Introduction

    Action Mailer allows you to send email from your application using a mailer model and views. -Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.

    +Yes, that is correct, in Rails, emails are used by creating models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.

    2. Sending Emails

    Let’s say you want to send a welcome email to a user after they signup. Here is how you would go about this:

    2.1. Walkthrough to generating a Mailer

    -

    2.1.1. 1. Create the mailer:

    +

    2.1.1. Create the mailer:

    end end
    -

    So what do we have here? -recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"] -from: Who the email will appear to come from in the recipients' mailbox -subject: The subject of the email -sent_on: Timestamp for the email -content_type: The content type, by default is text/plain

    +

    So what do we have here?

    +
    + +++ + + + + + + + + + + + + + + + + + + + + + +

    recipients

    who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]

    from

    Who the email will appear to come from in the recipients' mailbox

    subject

    The subject of the email

    sent_on

    Timestamp for the email

    content_type

    The content type, by default is text/plain

    +

    How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.

    -

    2.1.3. 3. Create the mailer view

    +

    2.1.3. Create the mailer view

    The file can look like:

    @@ -160,7 +188,6 @@ http://www.gnu.org/software/src-highlite --> </head> <body> <h1>Welcome to example.com, <%= @user.first_name %></h1> - <p> You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/> To login to the site, just follow this link: <%= @url %>. @@ -168,9 +195,10 @@ http://www.gnu.org/software/src-highlite --> <p>Thanks for joining and have a great day!</p> </body> </html>
    -

    2.1.4. 4. Wire it up so that the system sends the email when a user signs up

    -

    There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it’s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.

    -

    +

    2.1.4. Wire it up so that the system sends the email when a user signs up

    +

    There are 3 ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it’s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.

    +

    Let’s see how we would go about wiring it up using an observer:

    +

    In config/environment.rb:

    else super end end
    -

    Ah, this makes things so much clearer :) so if the method name starts with deliver_ followed by any combination of lowercase letters or underscore, method missing calls new on your mailer class UserMailer in our example above, sending the combination of lower case letters or underscore, along with the parameter. The resulting object is then sent the deliver! method, which well... delivers it.

    +

    Ah, this makes things so much clearer :) so if the method name starts with deliver_ followed by any combination of lowercase letters or underscore, method missing calls new on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameter. The resulting object is then sent the deliver! method, which well... delivers it.

    2.3. Complete List of ActionMailer user-settable attributes

    end

    2.5. Action Mailer Layouts

    -

    Just like controller views, you can also have mailer layouts. The layout needs end in _mailer to be automatically recognized by your mailer as a layout. So in our UserMailer example, we need to call our layout user_mailer.[html,txt].erb. In order to use a different file just use:

    +

    Just like controller views, you can also have mailer layouts. The layout name needs to end in _mailer to be automatically recognized by your mailer as a layout. So in our UserMailer example, we need to call our layout user_mailer.[html,txt].erb. In order to use a different file just use:

    end

    Just like with controller views, use yield to render the view inside the layout.

    -

    2.6. Sending multipart emails

    -

    Coming soon!

    +

    2.6. Generating URL’s in Action Mailer views

    +

    URLs can be generated in mailer views using url_for or named routes. +Unlike controllers from Action Pack, the mailer instance doesn’t have any context about the incoming request, so you’ll need to provide all of the details needed to generate a URL.

    +

    When using url_for you’ll need to provide the :host, :controller, and :action:

    +
    +
    +
    <%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
    +
    +

    When using named routes you only need to supply the :host:

    +
    +
    +
    <%= users_url(:host => "example.com") %>
    +
    +

    You will want to avoid using the name_of_route_path form of named routes because it doesn’t make sense to generate relative URLs in email messages. The reason that it doesn’t make sense is because the email is opened on a mail client outside of your environment. Since the email is not being served by your server, a URL like "/users/show/1", will have no context. In order for the email client to properly link to a URL on your server it needs something like "http://yourserver.com/users/show/1".

    +

    It is also possible to set a default host that will be used in all mailers by setting the :host option in +the ActionMailer::Base.default_url_options hash as follows:

    +
    +
    +
    ActionMailer::Base.default_url_options[:host] = "example.com"
    +
    +

    This can also be set as a configuration option in config/environment.rb:

    +
    +
    +
    config.action_mailer.default_url_options = { :host => "example.com" }
    +
    +

    If you do decide to set a default :host for your mailers you will want to use the :only_path => false option when using url_for. This will ensure that absolute URLs are generated because the url_for view helper will, by default, generate relative URLs when a :host option isn’t explicitly provided.

    +

    2.7. Sending multipart emails

    +

    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.txt.erb and welcome_email.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.

    +

    To explicitly specify multipart messages, you can do something like:

    +
    +
    +
    class UserMailer < ActionMailer::Base
    +
    +  def welcome_email(user)
    +    recipients      user.email_address
    +    subject         "New account information"
    +    from            "system@example.com"
    +    content_type    "multipart/alternative"
    +
    +    part :content_type => "text/html",
    +      :body => "<p>html content, can also be the name of an action that you call<p>"
    +
    +    part "text/plain" do |p|
    +      p.body = "text content, can also be the name of an action that you call"
    +    end
    +  end
    +
    +end
    +

    2.8. Sending emails with attachments

    +

    Attachments can be added by using the attachment method:

    +
    +
    +
    class UserMailer < ActionMailer::Base
    +
    +  def welcome_email(user)
    +    recipients      user.email_address
    +    subject         "New account information"
    +    from            "system@example.com"
    +    content_type    "multipart/alternative"
    +
    +    attachment :content_type => "image/jpeg",
    +      :body => File.read("an-image.jpg")
    +
    +    attachment "application/pdf" do |a|
    +      a.body = generate_your_pdf_here()
    +    end
    +  end
    +
    +end

    3. Receiving Emails

    +

    Receiving and parsing emails with Action Mailer can be a rather complex endeavour. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that. +So, to receive emails in your Rails app you’ll need:

    +
      +
    1. +

      +Configure your email server to forward emails from the address(es) you would like your app to receive to /path/to/app/script/runner 'UserMailer.receive(STDIN.read)' +

      +
    2. +
    3. +

      +Implement a receive method in your mailer +

      +
    4. +
    +

    Once a method called receive is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer object‘s receive method. Here’s an example:

    +
    +
    +
    class UserMailer < ActionMailer::Base
    +
    +  def receive(email)
    +    page = Page.find_by_address(email.to.first)
    +    page.emails.create(
    +      :subject => email.subject,
    +      :body => email.body
    +    )
    +
    +    if email.has_attachments?
    +      for attachment in email.attachments
    +        page.attachments.create({
    +          :file => attachment,
    +          :description => email.subject
    +        })
    +      end
    +    end
    +  end
    +
    +
    +end

    4. Using Action Mailer Helpers

    +

    Action Mailer classes have 4 helper methods available to them:

    +
    +
    +++ + + + + + + + + + + + + + + + + + +

    add_template_helper(helper_module)

    Makes all the (instance) methods in the helper module available to templates rendered through this controller.

    helper(*args, &block)

    Declare a helper: + helper :foo +requires foo_helper and includes FooHelper in the template class. + helper FooHelper +includes FooHelper in the template class. +evaluates the block in the template class, adding method foo. + helper(:three, BlindHelper) { def mice() mice end } +does all three.

    helper_method

    Declare a controller method as a helper. For example, + helper_method :link_to + def link_to(name, options) ... end +makes the link_to controller method available in the view.

    helper_attr

    Declare a controller attribute as a helper. For example, + helper_attr :name + attr_accessor :name +makes the name and name= controller methods available in the view. +The is a convenience wrapper for helper_method.

    +

    5. Action Mailer Configuration

    @@ -546,10 +731,6 @@ http://www.gnu.org/software/src-highlite -->

    7. Epilogue

    This guide presented how to create a mailer and how to test it. In reality, you may find that writing your tests before you actually write your code to be a rewarding experience. It may take some time to get used to TDD (Test Driven Development), but coding this way achieves two major benefits. Firstly, you know that the code does indeed work, because the tests fail (because there’s no code), then they pass, because the code that satisfies the tests was written. Secondly, when you start with the tests, you don’t have to make time AFTER you write the code, to write the tests, then never get around to it. The tests are already there and testing has now become part of your coding regimen.

    -
    -

    8. Changelog

    -
    -- cgit v1.2.3