To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / mailer.rb @ 1532:a0460a3d154f

History | View | Annotate | Download (19.9 KB)

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