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