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 @ 442:753f1380d6bc

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