README.rdoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. = Action Mailer -- Easy email delivery and testing
  2. Action Mailer is a framework for designing email-service layers. These layers
  3. are used to consolidate code for sending out forgotten passwords, welcome
  4. wishes on signup, invoices for billing, and any other use case that requires
  5. a written notification to either a person or another system.
  6. Action Mailer is in essence a wrapper around Action Controller and the
  7. Mail gem. It provides a way to make emails using templates in the same
  8. way that Action Controller renders views using templates.
  9. Additionally, an Action Mailer class can be used to process incoming email,
  10. such as allowing a blog to accept new posts from an email (which could even
  11. have been sent from a phone).
  12. == Sending emails
  13. The framework works by initializing any instance variables you want to be
  14. available in the email template, followed by a call to +mail+ to deliver
  15. the email.
  16. This can be as simple as:
  17. class Notifier < ActionMailer::Base
  18. delivers_from 'system@loudthinking.com'
  19. def welcome(recipient)
  20. @recipient = recipient
  21. mail(:to => recipient,
  22. :subject => "[Signed up] Welcome #{recipient}")
  23. end
  24. end
  25. The body of the email is created by using an Action View template (regular
  26. ERB) that has the instance variables that are declared in the mailer action.
  27. So the corresponding body template for the method above could look like this:
  28. Hello there,
  29. Mr. <%= @recipient %>
  30. Thank you for signing up!
  31. And if the recipient was given as "david@loudthinking.com", the email
  32. generated would look like this:
  33. Date: Mon, 25 Jan 2010 22:48:09 +1100
  34. From: system@loudthinking.com
  35. To: david@loudthinking.com
  36. Message-ID: <4b5d84f9dd6a5_7380800b81ac29578@void.loudthinking.com.mail>
  37. Subject: [Signed up] Welcome david@loudthinking.com
  38. Mime-Version: 1.0
  39. Content-Type: text/plain;
  40. charset="US-ASCII";
  41. Content-Transfer-Encoding: 7bit
  42. Hello there,
  43. Mr. david@loudthinking.com
  44. Thank you for signing up!
  45. In previous version of Rails you would call <tt>create_method_name</tt> and
  46. <tt>deliver_method_name</tt>. Rails 3.0 has a much simpler interface, you
  47. simply call the method and optionally call +deliver+ on the return value.
  48. Calling the method returns a Mail Message object:
  49. message = Notifier.welcome # => Returns a Mail::Message object
  50. message.deliver # => delivers the email
  51. Or you can just chain the methods together like:
  52. Notifier.welcome.deliver # Creates the email and sends it immediately
  53. == Setting defaults
  54. It is possible to set default values that will be used in every method in your Action Mailer class. To implement this functionality, you just call the public class method <tt>default</tt> which you get for free from ActionMailer::Base. This method accepts a Hash as the parameter. You can use any of the headers e-mail messages has, like <tt>:from</tt> as the key. You can also pass in a string as the key, like "Content-Type", but Action Mailer does this out of the box for you, so you won't need to worry about that. Finally it is also possible to pass in a Proc that will get evaluated when it is needed.
  55. Note that every value you set with this method will get over written if you use the same key in your mailer method.
  56. Example:
  57. class Authenticationmailer < ActionMailer::Base
  58. default :from => "awesome@application.com", :subject => Proc.new { "E-mail was generated at #{Time.now}" }
  59. .....
  60. end
  61. == Receiving emails
  62. To receive emails, you need to implement a public instance method called <tt>receive</tt> that takes an
  63. email object as its single parameter. The Action Mailer framework has a corresponding class method,
  64. which is also called <tt>receive</tt>, that accepts a raw, unprocessed email as a string, which it then turns
  65. into the email object and calls the receive instance method.
  66. Example:
  67. class Mailman < ActionMailer::Base
  68. def receive(email)
  69. page = Page.find_by_address(email.to.first)
  70. page.emails.create(
  71. :subject => email.subject, :body => email.body
  72. )
  73. if email.has_attachments?
  74. email.attachments.each do |attachment|
  75. page.attachments.create({
  76. :file => attachment, :description => email.subject
  77. })
  78. end
  79. end
  80. end
  81. end
  82. This Mailman can be the target for Postfix or other MTAs. In Rails, you would use the runner in the
  83. trivial case like this:
  84. rails runner 'Mailman.receive(STDIN.read)'
  85. However, invoking Rails in the runner for each mail to be received is very resource intensive. A single
  86. instance of Rails should be run within a daemon, if it is going to be utilized to process more than just
  87. a limited number of email.
  88. == Configuration
  89. The Base class has the full list of configuration options. Here's an example:
  90. ActionMailer::Base.smtp_settings = {
  91. :address => 'smtp.yourserver.com', # default: localhost
  92. :port => '25', # default: 25
  93. :user_name => 'user',
  94. :password => 'pass',
  95. :authentication => :plain # :plain, :login or :cram_md5
  96. }
  97. == Download and installation
  98. The latest version of Action Mailer can be installed with RubyGems:
  99. % [sudo] gem install actionmailer
  100. Source code can be downloaded as part of the Rails project on GitHub
  101. * https://github.com/rails/rails/tree/3-2-stable/actionmailer
  102. == License
  103. Action Mailer is released under the MIT license.
  104. == Support
  105. API documentation is at
  106. * http://api.rubyonrails.org
  107. Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
  108. * https://github.com/rails/rails/issues