##// END OF EJS Templates
code layout clean up of render_multipart() at app/models/mailer.rb...
Toshi MARUYAMA -
r7346:cf7efe3f7d53
parent child
Show More
@@ -1,468 +1,473
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'ar_condition'
19 19
20 20 class Mailer < ActionMailer::Base
21 21 layout 'mailer'
22 22 helper :application
23 23 helper :issues
24 24 helper :custom_fields
25 25
26 26 include ActionController::UrlWriter
27 27 include Redmine::I18n
28 28
29 29 def self.default_url_options
30 30 h = Setting.host_name
31 31 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
32 32 { :host => h, :protocol => Setting.protocol }
33 33 end
34 34
35 35 # Builds a tmail object used to email recipients of the added issue.
36 36 #
37 37 # Example:
38 38 # issue_add(issue) => tmail object
39 39 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients
40 40 def issue_add(issue)
41 41 redmine_headers 'Project' => issue.project.identifier,
42 42 'Issue-Id' => issue.id,
43 43 'Issue-Author' => issue.author.login
44 44 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
45 45 message_id issue
46 46 recipients issue.recipients
47 47 cc(issue.watcher_recipients - @recipients)
48 48 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
49 49 body :issue => issue,
50 50 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
51 51 render_multipart('issue_add', body)
52 52 end
53 53
54 54 # Builds a tmail object used to email recipients of the edited issue.
55 55 #
56 56 # Example:
57 57 # issue_edit(journal) => tmail object
58 58 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients
59 59 def issue_edit(journal)
60 60 issue = journal.journalized.reload
61 61 redmine_headers 'Project' => issue.project.identifier,
62 62 'Issue-Id' => issue.id,
63 63 'Issue-Author' => issue.author.login
64 64 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
65 65 message_id journal
66 66 references issue
67 67 @author = journal.user
68 68 recipients issue.recipients
69 69 # Watchers in cc
70 70 cc(issue.watcher_recipients - @recipients)
71 71 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
72 72 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
73 73 s << issue.subject
74 74 subject s
75 75 body :issue => issue,
76 76 :journal => journal,
77 77 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
78 78
79 79 render_multipart('issue_edit', body)
80 80 end
81 81
82 82 def reminder(user, issues, days)
83 83 set_language_if_valid user.language
84 84 recipients user.mail
85 85 subject l(:mail_subject_reminder, :count => issues.size, :days => days)
86 86 body :issues => issues,
87 87 :days => days,
88 88 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort => 'due_date:asc')
89 89 render_multipart('reminder', body)
90 90 end
91 91
92 92 # Builds a tmail object used to email users belonging to the added document's project.
93 93 #
94 94 # Example:
95 95 # document_added(document) => tmail object
96 96 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients
97 97 def document_added(document)
98 98 redmine_headers 'Project' => document.project.identifier
99 99 recipients document.recipients
100 100 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
101 101 body :document => document,
102 102 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
103 103 render_multipart('document_added', body)
104 104 end
105 105
106 106 # Builds a tmail object used to email recipients of a project when an attachements are added.
107 107 #
108 108 # Example:
109 109 # attachments_added(attachments) => tmail object
110 110 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients
111 111 def attachments_added(attachments)
112 112 container = attachments.first.container
113 113 added_to = ''
114 114 added_to_url = ''
115 115 case container.class.name
116 116 when 'Project'
117 117 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
118 118 added_to = "#{l(:label_project)}: #{container}"
119 119 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
120 120 when 'Version'
121 121 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
122 122 added_to = "#{l(:label_version)}: #{container.name}"
123 123 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
124 124 when 'Document'
125 125 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
126 126 added_to = "#{l(:label_document)}: #{container.title}"
127 127 recipients container.recipients
128 128 end
129 129 redmine_headers 'Project' => container.project.identifier
130 130 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
131 131 body :attachments => attachments,
132 132 :added_to => added_to,
133 133 :added_to_url => added_to_url
134 134 render_multipart('attachments_added', body)
135 135 end
136 136
137 137 # Builds a tmail object used to email recipients of a news' project when a news item is added.
138 138 #
139 139 # Example:
140 140 # news_added(news) => tmail object
141 141 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
142 142 def news_added(news)
143 143 redmine_headers 'Project' => news.project.identifier
144 144 message_id news
145 145 recipients news.recipients
146 146 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
147 147 body :news => news,
148 148 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
149 149 render_multipart('news_added', body)
150 150 end
151 151
152 152 # Builds a tmail object used to email recipients of a news' project when a news comment is added.
153 153 #
154 154 # Example:
155 155 # news_comment_added(comment) => tmail object
156 156 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
157 157 def news_comment_added(comment)
158 158 news = comment.commented
159 159 redmine_headers 'Project' => news.project.identifier
160 160 message_id comment
161 161 recipients news.recipients
162 162 cc news.watcher_recipients
163 163 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
164 164 body :news => news,
165 165 :comment => comment,
166 166 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
167 167 render_multipart('news_comment_added', body)
168 168 end
169 169
170 170 # Builds a tmail object used to email the recipients of the specified message that was posted.
171 171 #
172 172 # Example:
173 173 # message_posted(message) => tmail object
174 174 # Mailer.deliver_message_posted(message) => sends an email to the recipients
175 175 def message_posted(message)
176 176 redmine_headers 'Project' => message.project.identifier,
177 177 'Topic-Id' => (message.parent_id || message.id)
178 178 message_id message
179 179 references message.parent unless message.parent.nil?
180 180 recipients(message.recipients)
181 181 cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients)
182 182 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
183 183 body :message => message,
184 184 :message_url => url_for(message.event_url)
185 185 render_multipart('message_posted', body)
186 186 end
187 187
188 188 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
189 189 #
190 190 # Example:
191 191 # wiki_content_added(wiki_content) => tmail object
192 192 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
193 193 def wiki_content_added(wiki_content)
194 194 redmine_headers 'Project' => wiki_content.project.identifier,
195 195 'Wiki-Page-Id' => wiki_content.page.id
196 196 message_id wiki_content
197 197 recipients wiki_content.recipients
198 198 cc(wiki_content.page.wiki.watcher_recipients - recipients)
199 199 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
200 200 body :wiki_content => wiki_content,
201 201 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
202 202 :project_id => wiki_content.project,
203 203 :id => wiki_content.page.title)
204 204 render_multipart('wiki_content_added', body)
205 205 end
206 206
207 207 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
208 208 #
209 209 # Example:
210 210 # wiki_content_updated(wiki_content) => tmail object
211 211 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
212 212 def wiki_content_updated(wiki_content)
213 213 redmine_headers 'Project' => wiki_content.project.identifier,
214 214 'Wiki-Page-Id' => wiki_content.page.id
215 215 message_id wiki_content
216 216 recipients wiki_content.recipients
217 217 cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients)
218 218 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
219 219 body :wiki_content => wiki_content,
220 220 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
221 221 :project_id => wiki_content.project,
222 222 :id => wiki_content.page.title),
223 223 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff',
224 224 :project_id => wiki_content.project, :id => wiki_content.page.title,
225 225 :version => wiki_content.version)
226 226 render_multipart('wiki_content_updated', body)
227 227 end
228 228
229 229 # Builds a tmail object used to email the specified user their account information.
230 230 #
231 231 # Example:
232 232 # account_information(user, password) => tmail object
233 233 # Mailer.deliver_account_information(user, password) => sends account information to the user
234 234 def account_information(user, password)
235 235 set_language_if_valid user.language
236 236 recipients user.mail
237 237 subject l(:mail_subject_register, Setting.app_title)
238 238 body :user => user,
239 239 :password => password,
240 240 :login_url => url_for(:controller => 'account', :action => 'login')
241 241 render_multipart('account_information', body)
242 242 end
243 243
244 244 # Builds a tmail object used to email all active administrators of an account activation request.
245 245 #
246 246 # Example:
247 247 # account_activation_request(user) => tmail object
248 248 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
249 249 def account_activation_request(user)
250 250 # Send the email to all active administrators
251 251 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
252 252 subject l(:mail_subject_account_activation_request, Setting.app_title)
253 253 body :user => user,
254 254 :url => url_for(:controller => 'users', :action => 'index',
255 255 :status => User::STATUS_REGISTERED,
256 256 :sort_key => 'created_on', :sort_order => 'desc')
257 257 render_multipart('account_activation_request', body)
258 258 end
259 259
260 260 # Builds a tmail object used to email the specified user that their account was activated by an administrator.
261 261 #
262 262 # Example:
263 263 # account_activated(user) => tmail object
264 264 # Mailer.deliver_account_activated(user) => sends an email to the registered user
265 265 def account_activated(user)
266 266 set_language_if_valid user.language
267 267 recipients user.mail
268 268 subject l(:mail_subject_register, Setting.app_title)
269 269 body :user => user,
270 270 :login_url => url_for(:controller => 'account', :action => 'login')
271 271 render_multipart('account_activated', body)
272 272 end
273 273
274 274 def lost_password(token)
275 275 set_language_if_valid(token.user.language)
276 276 recipients token.user.mail
277 277 subject l(:mail_subject_lost_password, Setting.app_title)
278 278 body :token => token,
279 279 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
280 280 render_multipart('lost_password', body)
281 281 end
282 282
283 283 def register(token)
284 284 set_language_if_valid(token.user.language)
285 285 recipients token.user.mail
286 286 subject l(:mail_subject_register, Setting.app_title)
287 287 body :token => token,
288 288 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
289 289 render_multipart('register', body)
290 290 end
291 291
292 292 def test(user)
293 293 set_language_if_valid(user.language)
294 294 recipients user.mail
295 295 subject 'Redmine test'
296 296 body :url => url_for(:controller => 'welcome')
297 297 render_multipart('test', body)
298 298 end
299 299
300 300 # Overrides default deliver! method to prevent from sending an email
301 301 # with no recipient, cc or bcc
302 302 def deliver!(mail = @mail)
303 303 set_language_if_valid @initial_language
304 304 return false if (recipients.nil? || recipients.empty?) &&
305 305 (cc.nil? || cc.empty?) &&
306 306 (bcc.nil? || bcc.empty?)
307 307
308 308 # Set Message-Id and References
309 309 if @message_id_object
310 310 mail.message_id = self.class.message_id_for(@message_id_object)
311 311 end
312 312 if @references_objects
313 313 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
314 314 end
315 315
316 316 # Log errors when raise_delivery_errors is set to false, Rails does not
317 317 raise_errors = self.class.raise_delivery_errors
318 318 self.class.raise_delivery_errors = true
319 319 begin
320 320 return super(mail)
321 321 rescue Exception => e
322 322 if raise_errors
323 323 raise e
324 324 elsif mylogger
325 325 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml."
326 326 end
327 327 ensure
328 328 self.class.raise_delivery_errors = raise_errors
329 329 end
330 330 end
331 331
332 332 # Sends reminders to issue assignees
333 333 # Available options:
334 334 # * :days => how many days in the future to remind about (defaults to 7)
335 335 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
336 336 # * :project => id or identifier of project to process (defaults to all projects)
337 337 # * :users => array of user ids who should be reminded
338 338 def self.reminders(options={})
339 339 days = options[:days] || 7
340 340 project = options[:project] ? Project.find(options[:project]) : nil
341 341 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
342 342 user_ids = options[:users]
343 343
344 344 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
345 345 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
346 346 s << ["#{Issue.table_name}.assigned_to_id IN (?)", user_ids] if user_ids.present?
347 347 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
348 348 s << "#{Issue.table_name}.project_id = #{project.id}" if project
349 349 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
350 350
351 351 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
352 352 :conditions => s.conditions
353 353 ).group_by(&:assigned_to)
354 354 issues_by_assignee.each do |assignee, issues|
355 355 deliver_reminder(assignee, issues, days) if assignee && assignee.active?
356 356 end
357 357 end
358 358
359 359 # Activates/desactivates email deliveries during +block+
360 360 def self.with_deliveries(enabled = true, &block)
361 361 was_enabled = ActionMailer::Base.perform_deliveries
362 362 ActionMailer::Base.perform_deliveries = !!enabled
363 363 yield
364 364 ensure
365 365 ActionMailer::Base.perform_deliveries = was_enabled
366 366 end
367 367
368 368 private
369 369 def initialize_defaults(method_name)
370 370 super
371 371 @initial_language = current_language
372 372 set_language_if_valid Setting.default_language
373 373 from Setting.mail_from
374 374
375 375 # Common headers
376 376 headers 'X-Mailer' => 'Redmine',
377 377 'X-Redmine-Host' => Setting.host_name,
378 378 'X-Redmine-Site' => Setting.app_title,
379 379 'Precedence' => 'bulk',
380 380 'Auto-Submitted' => 'auto-generated'
381 381 end
382 382
383 383 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
384 384 def redmine_headers(h)
385 385 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
386 386 end
387 387
388 388 # Overrides the create_mail method
389 389 def create_mail
390 390 # Removes the current user from the recipients and cc
391 391 # if he doesn't want to receive notifications about what he does
392 392 @author ||= User.current
393 393 if @author.pref[:no_self_notified]
394 394 recipients.delete(@author.mail) if recipients
395 395 cc.delete(@author.mail) if cc
396 396 end
397 397
398 398 notified_users = [recipients, cc].flatten.compact.uniq
399 399 # Rails would log recipients only, not cc and bcc
400 400 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
401 401
402 402 # Blind carbon copy recipients
403 403 if Setting.bcc_recipients?
404 404 bcc(notified_users)
405 405 recipients []
406 406 cc []
407 407 end
408 408 super
409 409 end
410 410
411 411 # Rails 2.3 has problems rendering implicit multipart messages with
412 412 # layouts so this method will wrap an multipart messages with
413 413 # explicit parts.
414 414 #
415 415 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
416 416 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
417 417
418 418 def render_multipart(method_name, body)
419 419 if Setting.plain_text_mail?
420 420 content_type "text/plain"
421 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
421 body render(:file => "#{method_name}.text.plain.rhtml",
422 :body => body,
423 :layout => 'mailer.text.plain.erb')
422 424 else
423 425 content_type "multipart/alternative"
424 part :content_type => "text/plain", :body => render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
425 part :content_type => "text/html", :body => render_message("#{method_name}.text.html.rhtml", body)
426 part :content_type => "text/plain",
427 :body => render(:file => "#{method_name}.text.plain.rhtml",
428 :body => body, :layout => 'mailer.text.plain.erb')
429 part :content_type => "text/html",
430 :body => render_message("#{method_name}.text.html.rhtml", body)
426 431 end
427 432 end
428 433
429 434 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
430 435 def self.controller_path
431 436 ''
432 437 end unless respond_to?('controller_path')
433 438
434 439 # Returns a predictable Message-Id for the given object
435 440 def self.message_id_for(object)
436 441 # id + timestamp should reduce the odds of a collision
437 442 # as far as we don't send multiple emails for the same object
438 443 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
439 444 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
440 445 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
441 446 host = "#{::Socket.gethostname}.redmine" if host.empty?
442 447 "<#{hash}@#{host}>"
443 448 end
444 449
445 450 private
446 451
447 452 def message_id(object)
448 453 @message_id_object = object
449 454 end
450 455
451 456 def references(object)
452 457 @references_objects ||= []
453 458 @references_objects << object
454 459 end
455 460
456 461 def mylogger
457 462 RAILS_DEFAULT_LOGGER
458 463 end
459 464 end
460 465
461 466 # Patch TMail so that message_id is not overwritten
462 467 module TMail
463 468 class Mail
464 469 def add_message_id( fqdn = nil )
465 470 self.message_id ||= ::TMail::new_message_id(fqdn)
466 471 end
467 472 end
468 473 end
General Comments 0
You need to be logged in to leave comments. Login now