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