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