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