Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 163 lines (138 sloc) 6.381 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
29 class Base
30 private_class_method :new
31
32 # Template root determines the base from which template references will be made.
33 cattr_accessor :template_root
34
35 # The logger is used for generating information on the mailing run if available.
36 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
37 cattr_accessor :logger
38
39 # 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 @@server_settings = {
48 :address => "localhost",
49 :port => 25,
50 :domain => 'localhost.localdomain',
51 :user_name => nil,
52 :password => nil,
53 :authentication => nil
54 }
55 cattr_accessor :server_settings
56
57
58 # Whether or not errors should be raised if the email fails to be delivered
59 @@raise_delivery_errors = true
60 cattr_accessor :raise_delivery_errors
61
62 # Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.
63 # Sendmail is assumed to be present at "/usr/sbin/sendmail".
64 @@delivery_method = :smtp
65 cattr_accessor :delivery_method
66
67 # Determines whether deliver_* methods are actually carried out. By default they are,
68 # but this can be turned off to help functional testing.
69 @@perform_deliveries = true
70 cattr_accessor :perform_deliveries
71
72 # Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
73 # for unit and functional testing.
74 @@deliveries = []
75 cattr_accessor :deliveries
76
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
77 attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers, :bcc, :cc
78
79 def initialize
80 @headers = {}
81 end
db045db @dhh Initial
dhh authored
82
83 class << self
84 def method_missing(method_symbol, *parameters)#:nodoc:
85 case method_symbol.id2name
86 when /^create_([_a-z]*)/
87 create_from_action($1, *parameters)
88 when /^deliver_([_a-z]*)/
89 begin
90 deliver(send("create_" + $1, *parameters))
91 rescue Object => e
92 raise e if raise_delivery_errors
93 end
94 end
95 end
96
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
97 def mail(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
98 deliver(create(to, subject, body, from, timestamp, headers))
db045db @dhh Initial
dhh authored
99 end
100
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
101 def create(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc:
db045db @dhh Initial
dhh authored
102 m = TMail::Mail.new
103 m.to, m.subject, m.body, m.from = to, subject, body, from
104 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
105 headers.each do |k, v|
106 m[k] = v
107 end
108
db045db @dhh Initial
dhh authored
109 return m
110 end
111
112 def deliver(mail) #:nodoc:
113 logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil?
114 send("perform_delivery_#{delivery_method}", mail) if perform_deliveries
115 end
116
117 private
118 def perform_delivery_smtp(mail)
119 Net::SMTP.start(server_settings[:address], server_settings[:port], server_settings[:domain],
120 server_settings[:user_name], server_settings[:password], server_settings[:authentication]) do |smtp|
121 smtp.sendmail(mail.encoded, mail.from_address, mail.destinations)
122 end
123 end
124
125 def perform_delivery_sendmail(mail)
126 IO.popen("/usr/sbin/sendmail -i -t","w+") do |sm|
127 sm.print(mail.encoded)
128 sm.flush
129 end
130 end
131
132 def perform_delivery_test(mail)
133 deliveries << mail
134 end
135
136 def create_from_action(method_name, *parameters)
137 mailer = new
138 mailer.body = {}
139 mailer.send(method_name, *parameters)
140
141 if String === mailer.body
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
142 mail = create(mailer.recipients, mailer.subject, mailer.body, mailer.from, mailer.sent_on, mailer.headers)
db045db @dhh Initial
dhh authored
143 else
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
144 mail = create(mailer.recipients, mailer.subject, render_body(mailer, method_name), mailer.from, mailer.sent_on, mailer.headers)
db045db @dhh Initial
dhh authored
145 end
146
147 mail.bcc = @bcc if @bcc
148 mail.cc = @cc if @cc
149
150 return mail
151 end
152
153 def render_body(mailer, method_name)
154 ActionView::Base.new(template_path, mailer.body).render_file(method_name)
155 end
156
157 def template_path
158 template_root + "/" + Inflector.underscore(self.to_s)
159 end
160 end
161 end
162 end
Something went wrong with that request. Please try again.