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