annotate .svn/pristine/5a/5a6cc174ba291c127c96b8baee3cdc67423da788.svn-base @ 1517:dffacf8a6908 redmine-2.5

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