annotate .svn/pristine/ed/edac928e7280cbae74255ddecf1e0d7b4a436c30.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 class Mailer < ActionMailer::Base
Chris@1494 19 layout 'mailer'
Chris@1494 20 helper :application
Chris@1494 21 helper :issues
Chris@1494 22 helper :custom_fields
Chris@1494 23
Chris@1494 24 include Redmine::I18n
Chris@1494 25
Chris@1494 26 def self.default_url_options
Chris@1494 27 { :host => Setting.host_name, :protocol => Setting.protocol }
Chris@1494 28 end
Chris@1494 29
Chris@1494 30 # Builds a mail for notifying to_users and cc_users about a new issue
Chris@1494 31 def issue_add(issue, to_users, cc_users)
Chris@1494 32 redmine_headers 'Project' => issue.project.identifier,
Chris@1494 33 'Issue-Id' => issue.id,
Chris@1494 34 'Issue-Author' => issue.author.login
Chris@1494 35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
Chris@1494 36 message_id issue
Chris@1494 37 references issue
Chris@1494 38 @author = issue.author
Chris@1494 39 @issue = issue
Chris@1494 40 @users = to_users + cc_users
Chris@1494 41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
Chris@1494 42 mail :to => to_users.map(&:mail),
Chris@1494 43 :cc => cc_users.map(&:mail),
Chris@1494 44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
Chris@1494 45 end
Chris@1494 46
Chris@1494 47 # Notifies users about a new issue
Chris@1494 48 def self.deliver_issue_add(issue)
Chris@1494 49 to = issue.notified_users
Chris@1494 50 cc = issue.notified_watchers - to
Chris@1494 51 issue.each_notification(to + cc) do |users|
Chris@1494 52 Mailer.issue_add(issue, to & users, cc & users).deliver
Chris@1494 53 end
Chris@1494 54 end
Chris@1494 55
Chris@1494 56 # Builds a mail for notifying to_users and cc_users about an issue update
Chris@1494 57 def issue_edit(journal, to_users, cc_users)
Chris@1494 58 issue = journal.journalized
Chris@1494 59 redmine_headers 'Project' => issue.project.identifier,
Chris@1494 60 'Issue-Id' => issue.id,
Chris@1494 61 'Issue-Author' => issue.author.login
Chris@1494 62 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
Chris@1494 63 message_id journal
Chris@1494 64 references issue
Chris@1494 65 @author = journal.user
Chris@1494 66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
Chris@1494 67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
Chris@1494 68 s << issue.subject
Chris@1494 69 @issue = issue
Chris@1494 70 @users = to_users + cc_users
Chris@1494 71 @journal = journal
Chris@1494 72 @journal_details = journal.visible_details(@users.first)
Chris@1494 73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
Chris@1494 74 mail :to => to_users.map(&:mail),
Chris@1494 75 :cc => cc_users.map(&:mail),
Chris@1494 76 :subject => s
Chris@1494 77 end
Chris@1494 78
Chris@1494 79 # Notifies users about an issue update
Chris@1494 80 def self.deliver_issue_edit(journal)
Chris@1494 81 issue = journal.journalized.reload
Chris@1494 82 to = journal.notified_users
Chris@1494 83 cc = journal.notified_watchers
Chris@1494 84 journal.each_notification(to + cc) do |users|
Chris@1494 85 issue.each_notification(users) do |users2|
Chris@1494 86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
Chris@1494 87 end
Chris@1494 88 end
Chris@1494 89 end
Chris@1494 90
Chris@1494 91 def reminder(user, issues, days)
Chris@1494 92 set_language_if_valid user.language
Chris@1494 93 @issues = issues
Chris@1494 94 @days = days
Chris@1494 95 @issues_url = url_for(:controller => 'issues', :action => 'index',
Chris@1494 96 :set_filter => 1, :assigned_to_id => user.id,
Chris@1494 97 :sort => 'due_date:asc')
Chris@1494 98 mail :to => user.mail,
Chris@1494 99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
Chris@1494 100 end
Chris@1494 101
Chris@1494 102 # Builds a Mail::Message object used to email users belonging to the added document's project.
Chris@1494 103 #
Chris@1494 104 # Example:
Chris@1494 105 # document_added(document) => Mail::Message object
Chris@1494 106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
Chris@1494 107 def document_added(document)
Chris@1494 108 redmine_headers 'Project' => document.project.identifier
Chris@1494 109 @author = User.current
Chris@1494 110 @document = document
Chris@1494 111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
Chris@1494 112 mail :to => document.recipients,
Chris@1494 113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
Chris@1494 114 end
Chris@1494 115
Chris@1494 116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
Chris@1494 117 #
Chris@1494 118 # Example:
Chris@1494 119 # attachments_added(attachments) => Mail::Message object
Chris@1494 120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
Chris@1494 121 def attachments_added(attachments)
Chris@1494 122 container = attachments.first.container
Chris@1494 123 added_to = ''
Chris@1494 124 added_to_url = ''
Chris@1494 125 @author = attachments.first.author
Chris@1494 126 case container.class.name
Chris@1494 127 when 'Project'
Chris@1494 128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
Chris@1494 129 added_to = "#{l(:label_project)}: #{container}"
Chris@1494 130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
Chris@1494 131 when 'Version'
Chris@1494 132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
Chris@1494 133 added_to = "#{l(:label_version)}: #{container.name}"
Chris@1494 134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
Chris@1494 135 when 'Document'
Chris@1494 136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
Chris@1494 137 added_to = "#{l(:label_document)}: #{container.title}"
Chris@1494 138 recipients = container.recipients
Chris@1494 139 end
Chris@1494 140 redmine_headers 'Project' => container.project.identifier
Chris@1494 141 @attachments = attachments
Chris@1494 142 @added_to = added_to
Chris@1494 143 @added_to_url = added_to_url
Chris@1494 144 mail :to => recipients,
Chris@1494 145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
Chris@1494 146 end
Chris@1494 147
Chris@1494 148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
Chris@1494 149 #
Chris@1494 150 # Example:
Chris@1494 151 # news_added(news) => Mail::Message object
Chris@1494 152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
Chris@1494 153 def news_added(news)
Chris@1494 154 redmine_headers 'Project' => news.project.identifier
Chris@1494 155 @author = news.author
Chris@1494 156 message_id news
Chris@1494 157 references news
Chris@1494 158 @news = news
Chris@1494 159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
Chris@1494 160 mail :to => news.recipients,
Chris@1494 161 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
Chris@1494 162 end
Chris@1494 163
Chris@1494 164 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
Chris@1494 165 #
Chris@1494 166 # Example:
Chris@1494 167 # news_comment_added(comment) => Mail::Message object
Chris@1494 168 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
Chris@1494 169 def news_comment_added(comment)
Chris@1494 170 news = comment.commented
Chris@1494 171 redmine_headers 'Project' => news.project.identifier
Chris@1494 172 @author = comment.author
Chris@1494 173 message_id comment
Chris@1494 174 references news
Chris@1494 175 @news = news
Chris@1494 176 @comment = comment
Chris@1494 177 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
Chris@1494 178 mail :to => news.recipients,
Chris@1494 179 :cc => news.watcher_recipients,
Chris@1494 180 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
Chris@1494 181 end
Chris@1494 182
Chris@1494 183 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
Chris@1494 184 #
Chris@1494 185 # Example:
Chris@1494 186 # message_posted(message) => Mail::Message object
Chris@1494 187 # Mailer.message_posted(message).deliver => sends an email to the recipients
Chris@1494 188 def message_posted(message)
Chris@1494 189 redmine_headers 'Project' => message.project.identifier,
Chris@1494 190 'Topic-Id' => (message.parent_id || message.id)
Chris@1494 191 @author = message.author
Chris@1494 192 message_id message
Chris@1494 193 references message.root
Chris@1494 194 recipients = message.recipients
Chris@1494 195 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
Chris@1494 196 @message = message
Chris@1494 197 @message_url = url_for(message.event_url)
Chris@1494 198 mail :to => recipients,
Chris@1494 199 :cc => cc,
Chris@1494 200 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
Chris@1494 201 end
Chris@1494 202
Chris@1494 203 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
Chris@1494 204 #
Chris@1494 205 # Example:
Chris@1494 206 # wiki_content_added(wiki_content) => Mail::Message object
Chris@1494 207 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
Chris@1494 208 def wiki_content_added(wiki_content)
Chris@1494 209 redmine_headers 'Project' => wiki_content.project.identifier,
Chris@1494 210 'Wiki-Page-Id' => wiki_content.page.id
Chris@1494 211 @author = wiki_content.author
Chris@1494 212 message_id wiki_content
Chris@1494 213 recipients = wiki_content.recipients
Chris@1494 214 cc = wiki_content.page.wiki.watcher_recipients - recipients
Chris@1494 215 @wiki_content = wiki_content
Chris@1494 216 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
Chris@1494 217 :project_id => wiki_content.project,
Chris@1494 218 :id => wiki_content.page.title)
Chris@1494 219 mail :to => recipients,
Chris@1494 220 :cc => cc,
Chris@1494 221 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
Chris@1494 222 end
Chris@1494 223
Chris@1494 224 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
Chris@1494 225 #
Chris@1494 226 # Example:
Chris@1494 227 # wiki_content_updated(wiki_content) => Mail::Message object
Chris@1494 228 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
Chris@1494 229 def wiki_content_updated(wiki_content)
Chris@1494 230 redmine_headers 'Project' => wiki_content.project.identifier,
Chris@1494 231 'Wiki-Page-Id' => wiki_content.page.id
Chris@1494 232 @author = wiki_content.author
Chris@1494 233 message_id wiki_content
Chris@1494 234 recipients = wiki_content.recipients
Chris@1494 235 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
Chris@1494 236 @wiki_content = wiki_content
Chris@1494 237 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
Chris@1494 238 :project_id => wiki_content.project,
Chris@1494 239 :id => wiki_content.page.title)
Chris@1494 240 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
Chris@1494 241 :project_id => wiki_content.project, :id => wiki_content.page.title,
Chris@1494 242 :version => wiki_content.version)
Chris@1494 243 mail :to => recipients,
Chris@1494 244 :cc => cc,
Chris@1494 245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
Chris@1494 246 end
Chris@1494 247
Chris@1494 248 # Builds a Mail::Message object used to email the specified user their account information.
Chris@1494 249 #
Chris@1494 250 # Example:
Chris@1494 251 # account_information(user, password) => Mail::Message object
Chris@1494 252 # Mailer.account_information(user, password).deliver => sends account information to the user
Chris@1494 253 def account_information(user, password)
Chris@1494 254 set_language_if_valid user.language
Chris@1494 255 @user = user
Chris@1494 256 @password = password
Chris@1494 257 @login_url = url_for(:controller => 'account', :action => 'login')
Chris@1494 258 mail :to => user.mail,
Chris@1494 259 :subject => l(:mail_subject_register, Setting.app_title)
Chris@1494 260 end
Chris@1494 261
Chris@1494 262 # Builds a Mail::Message object used to email all active administrators of an account activation request.
Chris@1494 263 #
Chris@1494 264 # Example:
Chris@1494 265 # account_activation_request(user) => Mail::Message object
Chris@1494 266 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
Chris@1494 267 def account_activation_request(user)
Chris@1494 268 # Send the email to all active administrators
Chris@1494 269 recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
Chris@1494 270 @user = user
Chris@1494 271 @url = url_for(:controller => 'users', :action => 'index',
Chris@1494 272 :status => User::STATUS_REGISTERED,
Chris@1494 273 :sort_key => 'created_on', :sort_order => 'desc')
Chris@1494 274 mail :to => recipients,
Chris@1494 275 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
Chris@1494 276 end
Chris@1494 277
Chris@1494 278 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
Chris@1494 279 #
Chris@1494 280 # Example:
Chris@1494 281 # account_activated(user) => Mail::Message object
Chris@1494 282 # Mailer.account_activated(user).deliver => sends an email to the registered user
Chris@1494 283 def account_activated(user)
Chris@1494 284 set_language_if_valid user.language
Chris@1494 285 @user = user
Chris@1494 286 @login_url = url_for(:controller => 'account', :action => 'login')
Chris@1494 287 mail :to => user.mail,
Chris@1494 288 :subject => l(:mail_subject_register, Setting.app_title)
Chris@1494 289 end
Chris@1494 290
Chris@1494 291 def lost_password(token)
Chris@1494 292 set_language_if_valid(token.user.language)
Chris@1494 293 @token = token
Chris@1494 294 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
Chris@1494 295 mail :to => token.user.mail,
Chris@1494 296 :subject => l(:mail_subject_lost_password, Setting.app_title)
Chris@1494 297 end
Chris@1494 298
Chris@1494 299 def register(token)
Chris@1494 300 set_language_if_valid(token.user.language)
Chris@1494 301 @token = token
Chris@1494 302 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
Chris@1494 303 mail :to => token.user.mail,
Chris@1494 304 :subject => l(:mail_subject_register, Setting.app_title)
Chris@1494 305 end
Chris@1494 306
Chris@1494 307 def test_email(user)
Chris@1494 308 set_language_if_valid(user.language)
Chris@1494 309 @url = url_for(:controller => 'welcome')
Chris@1494 310 mail :to => user.mail,
Chris@1494 311 :subject => 'Redmine test'
Chris@1494 312 end
Chris@1494 313
Chris@1494 314 # Sends reminders to issue assignees
Chris@1494 315 # Available options:
Chris@1494 316 # * :days => how many days in the future to remind about (defaults to 7)
Chris@1494 317 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
Chris@1494 318 # * :project => id or identifier of project to process (defaults to all projects)
Chris@1494 319 # * :users => array of user/group ids who should be reminded
Chris@1494 320 def self.reminders(options={})
Chris@1494 321 days = options[:days] || 7
Chris@1494 322 project = options[:project] ? Project.find(options[:project]) : nil
Chris@1494 323 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
Chris@1494 324 user_ids = options[:users]
Chris@1494 325
Chris@1494 326 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
Chris@1494 327 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
Chris@1494 328 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
Chris@1494 329 )
Chris@1494 330 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
Chris@1494 331 scope = scope.where(:project_id => project.id) if project
Chris@1494 332 scope = scope.where(:tracker_id => tracker.id) if tracker
Chris@1494 333
Chris@1494 334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
Chris@1494 335 issues_by_assignee.keys.each do |assignee|
Chris@1494 336 if assignee.is_a?(Group)
Chris@1494 337 assignee.users.each do |user|
Chris@1494 338 issues_by_assignee[user] ||= []
Chris@1494 339 issues_by_assignee[user] += issues_by_assignee[assignee]
Chris@1494 340 end
Chris@1494 341 end
Chris@1494 342 end
Chris@1494 343
Chris@1494 344 issues_by_assignee.each do |assignee, issues|
Chris@1494 345 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
Chris@1494 346 end
Chris@1494 347 end
Chris@1494 348
Chris@1494 349 # Activates/desactivates email deliveries during +block+
Chris@1494 350 def self.with_deliveries(enabled = true, &block)
Chris@1494 351 was_enabled = ActionMailer::Base.perform_deliveries
Chris@1494 352 ActionMailer::Base.perform_deliveries = !!enabled
Chris@1494 353 yield
Chris@1494 354 ensure
Chris@1494 355 ActionMailer::Base.perform_deliveries = was_enabled
Chris@1494 356 end
Chris@1494 357
Chris@1494 358 # Sends emails synchronously in the given block
Chris@1494 359 def self.with_synched_deliveries(&block)
Chris@1494 360 saved_method = ActionMailer::Base.delivery_method
Chris@1494 361 if m = saved_method.to_s.match(%r{^async_(.+)$})
Chris@1494 362 synched_method = m[1]
Chris@1494 363 ActionMailer::Base.delivery_method = synched_method.to_sym
Chris@1494 364 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
Chris@1494 365 end
Chris@1494 366 yield
Chris@1494 367 ensure
Chris@1494 368 ActionMailer::Base.delivery_method = saved_method
Chris@1494 369 end
Chris@1494 370
Chris@1494 371 def mail(headers={}, &block)
Chris@1494 372 headers.merge! 'X-Mailer' => 'Redmine',
Chris@1494 373 'X-Redmine-Host' => Setting.host_name,
Chris@1494 374 'X-Redmine-Site' => Setting.app_title,
Chris@1494 375 'X-Auto-Response-Suppress' => 'OOF',
Chris@1494 376 'Auto-Submitted' => 'auto-generated',
Chris@1494 377 'From' => Setting.mail_from,
Chris@1494 378 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
Chris@1494 379
Chris@1494 380 # Removes the author from the recipients and cc
Chris@1494 381 # if the author does not want to receive notifications
Chris@1494 382 # about what the author do
Chris@1494 383 if @author && @author.logged? && @author.pref.no_self_notified
Chris@1494 384 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
Chris@1494 385 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
Chris@1494 386 end
Chris@1494 387
Chris@1494 388 if @author && @author.logged?
Chris@1494 389 redmine_headers 'Sender' => @author.login
Chris@1494 390 end
Chris@1494 391
Chris@1494 392 # Blind carbon copy recipients
Chris@1494 393 if Setting.bcc_recipients?
Chris@1494 394 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
Chris@1494 395 headers[:to] = nil
Chris@1494 396 headers[:cc] = nil
Chris@1494 397 end
Chris@1494 398
Chris@1494 399 if @message_id_object
Chris@1494 400 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
Chris@1494 401 end
Chris@1494 402 if @references_objects
Chris@1494 403 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
Chris@1494 404 end
Chris@1494 405
Chris@1494 406 m = if block_given?
Chris@1494 407 super headers, &block
Chris@1494 408 else
Chris@1494 409 super headers do |format|
Chris@1494 410 format.text
Chris@1494 411 format.html unless Setting.plain_text_mail?
Chris@1494 412 end
Chris@1494 413 end
Chris@1494 414 set_language_if_valid @initial_language
Chris@1494 415
Chris@1494 416 m
Chris@1494 417 end
Chris@1494 418
Chris@1494 419 def initialize(*args)
Chris@1494 420 @initial_language = current_language
Chris@1494 421 set_language_if_valid Setting.default_language
Chris@1494 422 super
Chris@1494 423 end
Chris@1494 424
Chris@1494 425 def self.deliver_mail(mail)
Chris@1494 426 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
Chris@1494 427 begin
Chris@1494 428 # Log errors when raise_delivery_errors is set to false, Rails does not
Chris@1494 429 mail.raise_delivery_errors = true
Chris@1494 430 super
Chris@1494 431 rescue Exception => e
Chris@1494 432 if ActionMailer::Base.raise_delivery_errors
Chris@1494 433 raise e
Chris@1494 434 else
Chris@1494 435 Rails.logger.error "Email delivery error: #{e.message}"
Chris@1494 436 end
Chris@1494 437 end
Chris@1494 438 end
Chris@1494 439
Chris@1494 440 def self.method_missing(method, *args, &block)
Chris@1494 441 if m = method.to_s.match(%r{^deliver_(.+)$})
Chris@1494 442 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
Chris@1494 443 send(m[1], *args).deliver
Chris@1494 444 else
Chris@1494 445 super
Chris@1494 446 end
Chris@1494 447 end
Chris@1494 448
Chris@1494 449 private
Chris@1494 450
Chris@1494 451 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
Chris@1494 452 def redmine_headers(h)
Chris@1494 453 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
Chris@1494 454 end
Chris@1494 455
Chris@1494 456 def self.token_for(object, rand=true)
Chris@1494 457 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
Chris@1494 458 hash = [
Chris@1494 459 "redmine",
Chris@1494 460 "#{object.class.name.demodulize.underscore}-#{object.id}",
Chris@1494 461 timestamp.strftime("%Y%m%d%H%M%S")
Chris@1494 462 ]
Chris@1494 463 if rand
Chris@1494 464 hash << Redmine::Utils.random_hex(8)
Chris@1494 465 end
Chris@1494 466 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
Chris@1494 467 host = "#{::Socket.gethostname}.redmine" if host.empty?
Chris@1494 468 "#{hash.join('.')}@#{host}"
Chris@1494 469 end
Chris@1494 470
Chris@1494 471 # Returns a Message-Id for the given object
Chris@1494 472 def self.message_id_for(object)
Chris@1494 473 token_for(object, true)
Chris@1494 474 end
Chris@1494 475
Chris@1494 476 # Returns a uniq token for a given object referenced by all notifications
Chris@1494 477 # related to this object
Chris@1494 478 def self.references_for(object)
Chris@1494 479 token_for(object, false)
Chris@1494 480 end
Chris@1494 481
Chris@1494 482 def message_id(object)
Chris@1494 483 @message_id_object = object
Chris@1494 484 end
Chris@1494 485
Chris@1494 486 def references(object)
Chris@1494 487 @references_objects ||= []
Chris@1494 488 @references_objects << object
Chris@1494 489 end
Chris@1494 490
Chris@1494 491 def mylogger
Chris@1494 492 Rails.logger
Chris@1494 493 end
Chris@1494 494 end