1 = Action Mailer -- Easy email delivery and testing
3 Action Mailer is a framework for designing email-service layers. These layers
4 are used to consolidate code for sending out forgotten passwords, welcome
5 wishes on signup, invoices for billing, and any other use case that requires
6 a written notification to either a person or another system.
8 Additionally, an Action Mailer class can be used to process incoming email,
9 such as allowing a weblog to accept new posts from an email (which could even
10 have been sent from a phone).
14 The framework works by setting up all the email details, except the body,
15 in methods on the service layer. Subject, recipients, sender, and timestamp
16 are all set up this way. An example of such a method:
18 def signed_up(recipient)
20 subject "[Signed up] Welcome #{recipient}"
21 from "system@loudthinking.com"
22 body :recipient => recipient
25 The body of the email is created by using an Action View template (regular
26 ERb) that has the content of the body hash parameter available as instance variables.
27 So the corresponding body template for the method above could look like this:
33 And if the recipient was given as "david@loudthinking.com", the email
34 generated would look like this:
36 Date: Sun, 12 Dec 2004 00:00:00 +0100
37 From: system@loudthinking.com
38 To: david@loudthinking.com
39 Subject: [Signed up] Welcome david@loudthinking.com
43 Mr. david@loudthinking.com
45 You never actually call the instance methods like signed_up directly. Instead,
46 you call class methods like deliver_* and create_* that are automatically
47 created for each instance method. So if the signed_up method sat on
48 ApplicationMailer, it would look like this:
50 ApplicationMailer.create_signed_up("david@loudthinking.com") # => tmail object for testing
51 ApplicationMailer.deliver_signed_up("david@loudthinking.com") # sends the email
52 ApplicationMailer.new.signed_up("david@loudthinking.com") # won't work!
56 To receive emails, you need to implement a public instance method called receive that takes a
57 tmail object as its single parameter. The Action Mailer framework has a corresponding class method,
58 which is also called receive, that accepts a raw, unprocessed email as a string, which it then turns
59 into the tmail object and calls the receive instance method.
63 class Mailman < ActionMailer::Base
65 page = Page.find_by_address(email.to.first)
67 :subject => email.subject, :body => email.body
70 if email.has_attachments?
71 for attachment in email.attachments
72 page.attachments.create({
73 :file => attachment, :description => email.subject
80 This Mailman can be the target for Postfix or other MTAs. In Rails, you would use the runner in the
81 trivial case like this:
83 ./script/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.
91 The Base class has the full list of configuration options. Here's an example:
93 ActionMailer::Base.smtp_settings = {
94 :address => 'smtp.yourserver.com', # default: localhost
95 :port => '25', # default: 25
98 :authentication => :plain # :plain, :login or :cram_md5
103 Action Mailer requires that the Action Pack is either available to be required immediately
104 or is accessible as a GEM.
109 * tmail 0.10.8 by Minero Aoki released under LGPL
110 Read more on http://i.loveruby.net/en/prog/tmail.html
112 * Text::Format 0.63 by Austin Ziegler released under OpenSource
113 Read more on http://www.halostatue.ca/ruby/Text__Format.html
118 The latest version of Action Mailer can be found at
120 * http://rubyforge.org/project/showfiles.php?group_id=361
122 Documentation can be found at
124 * http://actionmailer.rubyonrails.org
129 You can install Action Mailer with the following command.
131 % [sudo] ruby install.rb
133 from its distribution directory.
138 Action Mailer is released under the MIT license.
143 The Action Mailer homepage is http://www.rubyonrails.org. You can find
144 the Action Mailer RubyForge page at http://rubyforge.org/projects/actionmailer.
145 And as Jim from Rake says:
147 Feel free to submit commits or feature requests. If you send a patch,
148 remember to update the corresponding unit tests. If fact, I prefer
149 new feature to be submitted in the form of new unit tests.