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