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 @ 912:5e80956cc792

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