##// END OF EJS Templates
Adds a List-Id header to all emails (#2879)....
Jean-Philippe Lang -
r2497:554e569de10e
parent child
Show More
@@ -1,307 +1,308
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Mailer < ActionMailer::Base
18 class Mailer < ActionMailer::Base
19 helper :application
19 helper :application
20 helper :issues
20 helper :issues
21 helper :custom_fields
21 helper :custom_fields
22
22
23 include ActionController::UrlWriter
23 include ActionController::UrlWriter
24 include Redmine::I18n
24 include Redmine::I18n
25
25
26 def self.default_url_options
26 def self.default_url_options
27 h = Setting.host_name
27 h = Setting.host_name
28 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
28 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
29 { :host => h, :protocol => Setting.protocol }
29 { :host => h, :protocol => Setting.protocol }
30 end
30 end
31
31
32 def issue_add(issue)
32 def issue_add(issue)
33 redmine_headers 'Project' => issue.project.identifier,
33 redmine_headers 'Project' => issue.project.identifier,
34 'Issue-Id' => issue.id,
34 'Issue-Id' => issue.id,
35 'Issue-Author' => issue.author.login
35 'Issue-Author' => issue.author.login
36 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
37 message_id issue
37 message_id issue
38 recipients issue.recipients
38 recipients issue.recipients
39 cc(issue.watcher_recipients - @recipients)
39 cc(issue.watcher_recipients - @recipients)
40 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
40 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
41 body :issue => issue,
41 body :issue => issue,
42 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
42 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
43 end
43 end
44
44
45 def issue_edit(journal)
45 def issue_edit(journal)
46 issue = journal.journalized
46 issue = journal.journalized
47 redmine_headers 'Project' => issue.project.identifier,
47 redmine_headers 'Project' => issue.project.identifier,
48 'Issue-Id' => issue.id,
48 'Issue-Id' => issue.id,
49 'Issue-Author' => issue.author.login
49 'Issue-Author' => issue.author.login
50 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
50 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
51 message_id journal
51 message_id journal
52 references issue
52 references issue
53 @author = journal.user
53 @author = journal.user
54 recipients issue.recipients
54 recipients issue.recipients
55 # Watchers in cc
55 # Watchers in cc
56 cc(issue.watcher_recipients - @recipients)
56 cc(issue.watcher_recipients - @recipients)
57 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
57 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
58 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
58 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
59 s << issue.subject
59 s << issue.subject
60 subject s
60 subject s
61 body :issue => issue,
61 body :issue => issue,
62 :journal => journal,
62 :journal => journal,
63 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
63 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
64 end
64 end
65
65
66 def reminder(user, issues, days)
66 def reminder(user, issues, days)
67 set_language_if_valid user.language
67 set_language_if_valid user.language
68 recipients user.mail
68 recipients user.mail
69 subject l(:mail_subject_reminder, issues.size)
69 subject l(:mail_subject_reminder, issues.size)
70 body :issues => issues,
70 body :issues => issues,
71 :days => days,
71 :days => days,
72 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc')
72 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc')
73 end
73 end
74
74
75 def document_added(document)
75 def document_added(document)
76 redmine_headers 'Project' => document.project.identifier
76 redmine_headers 'Project' => document.project.identifier
77 recipients document.project.recipients
77 recipients document.project.recipients
78 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
78 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
79 body :document => document,
79 body :document => document,
80 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
80 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
81 end
81 end
82
82
83 def attachments_added(attachments)
83 def attachments_added(attachments)
84 container = attachments.first.container
84 container = attachments.first.container
85 added_to = ''
85 added_to = ''
86 added_to_url = ''
86 added_to_url = ''
87 case container.class.name
87 case container.class.name
88 when 'Project'
88 when 'Project'
89 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
89 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
90 added_to = "#{l(:label_project)}: #{container}"
90 added_to = "#{l(:label_project)}: #{container}"
91 when 'Version'
91 when 'Version'
92 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
92 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
93 added_to = "#{l(:label_version)}: #{container.name}"
93 added_to = "#{l(:label_version)}: #{container.name}"
94 when 'Document'
94 when 'Document'
95 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
95 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
96 added_to = "#{l(:label_document)}: #{container.title}"
96 added_to = "#{l(:label_document)}: #{container.title}"
97 end
97 end
98 redmine_headers 'Project' => container.project.identifier
98 redmine_headers 'Project' => container.project.identifier
99 recipients container.project.recipients
99 recipients container.project.recipients
100 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
100 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
101 body :attachments => attachments,
101 body :attachments => attachments,
102 :added_to => added_to,
102 :added_to => added_to,
103 :added_to_url => added_to_url
103 :added_to_url => added_to_url
104 end
104 end
105
105
106 def news_added(news)
106 def news_added(news)
107 redmine_headers 'Project' => news.project.identifier
107 redmine_headers 'Project' => news.project.identifier
108 message_id news
108 message_id news
109 recipients news.project.recipients
109 recipients news.project.recipients
110 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
110 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
111 body :news => news,
111 body :news => news,
112 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
112 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
113 end
113 end
114
114
115 def message_posted(message, recipients)
115 def message_posted(message, recipients)
116 redmine_headers 'Project' => message.project.identifier,
116 redmine_headers 'Project' => message.project.identifier,
117 'Topic-Id' => (message.parent_id || message.id)
117 'Topic-Id' => (message.parent_id || message.id)
118 message_id message
118 message_id message
119 references message.parent unless message.parent.nil?
119 references message.parent unless message.parent.nil?
120 recipients(recipients)
120 recipients(recipients)
121 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
121 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
122 body :message => message,
122 body :message => message,
123 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
123 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
124 end
124 end
125
125
126 def account_information(user, password)
126 def account_information(user, password)
127 set_language_if_valid user.language
127 set_language_if_valid user.language
128 recipients user.mail
128 recipients user.mail
129 subject l(:mail_subject_register, Setting.app_title)
129 subject l(:mail_subject_register, Setting.app_title)
130 body :user => user,
130 body :user => user,
131 :password => password,
131 :password => password,
132 :login_url => url_for(:controller => 'account', :action => 'login')
132 :login_url => url_for(:controller => 'account', :action => 'login')
133 end
133 end
134
134
135 def account_activation_request(user)
135 def account_activation_request(user)
136 # Send the email to all active administrators
136 # Send the email to all active administrators
137 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
137 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
138 subject l(:mail_subject_account_activation_request, Setting.app_title)
138 subject l(:mail_subject_account_activation_request, Setting.app_title)
139 body :user => user,
139 body :user => user,
140 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
140 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
141 end
141 end
142
142
143 # A registered user's account was activated by an administrator
143 # A registered user's account was activated by an administrator
144 def account_activated(user)
144 def account_activated(user)
145 set_language_if_valid user.language
145 set_language_if_valid user.language
146 recipients user.mail
146 recipients user.mail
147 subject l(:mail_subject_register, Setting.app_title)
147 subject l(:mail_subject_register, Setting.app_title)
148 body :user => user,
148 body :user => user,
149 :login_url => url_for(:controller => 'account', :action => 'login')
149 :login_url => url_for(:controller => 'account', :action => 'login')
150 end
150 end
151
151
152 def lost_password(token)
152 def lost_password(token)
153 set_language_if_valid(token.user.language)
153 set_language_if_valid(token.user.language)
154 recipients token.user.mail
154 recipients token.user.mail
155 subject l(:mail_subject_lost_password, Setting.app_title)
155 subject l(:mail_subject_lost_password, Setting.app_title)
156 body :token => token,
156 body :token => token,
157 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
157 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
158 end
158 end
159
159
160 def register(token)
160 def register(token)
161 set_language_if_valid(token.user.language)
161 set_language_if_valid(token.user.language)
162 recipients token.user.mail
162 recipients token.user.mail
163 subject l(:mail_subject_register, Setting.app_title)
163 subject l(:mail_subject_register, Setting.app_title)
164 body :token => token,
164 body :token => token,
165 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
165 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
166 end
166 end
167
167
168 def test(user)
168 def test(user)
169 set_language_if_valid(user.language)
169 set_language_if_valid(user.language)
170 recipients user.mail
170 recipients user.mail
171 subject 'Redmine test'
171 subject 'Redmine test'
172 body :url => url_for(:controller => 'welcome')
172 body :url => url_for(:controller => 'welcome')
173 end
173 end
174
174
175 # Overrides default deliver! method to prevent from sending an email
175 # Overrides default deliver! method to prevent from sending an email
176 # with no recipient, cc or bcc
176 # with no recipient, cc or bcc
177 def deliver!(mail = @mail)
177 def deliver!(mail = @mail)
178 return false if (recipients.nil? || recipients.empty?) &&
178 return false if (recipients.nil? || recipients.empty?) &&
179 (cc.nil? || cc.empty?) &&
179 (cc.nil? || cc.empty?) &&
180 (bcc.nil? || bcc.empty?)
180 (bcc.nil? || bcc.empty?)
181
181
182 # Set Message-Id and References
182 # Set Message-Id and References
183 if @message_id_object
183 if @message_id_object
184 mail.message_id = self.class.message_id_for(@message_id_object)
184 mail.message_id = self.class.message_id_for(@message_id_object)
185 end
185 end
186 if @references_objects
186 if @references_objects
187 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
187 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
188 end
188 end
189 super(mail)
189 super(mail)
190 end
190 end
191
191
192 # Sends reminders to issue assignees
192 # Sends reminders to issue assignees
193 # Available options:
193 # Available options:
194 # * :days => how many days in the future to remind about (defaults to 7)
194 # * :days => how many days in the future to remind about (defaults to 7)
195 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
195 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
196 # * :project => id or identifier of project to process (defaults to all projects)
196 # * :project => id or identifier of project to process (defaults to all projects)
197 def self.reminders(options={})
197 def self.reminders(options={})
198 days = options[:days] || 7
198 days = options[:days] || 7
199 project = options[:project] ? Project.find(options[:project]) : nil
199 project = options[:project] ? Project.find(options[:project]) : nil
200 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
200 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
201
201
202 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
202 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
203 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
203 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
204 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
204 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
205 s << "#{Issue.table_name}.project_id = #{project.id}" if project
205 s << "#{Issue.table_name}.project_id = #{project.id}" if project
206 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
206 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
207
207
208 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
208 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
209 :conditions => s.conditions
209 :conditions => s.conditions
210 ).group_by(&:assigned_to)
210 ).group_by(&:assigned_to)
211 issues_by_assignee.each do |assignee, issues|
211 issues_by_assignee.each do |assignee, issues|
212 deliver_reminder(assignee, issues, days) unless assignee.nil?
212 deliver_reminder(assignee, issues, days) unless assignee.nil?
213 end
213 end
214 end
214 end
215
215
216 private
216 private
217 def initialize_defaults(method_name)
217 def initialize_defaults(method_name)
218 super
218 super
219 set_language_if_valid Setting.default_language
219 set_language_if_valid Setting.default_language
220 from Setting.mail_from
220 from Setting.mail_from
221
221
222 # Common headers
222 # Common headers
223 headers 'X-Mailer' => 'Redmine',
223 headers 'X-Mailer' => 'Redmine',
224 'X-Redmine-Host' => Setting.host_name,
224 'X-Redmine-Host' => Setting.host_name,
225 'X-Redmine-Site' => Setting.app_title
225 'X-Redmine-Site' => Setting.app_title,
226 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
226 end
227 end
227
228
228 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
229 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
229 def redmine_headers(h)
230 def redmine_headers(h)
230 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
231 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
231 end
232 end
232
233
233 # Overrides the create_mail method
234 # Overrides the create_mail method
234 def create_mail
235 def create_mail
235 # Removes the current user from the recipients and cc
236 # Removes the current user from the recipients and cc
236 # if he doesn't want to receive notifications about what he does
237 # if he doesn't want to receive notifications about what he does
237 @author ||= User.current
238 @author ||= User.current
238 if @author.pref[:no_self_notified]
239 if @author.pref[:no_self_notified]
239 recipients.delete(@author.mail) if recipients
240 recipients.delete(@author.mail) if recipients
240 cc.delete(@author.mail) if cc
241 cc.delete(@author.mail) if cc
241 end
242 end
242 # Blind carbon copy recipients
243 # Blind carbon copy recipients
243 if Setting.bcc_recipients?
244 if Setting.bcc_recipients?
244 bcc([recipients, cc].flatten.compact.uniq)
245 bcc([recipients, cc].flatten.compact.uniq)
245 recipients []
246 recipients []
246 cc []
247 cc []
247 end
248 end
248 super
249 super
249 end
250 end
250
251
251 # Renders a message with the corresponding layout
252 # Renders a message with the corresponding layout
252 def render_message(method_name, body)
253 def render_message(method_name, body)
253 layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
254 layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
254 body[:content_for_layout] = render(:file => method_name, :body => body)
255 body[:content_for_layout] = render(:file => method_name, :body => body)
255 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true)
256 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true)
256 end
257 end
257
258
258 # for the case of plain text only
259 # for the case of plain text only
259 def body(*params)
260 def body(*params)
260 value = super(*params)
261 value = super(*params)
261 if Setting.plain_text_mail?
262 if Setting.plain_text_mail?
262 templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}")
263 templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}")
263 unless String === @body or templates.empty?
264 unless String === @body or templates.empty?
264 template = File.basename(templates.first)
265 template = File.basename(templates.first)
265 @body[:content_for_layout] = render(:file => template, :body => @body)
266 @body[:content_for_layout] = render(:file => template, :body => @body)
266 @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true)
267 @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true)
267 return @body
268 return @body
268 end
269 end
269 end
270 end
270 return value
271 return value
271 end
272 end
272
273
273 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
274 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
274 def self.controller_path
275 def self.controller_path
275 ''
276 ''
276 end unless respond_to?('controller_path')
277 end unless respond_to?('controller_path')
277
278
278 # Returns a predictable Message-Id for the given object
279 # Returns a predictable Message-Id for the given object
279 def self.message_id_for(object)
280 def self.message_id_for(object)
280 # id + timestamp should reduce the odds of a collision
281 # id + timestamp should reduce the odds of a collision
281 # as far as we don't send multiple emails for the same object
282 # as far as we don't send multiple emails for the same object
282 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{object.created_on.strftime("%Y%m%d%H%M%S")}"
283 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{object.created_on.strftime("%Y%m%d%H%M%S")}"
283 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
284 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
284 host = "#{::Socket.gethostname}.redmine" if host.empty?
285 host = "#{::Socket.gethostname}.redmine" if host.empty?
285 "<#{hash}@#{host}>"
286 "<#{hash}@#{host}>"
286 end
287 end
287
288
288 private
289 private
289
290
290 def message_id(object)
291 def message_id(object)
291 @message_id_object = object
292 @message_id_object = object
292 end
293 end
293
294
294 def references(object)
295 def references(object)
295 @references_objects ||= []
296 @references_objects ||= []
296 @references_objects << object
297 @references_objects << object
297 end
298 end
298 end
299 end
299
300
300 # Patch TMail so that message_id is not overwritten
301 # Patch TMail so that message_id is not overwritten
301 module TMail
302 module TMail
302 class Mail
303 class Mail
303 def add_message_id( fqdn = nil )
304 def add_message_id( fqdn = nil )
304 self.message_id ||= ::TMail::new_message_id(fqdn)
305 self.message_id ||= ::TMail::new_message_id(fqdn)
305 end
306 end
306 end
307 end
307 end
308 end
General Comments 0
You need to be logged in to leave comments. Login now