annotate app/models/mailer.rb @ 1116:bb32da3bea34 redmine-2.2-integration

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