Skip to content

HTTPS clone URL

Subversion checkout URL

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