comparison app/models/mailer.rb @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents 350acce374a2
children 5e80956cc792
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
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.
104 set_language_if_valid user.language 104 set_language_if_valid user.language
105 recipients user.mail 105 recipients user.mail
106 subject l(:mail_subject_reminder, :count => issues.size, :days => days) 106 subject l(:mail_subject_reminder, :count => issues.size, :days => days)
107 body :issues => issues, 107 body :issues => issues,
108 :days => days, 108 :days => days,
109 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc') 109 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort => 'due_date:asc')
110 render_multipart('reminder', body) 110 render_multipart('reminder', body)
111 end 111 end
112 112
113 # Builds a tmail object used to email users belonging to the added document's project. 113 # Builds a tmail object used to email users belonging to the added document's project.
114 # 114 #
133 container = attachments.first.container 133 container = attachments.first.container
134 added_to = '' 134 added_to = ''
135 added_to_url = '' 135 added_to_url = ''
136 case container.class.name 136 case container.class.name
137 when 'Project' 137 when 'Project'
138 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container) 138 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
139 added_to = "#{l(:label_project)}: #{container}" 139 added_to = "#{l(:label_project)}: #{container}"
140 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 140 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
141 when 'Version' 141 when 'Version'
142 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) 142 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
143 added_to = "#{l(:label_version)}: #{container.name}" 143 added_to = "#{l(:label_version)}: #{container.name}"
144 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 144 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
145 when 'Document' 145 when 'Document'
146 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) 146 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
147 added_to = "#{l(:label_document)}: #{container.title}" 147 added_to = "#{l(:label_document)}: #{container.title}"
152 body :attachments => attachments, 152 body :attachments => attachments,
153 :added_to => added_to, 153 :added_to => added_to,
154 :added_to_url => added_to_url 154 :added_to_url => added_to_url
155 render_multipart('attachments_added', body) 155 render_multipart('attachments_added', body)
156 end 156 end
157 157
158 # Builds a tmail object used to email recipients of a news' project when a news item is added. 158 # Builds a tmail object used to email recipients of a news' project when a news item is added.
159 # 159 #
160 # Example: 160 # Example:
161 # news_added(news) => tmail object 161 # news_added(news) => tmail object
162 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients 162 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
168 body :news => news, 168 body :news => news,
169 :news_url => url_for(:controller => 'news', :action => 'show', :id => news) 169 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
170 render_multipart('news_added', body) 170 render_multipart('news_added', body)
171 end 171 end
172 172
173 # Builds a tmail object used to email the recipients of the specified message that was posted. 173 # Builds a tmail object used to email recipients of a news' project when a news comment is added.
174 #
175 # Example:
176 # news_comment_added(comment) => tmail object
177 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
178 def news_comment_added(comment)
179 news = comment.commented
180 redmine_headers 'Project' => news.project.identifier
181 message_id comment
182 recipients news.recipients
183 cc news.watcher_recipients
184 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
185 body :news => news,
186 :comment => comment,
187 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
188 render_multipart('news_comment_added', body)
189 end
190
191 # Builds a tmail object used to email the recipients of the specified message that was posted.
174 # 192 #
175 # Example: 193 # Example:
176 # message_posted(message) => tmail object 194 # message_posted(message) => tmail object
177 # Mailer.deliver_message_posted(message) => sends an email to the recipients 195 # Mailer.deliver_message_posted(message) => sends an email to the recipients
178 def message_posted(message) 196 def message_posted(message)
185 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" 203 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
186 body :message => message, 204 body :message => message,
187 :message_url => url_for(message.event_url) 205 :message_url => url_for(message.event_url)
188 render_multipart('message_posted', body) 206 render_multipart('message_posted', body)
189 end 207 end
190 208
191 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added. 209 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
192 # 210 #
193 # Example: 211 # Example:
194 # wiki_content_added(wiki_content) => tmail object 212 # wiki_content_added(wiki_content) => tmail object
195 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients 213 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
196 def wiki_content_added(wiki_content) 214 def wiki_content_added(wiki_content)
202 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" 220 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
203 body :wiki_content => wiki_content, 221 body :wiki_content => wiki_content,
204 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', :project_id => wiki_content.project, :id => wiki_content.page.title) 222 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', :project_id => wiki_content.project, :id => wiki_content.page.title)
205 render_multipart('wiki_content_added', body) 223 render_multipart('wiki_content_added', body)
206 end 224 end
207 225
208 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated. 226 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
209 # 227 #
210 # Example: 228 # Example:
211 # wiki_content_updated(wiki_content) => tmail object 229 # wiki_content_updated(wiki_content) => tmail object
212 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients 230 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
213 def wiki_content_updated(wiki_content) 231 def wiki_content_updated(wiki_content)
297 def deliver!(mail = @mail) 315 def deliver!(mail = @mail)
298 set_language_if_valid @initial_language 316 set_language_if_valid @initial_language
299 return false if (recipients.nil? || recipients.empty?) && 317 return false if (recipients.nil? || recipients.empty?) &&
300 (cc.nil? || cc.empty?) && 318 (cc.nil? || cc.empty?) &&
301 (bcc.nil? || bcc.empty?) 319 (bcc.nil? || bcc.empty?)
302 320
303 # Set Message-Id and References 321 # Set Message-Id and References
304 if @message_id_object 322 if @message_id_object
305 mail.message_id = self.class.message_id_for(@message_id_object) 323 mail.message_id = self.class.message_id_for(@message_id_object)
306 end 324 end
307 if @references_objects 325 if @references_objects
308 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} 326 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
309 end 327 end
310 328
311 # Log errors when raise_delivery_errors is set to false, Rails does not 329 # Log errors when raise_delivery_errors is set to false, Rails does not
312 raise_errors = self.class.raise_delivery_errors 330 raise_errors = self.class.raise_delivery_errors
313 self.class.raise_delivery_errors = true 331 self.class.raise_delivery_errors = true
314 begin 332 begin
315 return super(mail) 333 return super(mail)
316 rescue Exception => e 334 rescue Exception => e
317 if raise_errors 335 if raise_errors
318 raise e 336 raise e
319 elsif mylogger 337 elsif mylogger
320 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/email.yml." 338 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml."
321 end 339 end
322 ensure 340 ensure
323 self.class.raise_delivery_errors = raise_errors 341 self.class.raise_delivery_errors = raise_errors
324 end 342 end
325 end 343 end
345 363
346 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], 364 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
347 :conditions => s.conditions 365 :conditions => s.conditions
348 ).group_by(&:assigned_to) 366 ).group_by(&:assigned_to)
349 issues_by_assignee.each do |assignee, issues| 367 issues_by_assignee.each do |assignee, issues|
350 deliver_reminder(assignee, issues, days) unless assignee.nil? 368 deliver_reminder(assignee, issues, days) if assignee && assignee.active?
351 end 369 end
352 end 370 end
353 371
354 # Activates/desactivates email deliveries during +block+ 372 # Activates/desactivates email deliveries during +block+
355 def self.with_deliveries(enabled = true, &block) 373 def self.with_deliveries(enabled = true, &block)
356 was_enabled = ActionMailer::Base.perform_deliveries 374 was_enabled = ActionMailer::Base.perform_deliveries
357 ActionMailer::Base.perform_deliveries = !!enabled 375 ActionMailer::Base.perform_deliveries = !!enabled
358 yield 376 yield
364 def initialize_defaults(method_name) 382 def initialize_defaults(method_name)
365 super 383 super
366 @initial_language = current_language 384 @initial_language = current_language
367 set_language_if_valid Setting.default_language 385 set_language_if_valid Setting.default_language
368 from Setting.mail_from 386 from Setting.mail_from
369 387
370 # Common headers 388 # Common headers
371 headers 'X-Mailer' => 'Redmine', 389 headers 'X-Mailer' => 'Redmine',
372 'X-Redmine-Host' => Setting.host_name, 390 'X-Redmine-Host' => Setting.host_name,
373 'X-Redmine-Site' => Setting.app_title, 391 'X-Redmine-Site' => Setting.app_title,
374 'Precedence' => 'bulk', 392 'Precedence' => 'bulk',
387 @author ||= User.current 405 @author ||= User.current
388 if @author.pref[:no_self_notified] 406 if @author.pref[:no_self_notified]
389 recipients.delete(@author.mail) if recipients 407 recipients.delete(@author.mail) if recipients
390 cc.delete(@author.mail) if cc 408 cc.delete(@author.mail) if cc
391 end 409 end
392 410
393 notified_users = [recipients, cc].flatten.compact.uniq 411 notified_users = [recipients, cc].flatten.compact.uniq
394 # Rails would log recipients only, not cc and bcc 412 # Rails would log recipients only, not cc and bcc
395 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger 413 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
396 414
397 # Blind carbon copy recipients 415 # Blind carbon copy recipients
398 if Setting.bcc_recipients? 416 if Setting.bcc_recipients?
399 bcc(notified_users) 417 bcc(notified_users)
400 recipients [] 418 recipients []
401 cc [] 419 cc []
407 # layouts so this method will wrap an multipart messages with 425 # layouts so this method will wrap an multipart messages with
408 # explicit parts. 426 # explicit parts.
409 # 427 #
410 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type 428 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
411 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts 429 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
412 430
413 def render_multipart(method_name, body) 431 def render_multipart(method_name, body)
414 if Setting.plain_text_mail? 432 if Setting.plain_text_mail?
415 content_type "text/plain" 433 content_type "text/plain"
416 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb') 434 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
417 else 435 else
423 441
424 # Makes partial rendering work with Rails 1.2 (retro-compatibility) 442 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
425 def self.controller_path 443 def self.controller_path
426 '' 444 ''
427 end unless respond_to?('controller_path') 445 end unless respond_to?('controller_path')
428 446
429 # Returns a predictable Message-Id for the given object 447 # Returns a predictable Message-Id for the given object
430 def self.message_id_for(object) 448 def self.message_id_for(object)
431 # id + timestamp should reduce the odds of a collision 449 # id + timestamp should reduce the odds of a collision
432 # as far as we don't send multiple emails for the same object 450 # as far as we don't send multiple emails for the same object
433 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) 451 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
434 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" 452 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
435 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') 453 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
436 host = "#{::Socket.gethostname}.redmine" if host.empty? 454 host = "#{::Socket.gethostname}.redmine" if host.empty?
437 "<#{hash}@#{host}>" 455 "<#{hash}@#{host}>"
438 end 456 end
439 457
440 private 458 private
441 459
442 def message_id(object) 460 def message_id(object)
443 @message_id_object = object 461 @message_id_object = object
444 end 462 end
445 463
446 def references(object) 464 def references(object)
447 @references_objects ||= [] 465 @references_objects ||= []
448 @references_objects << object 466 @references_objects << object
449 end 467 end
450 468
451 def mylogger 469 def mylogger
452 RAILS_DEFAULT_LOGGER 470 RAILS_DEFAULT_LOGGER
453 end 471 end
454 end 472 end
455 473