comparison app/models/.svn/text-base/mailer.rb.svn-base @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 0579821a129a
children
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
29 def self.default_url_options 29 def self.default_url_options
30 h = Setting.host_name 30 h = Setting.host_name
31 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? 31 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
32 { :host => h, :protocol => Setting.protocol } 32 { :host => h, :protocol => Setting.protocol }
33 end 33 end
34 34
35 # Builds a tmail object used to email recipients of the added issue. 35 # Builds a tmail object used to email recipients of the added issue.
36 # 36 #
37 # Example: 37 # Example:
38 # issue_add(issue) => tmail object 38 # issue_add(issue) => tmail object
39 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients 39 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients
83 set_language_if_valid user.language 83 set_language_if_valid user.language
84 recipients user.mail 84 recipients user.mail
85 subject l(:mail_subject_reminder, :count => issues.size, :days => days) 85 subject l(:mail_subject_reminder, :count => issues.size, :days => days)
86 body :issues => issues, 86 body :issues => issues,
87 :days => days, 87 :days => days,
88 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc') 88 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort => 'due_date:asc')
89 render_multipart('reminder', body) 89 render_multipart('reminder', body)
90 end 90 end
91 91
92 # Builds a tmail object used to email users belonging to the added document's project. 92 # Builds a tmail object used to email users belonging to the added document's project.
93 # 93 #
112 container = attachments.first.container 112 container = attachments.first.container
113 added_to = '' 113 added_to = ''
114 added_to_url = '' 114 added_to_url = ''
115 case container.class.name 115 case container.class.name
116 when 'Project' 116 when 'Project'
117 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container) 117 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
118 added_to = "#{l(:label_project)}: #{container}" 118 added_to = "#{l(:label_project)}: #{container}"
119 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 119 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
120 when 'Version' 120 when 'Version'
121 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) 121 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
122 added_to = "#{l(:label_version)}: #{container.name}" 122 added_to = "#{l(:label_version)}: #{container.name}"
123 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 123 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
124 when 'Document' 124 when 'Document'
125 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) 125 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
126 added_to = "#{l(:label_document)}: #{container.title}" 126 added_to = "#{l(:label_document)}: #{container.title}"
131 body :attachments => attachments, 131 body :attachments => attachments,
132 :added_to => added_to, 132 :added_to => added_to,
133 :added_to_url => added_to_url 133 :added_to_url => added_to_url
134 render_multipart('attachments_added', body) 134 render_multipart('attachments_added', body)
135 end 135 end
136 136
137 # Builds a tmail object used to email recipients of a news' project when a news item is added. 137 # Builds a tmail object used to email recipients of a news' project when a news item is added.
138 # 138 #
139 # Example: 139 # Example:
140 # news_added(news) => tmail object 140 # news_added(news) => tmail object
141 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients 141 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
147 body :news => news, 147 body :news => news,
148 :news_url => url_for(:controller => 'news', :action => 'show', :id => news) 148 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
149 render_multipart('news_added', body) 149 render_multipart('news_added', body)
150 end 150 end
151 151
152 # Builds a tmail object used to email the recipients of the specified message that was posted. 152 # Builds a tmail object used to email recipients of a news' project when a news comment is added.
153 #
154 # Example:
155 # news_comment_added(comment) => tmail object
156 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
157 def news_comment_added(comment)
158 news = comment.commented
159 redmine_headers 'Project' => news.project.identifier
160 message_id comment
161 recipients news.recipients
162 cc news.watcher_recipients
163 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
164 body :news => news,
165 :comment => comment,
166 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
167 render_multipart('news_comment_added', body)
168 end
169
170 # Builds a tmail object used to email the recipients of the specified message that was posted.
153 # 171 #
154 # Example: 172 # Example:
155 # message_posted(message) => tmail object 173 # message_posted(message) => tmail object
156 # Mailer.deliver_message_posted(message) => sends an email to the recipients 174 # Mailer.deliver_message_posted(message) => sends an email to the recipients
157 def message_posted(message) 175 def message_posted(message)
164 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" 182 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
165 body :message => message, 183 body :message => message,
166 :message_url => url_for(message.event_url) 184 :message_url => url_for(message.event_url)
167 render_multipart('message_posted', body) 185 render_multipart('message_posted', body)
168 end 186 end
169 187
170 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added. 188 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
171 # 189 #
172 # Example: 190 # Example:
173 # wiki_content_added(wiki_content) => tmail object 191 # wiki_content_added(wiki_content) => tmail object
174 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients 192 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
175 def wiki_content_added(wiki_content) 193 def wiki_content_added(wiki_content)
181 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" 199 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
182 body :wiki_content => wiki_content, 200 body :wiki_content => wiki_content,
183 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', :project_id => wiki_content.project, :id => wiki_content.page.title) 201 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', :project_id => wiki_content.project, :id => wiki_content.page.title)
184 render_multipart('wiki_content_added', body) 202 render_multipart('wiki_content_added', body)
185 end 203 end
186 204
187 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated. 205 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
188 # 206 #
189 # Example: 207 # Example:
190 # wiki_content_updated(wiki_content) => tmail object 208 # wiki_content_updated(wiki_content) => tmail object
191 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients 209 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
192 def wiki_content_updated(wiki_content) 210 def wiki_content_updated(wiki_content)
276 def deliver!(mail = @mail) 294 def deliver!(mail = @mail)
277 set_language_if_valid @initial_language 295 set_language_if_valid @initial_language
278 return false if (recipients.nil? || recipients.empty?) && 296 return false if (recipients.nil? || recipients.empty?) &&
279 (cc.nil? || cc.empty?) && 297 (cc.nil? || cc.empty?) &&
280 (bcc.nil? || bcc.empty?) 298 (bcc.nil? || bcc.empty?)
281 299
282 # Set Message-Id and References 300 # Set Message-Id and References
283 if @message_id_object 301 if @message_id_object
284 mail.message_id = self.class.message_id_for(@message_id_object) 302 mail.message_id = self.class.message_id_for(@message_id_object)
285 end 303 end
286 if @references_objects 304 if @references_objects
287 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} 305 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
288 end 306 end
289 307
290 # Log errors when raise_delivery_errors is set to false, Rails does not 308 # Log errors when raise_delivery_errors is set to false, Rails does not
291 raise_errors = self.class.raise_delivery_errors 309 raise_errors = self.class.raise_delivery_errors
292 self.class.raise_delivery_errors = true 310 self.class.raise_delivery_errors = true
293 begin 311 begin
294 return super(mail) 312 return super(mail)
324 342
325 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], 343 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
326 :conditions => s.conditions 344 :conditions => s.conditions
327 ).group_by(&:assigned_to) 345 ).group_by(&:assigned_to)
328 issues_by_assignee.each do |assignee, issues| 346 issues_by_assignee.each do |assignee, issues|
329 deliver_reminder(assignee, issues, days) unless assignee.nil? 347 deliver_reminder(assignee, issues, days) if assignee && assignee.active?
330 end 348 end
331 end 349 end
332 350
333 # Activates/desactivates email deliveries during +block+ 351 # Activates/desactivates email deliveries during +block+
334 def self.with_deliveries(enabled = true, &block) 352 def self.with_deliveries(enabled = true, &block)
335 was_enabled = ActionMailer::Base.perform_deliveries 353 was_enabled = ActionMailer::Base.perform_deliveries
336 ActionMailer::Base.perform_deliveries = !!enabled 354 ActionMailer::Base.perform_deliveries = !!enabled
337 yield 355 yield
343 def initialize_defaults(method_name) 361 def initialize_defaults(method_name)
344 super 362 super
345 @initial_language = current_language 363 @initial_language = current_language
346 set_language_if_valid Setting.default_language 364 set_language_if_valid Setting.default_language
347 from Setting.mail_from 365 from Setting.mail_from
348 366
349 # Common headers 367 # Common headers
350 headers 'X-Mailer' => 'Redmine', 368 headers 'X-Mailer' => 'Redmine',
351 'X-Redmine-Host' => Setting.host_name, 369 'X-Redmine-Host' => Setting.host_name,
352 'X-Redmine-Site' => Setting.app_title, 370 'X-Redmine-Site' => Setting.app_title,
353 'Precedence' => 'bulk', 371 'Precedence' => 'bulk',
366 @author ||= User.current 384 @author ||= User.current
367 if @author.pref[:no_self_notified] 385 if @author.pref[:no_self_notified]
368 recipients.delete(@author.mail) if recipients 386 recipients.delete(@author.mail) if recipients
369 cc.delete(@author.mail) if cc 387 cc.delete(@author.mail) if cc
370 end 388 end
371 389
372 notified_users = [recipients, cc].flatten.compact.uniq 390 notified_users = [recipients, cc].flatten.compact.uniq
373 # Rails would log recipients only, not cc and bcc 391 # Rails would log recipients only, not cc and bcc
374 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger 392 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
375 393
376 # Blind carbon copy recipients 394 # Blind carbon copy recipients
377 if Setting.bcc_recipients? 395 if Setting.bcc_recipients?
378 bcc(notified_users) 396 bcc(notified_users)
379 recipients [] 397 recipients []
380 cc [] 398 cc []
386 # layouts so this method will wrap an multipart messages with 404 # layouts so this method will wrap an multipart messages with
387 # explicit parts. 405 # explicit parts.
388 # 406 #
389 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type 407 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
390 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts 408 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
391 409
392 def render_multipart(method_name, body) 410 def render_multipart(method_name, body)
393 if Setting.plain_text_mail? 411 if Setting.plain_text_mail?
394 content_type "text/plain" 412 content_type "text/plain"
395 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb') 413 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
396 else 414 else
402 420
403 # Makes partial rendering work with Rails 1.2 (retro-compatibility) 421 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
404 def self.controller_path 422 def self.controller_path
405 '' 423 ''
406 end unless respond_to?('controller_path') 424 end unless respond_to?('controller_path')
407 425
408 # Returns a predictable Message-Id for the given object 426 # Returns a predictable Message-Id for the given object
409 def self.message_id_for(object) 427 def self.message_id_for(object)
410 # id + timestamp should reduce the odds of a collision 428 # id + timestamp should reduce the odds of a collision
411 # as far as we don't send multiple emails for the same object 429 # as far as we don't send multiple emails for the same object
412 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) 430 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
413 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" 431 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
414 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') 432 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
415 host = "#{::Socket.gethostname}.redmine" if host.empty? 433 host = "#{::Socket.gethostname}.redmine" if host.empty?
416 "<#{hash}@#{host}>" 434 "<#{hash}@#{host}>"
417 end 435 end
418 436
419 private 437 private
420 438
421 def message_id(object) 439 def message_id(object)
422 @message_id_object = object 440 @message_id_object = object
423 end 441 end
424 442
425 def references(object) 443 def references(object)
426 @references_objects ||= [] 444 @references_objects ||= []
427 @references_objects << object 445 @references_objects << object
428 end 446 end
429 447
430 def mylogger 448 def mylogger
431 RAILS_DEFAULT_LOGGER 449 RAILS_DEFAULT_LOGGER
432 end 450 end
433 end 451 end
434 452