annotate app/models/mailer.rb @ 1459:cf78a7ade302 luisf

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