Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 174 lines (150 sloc) 6.799 kb
db045db @dhh Initial
dhh authored
1 module ActionMailer #:nodoc:
2 # Usage:
3 #
4 # class ApplicationMailer < ActionMailer::Base
5 # def post_notification(recipients, post)
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
6 # @recipients = recipients
7 # @from = post.author.email_address_with_name
8 # @headers["bcc"] = SYSTEM_ADMINISTRATOR_EMAIL
9 # @headers["reply-to"] = "notifications@example.com"
10 # @subject = "[#{post.account.name} #{post.title}]"
11 # @body["post"] = post
db045db @dhh Initial
dhh authored
12 # end
13 #
14 # def comment_notification(recipient, comment)
15 # @recipients = recipient.email_address_with_name
16 # @subject = "[#{comment.post.project.client.firm.account.name}]" +
17 # " Re: #{comment.post.title}"
18 # @body["comment"] = comment
19 # @from = comment.author.email_address_with_name
20 # @sent_on = comment.posted_on
21 # end
22 # end
23 #
24 # # After this post_notification will look for "templates/application_mailer/post_notification.rhtml"
25 # ApplicationMailer.template_root = "templates"
26 #
27 # ApplicationMailer.create_comment_notification(david, hello_world) # => a tmail object
28 # ApplicationMailer.deliver_comment_notification(david, hello_world) # sends the email
c927aa0 @dhh Updated documentation
dhh authored
29 #
30 # = Configuration options
31 #
32 # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
33 #
34 # * <tt>template_root</tt> - template root determines the base from which template references will be made.
35 #
36 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
37 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
38 #
39 # * <tt>server_settings</tt> - Allows detailed configuration of the server:
40 # * <tt>:address</tt> Allows you to use a remote mail server. Just change it away from it's default "localhost" setting.
41 # * <tt>:port</tt> On the off change that your mail server doesn't run on port 25, you can change it.
42 # * <tt>:domain</tt> If you need to specify a HELO domain, you can do it here.
43 # * <tt>:user_name</tt> If your mail server requires authentication, set the username and password in these two settings.
44 # * <tt>:password</tt> If your mail server requires authentication, set the username and password in these two settings.
45 # * <tt>:authentication</tt> If your mail server requires authentication, you need to specify the authentication type here.
46 # This is a symbol and one of :plain, :login, :cram_md5
47 #
48 # * <tt>raise_delivery_errors</tt> - whether or not errors should be raised if the email fails to be delivered.
49 #
50 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.
51 # Sendmail is assumed to be present at "/usr/sbin/sendmail".
52 #
53 # * <tt>perform_deliveries</tt> - Determines whether deliver_* methods are actually carried out. By default they are,
54 # but this can be turned off to help functional testing.
55 #
56 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
57 # for unit and functional testing.
db045db @dhh Initial
dhh authored
58 class Base
c927aa0 @dhh Updated documentation
dhh authored
59 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
60
61 cattr_accessor :template_root
62 cattr_accessor :logger
63
64 @@server_settings = {
65 :address => "localhost",
66 :port => 25,
67 :domain => 'localhost.localdomain',
68 :user_name => nil,
69 :password => nil,
70 :authentication => nil
71 }
72 cattr_accessor :server_settings
73
74 @@raise_delivery_errors = true
75 cattr_accessor :raise_delivery_errors
76
77 @@delivery_method = :smtp
78 cattr_accessor :delivery_method
79
80 @@perform_deliveries = true
81 cattr_accessor :perform_deliveries
82
83 @@deliveries = []
84 cattr_accessor :deliveries
85
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
86 attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers, :bcc, :cc
87
88 def initialize
5f5b053 @dhh Fixed that bcc and cc should be settable through @bcc and @cc -- not …
dhh authored
89 @bcc = @cc = @from = @recipients = @sent_on = @subject = @body = nil
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
90 @headers = {}
91 end
db045db @dhh Initial
dhh authored
92
93 class << self
94 def method_missing(method_symbol, *parameters)#:nodoc:
95 case method_symbol.id2name
96 when /^create_([_a-z]*)/
97 create_from_action($1, *parameters)
98 when /^deliver_([_a-z]*)/
99 begin
100 deliver(send("create_" + $1, *parameters))
101 rescue Object => e
102 raise e if raise_delivery_errors
103 end
104 end
105 end
106
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
107 def mail(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
108 deliver(create(to, subject, body, from, timestamp, headers))
db045db @dhh Initial
dhh authored
109 end
110
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
111 def create(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
db045db @dhh Initial
dhh authored
112 m = TMail::Mail.new
113 m.to, m.subject, m.body, m.from = to, subject, body, from
114 m.date = timestamp.respond_to?("to_time") ? timestamp.to_time : (timestamp || Time.now)
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
115 headers.each do |k, v|
116 m[k] = v
117 end
118
db045db @dhh Initial
dhh authored
119 return m
120 end
121
122 def deliver(mail) #:nodoc:
123 logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil?
124 send("perform_delivery_#{delivery_method}", mail) if perform_deliveries
125 end
126
127 private
128 def perform_delivery_smtp(mail)
129 Net::SMTP.start(server_settings[:address], server_settings[:port], server_settings[:domain],
130 server_settings[:user_name], server_settings[:password], server_settings[:authentication]) do |smtp|
131 smtp.sendmail(mail.encoded, mail.from_address, mail.destinations)
132 end
133 end
134
135 def perform_delivery_sendmail(mail)
136 IO.popen("/usr/sbin/sendmail -i -t","w+") do |sm|
137 sm.print(mail.encoded)
138 sm.flush
139 end
140 end
141
142 def perform_delivery_test(mail)
143 deliveries << mail
144 end
145
146 def create_from_action(method_name, *parameters)
147 mailer = new
148 mailer.body = {}
149 mailer.send(method_name, *parameters)
150
5f5b053 @dhh Fixed that bcc and cc should be settable through @bcc and @cc -- not …
dhh authored
151 unless String === mailer.body then
152 mailer.body = render_body mailer, method_name
db045db @dhh Initial
dhh authored
153 end
154
5f5b053 @dhh Fixed that bcc and cc should be settable through @bcc and @cc -- not …
dhh authored
155 mail = create(mailer.recipients, mailer.subject, mailer.body,
156 mailer.from, mailer.sent_on, mailer.headers)
157
158 mail.bcc = mailer.bcc unless mailer.bcc.nil?
159 mail.cc = mailer.cc unless mailer.cc.nil?
160
db045db @dhh Initial
dhh authored
161 return mail
162 end
163
164 def render_body(mailer, method_name)
165 ActionView::Base.new(template_path, mailer.body).render_file(method_name)
166 end
167
168 def template_path
169 template_root + "/" + Inflector.underscore(self.to_s)
170 end
171 end
172 end
173 end
Something went wrong with that request. Please try again.