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 @ 955:97a00e2e145d

History | View | Annotate | Download (20.1 KB)

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