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 @ 443:350acce374a2

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