##// END OF EJS Templates
remove unneeded Relation#all from Mailer model...
Toshi MARUYAMA -
r12448:ea8e5c74ec49
parent child
Show More
@@ -1,494 +1,494
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Mailer < ActionMailer::Base
18 class Mailer < ActionMailer::Base
19 layout 'mailer'
19 layout 'mailer'
20 helper :application
20 helper :application
21 helper :issues
21 helper :issues
22 helper :custom_fields
22 helper :custom_fields
23
23
24 include Redmine::I18n
24 include Redmine::I18n
25
25
26 def self.default_url_options
26 def self.default_url_options
27 { :host => Setting.host_name, :protocol => Setting.protocol }
27 { :host => Setting.host_name, :protocol => Setting.protocol }
28 end
28 end
29
29
30 # Builds a mail for notifying to_users and cc_users about a new issue
30 # Builds a mail for notifying to_users and cc_users about a new issue
31 def issue_add(issue, to_users, cc_users)
31 def issue_add(issue, to_users, cc_users)
32 redmine_headers 'Project' => issue.project.identifier,
32 redmine_headers 'Project' => issue.project.identifier,
33 'Issue-Id' => issue.id,
33 'Issue-Id' => issue.id,
34 'Issue-Author' => issue.author.login
34 'Issue-Author' => issue.author.login
35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 message_id issue
36 message_id issue
37 references issue
37 references issue
38 @author = issue.author
38 @author = issue.author
39 @issue = issue
39 @issue = issue
40 @users = to_users + cc_users
40 @users = to_users + cc_users
41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
42 mail :to => to_users.map(&:mail),
42 mail :to => to_users.map(&:mail),
43 :cc => cc_users.map(&:mail),
43 :cc => cc_users.map(&:mail),
44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
45 end
45 end
46
46
47 # Notifies users about a new issue
47 # Notifies users about a new issue
48 def self.deliver_issue_add(issue)
48 def self.deliver_issue_add(issue)
49 to = issue.notified_users
49 to = issue.notified_users
50 cc = issue.notified_watchers - to
50 cc = issue.notified_watchers - to
51 issue.each_notification(to + cc) do |users|
51 issue.each_notification(to + cc) do |users|
52 Mailer.issue_add(issue, to & users, cc & users).deliver
52 Mailer.issue_add(issue, to & users, cc & users).deliver
53 end
53 end
54 end
54 end
55
55
56 # Builds a mail for notifying to_users and cc_users about an issue update
56 # Builds a mail for notifying to_users and cc_users about an issue update
57 def issue_edit(journal, to_users, cc_users)
57 def issue_edit(journal, to_users, cc_users)
58 issue = journal.journalized
58 issue = journal.journalized
59 redmine_headers 'Project' => issue.project.identifier,
59 redmine_headers 'Project' => issue.project.identifier,
60 'Issue-Id' => issue.id,
60 'Issue-Id' => issue.id,
61 'Issue-Author' => issue.author.login
61 'Issue-Author' => issue.author.login
62 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
63 message_id journal
63 message_id journal
64 references issue
64 references issue
65 @author = journal.user
65 @author = journal.user
66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
68 s << issue.subject
68 s << issue.subject
69 @issue = issue
69 @issue = issue
70 @users = to_users + cc_users
70 @users = to_users + cc_users
71 @journal = journal
71 @journal = journal
72 @journal_details = journal.visible_details(@users.first)
72 @journal_details = journal.visible_details(@users.first)
73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
74 mail :to => to_users.map(&:mail),
74 mail :to => to_users.map(&:mail),
75 :cc => cc_users.map(&:mail),
75 :cc => cc_users.map(&:mail),
76 :subject => s
76 :subject => s
77 end
77 end
78
78
79 # Notifies users about an issue update
79 # Notifies users about an issue update
80 def self.deliver_issue_edit(journal)
80 def self.deliver_issue_edit(journal)
81 issue = journal.journalized.reload
81 issue = journal.journalized.reload
82 to = journal.notified_users
82 to = journal.notified_users
83 cc = journal.notified_watchers
83 cc = journal.notified_watchers
84 journal.each_notification(to + cc) do |users|
84 journal.each_notification(to + cc) do |users|
85 issue.each_notification(users) do |users2|
85 issue.each_notification(users) do |users2|
86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
87 end
87 end
88 end
88 end
89 end
89 end
90
90
91 def reminder(user, issues, days)
91 def reminder(user, issues, days)
92 set_language_if_valid user.language
92 set_language_if_valid user.language
93 @issues = issues
93 @issues = issues
94 @days = days
94 @days = days
95 @issues_url = url_for(:controller => 'issues', :action => 'index',
95 @issues_url = url_for(:controller => 'issues', :action => 'index',
96 :set_filter => 1, :assigned_to_id => user.id,
96 :set_filter => 1, :assigned_to_id => user.id,
97 :sort => 'due_date:asc')
97 :sort => 'due_date:asc')
98 mail :to => user.mail,
98 mail :to => user.mail,
99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
100 end
100 end
101
101
102 # Builds a Mail::Message object used to email users belonging to the added document's project.
102 # Builds a Mail::Message object used to email users belonging to the added document's project.
103 #
103 #
104 # Example:
104 # Example:
105 # document_added(document) => Mail::Message object
105 # document_added(document) => Mail::Message object
106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
107 def document_added(document)
107 def document_added(document)
108 redmine_headers 'Project' => document.project.identifier
108 redmine_headers 'Project' => document.project.identifier
109 @author = User.current
109 @author = User.current
110 @document = document
110 @document = document
111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
112 mail :to => document.recipients,
112 mail :to => document.recipients,
113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
114 end
114 end
115
115
116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
117 #
117 #
118 # Example:
118 # Example:
119 # attachments_added(attachments) => Mail::Message object
119 # attachments_added(attachments) => Mail::Message object
120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
121 def attachments_added(attachments)
121 def attachments_added(attachments)
122 container = attachments.first.container
122 container = attachments.first.container
123 added_to = ''
123 added_to = ''
124 added_to_url = ''
124 added_to_url = ''
125 @author = attachments.first.author
125 @author = attachments.first.author
126 case container.class.name
126 case container.class.name
127 when 'Project'
127 when 'Project'
128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
129 added_to = "#{l(:label_project)}: #{container}"
129 added_to = "#{l(:label_project)}: #{container}"
130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
131 when 'Version'
131 when 'Version'
132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
133 added_to = "#{l(:label_version)}: #{container.name}"
133 added_to = "#{l(:label_version)}: #{container.name}"
134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
135 when 'Document'
135 when 'Document'
136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
137 added_to = "#{l(:label_document)}: #{container.title}"
137 added_to = "#{l(:label_document)}: #{container.title}"
138 recipients = container.recipients
138 recipients = container.recipients
139 end
139 end
140 redmine_headers 'Project' => container.project.identifier
140 redmine_headers 'Project' => container.project.identifier
141 @attachments = attachments
141 @attachments = attachments
142 @added_to = added_to
142 @added_to = added_to
143 @added_to_url = added_to_url
143 @added_to_url = added_to_url
144 mail :to => recipients,
144 mail :to => recipients,
145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
146 end
146 end
147
147
148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
149 #
149 #
150 # Example:
150 # Example:
151 # news_added(news) => Mail::Message object
151 # news_added(news) => Mail::Message object
152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
153 def news_added(news)
153 def news_added(news)
154 redmine_headers 'Project' => news.project.identifier
154 redmine_headers 'Project' => news.project.identifier
155 @author = news.author
155 @author = news.author
156 message_id news
156 message_id news
157 references news
157 references news
158 @news = news
158 @news = news
159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
160 mail :to => news.recipients,
160 mail :to => news.recipients,
161 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
161 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
162 end
162 end
163
163
164 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
164 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
165 #
165 #
166 # Example:
166 # Example:
167 # news_comment_added(comment) => Mail::Message object
167 # news_comment_added(comment) => Mail::Message object
168 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
168 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
169 def news_comment_added(comment)
169 def news_comment_added(comment)
170 news = comment.commented
170 news = comment.commented
171 redmine_headers 'Project' => news.project.identifier
171 redmine_headers 'Project' => news.project.identifier
172 @author = comment.author
172 @author = comment.author
173 message_id comment
173 message_id comment
174 references news
174 references news
175 @news = news
175 @news = news
176 @comment = comment
176 @comment = comment
177 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
177 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
178 mail :to => news.recipients,
178 mail :to => news.recipients,
179 :cc => news.watcher_recipients,
179 :cc => news.watcher_recipients,
180 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
180 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
181 end
181 end
182
182
183 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
183 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
184 #
184 #
185 # Example:
185 # Example:
186 # message_posted(message) => Mail::Message object
186 # message_posted(message) => Mail::Message object
187 # Mailer.message_posted(message).deliver => sends an email to the recipients
187 # Mailer.message_posted(message).deliver => sends an email to the recipients
188 def message_posted(message)
188 def message_posted(message)
189 redmine_headers 'Project' => message.project.identifier,
189 redmine_headers 'Project' => message.project.identifier,
190 'Topic-Id' => (message.parent_id || message.id)
190 'Topic-Id' => (message.parent_id || message.id)
191 @author = message.author
191 @author = message.author
192 message_id message
192 message_id message
193 references message.root
193 references message.root
194 recipients = message.recipients
194 recipients = message.recipients
195 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
195 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
196 @message = message
196 @message = message
197 @message_url = url_for(message.event_url)
197 @message_url = url_for(message.event_url)
198 mail :to => recipients,
198 mail :to => recipients,
199 :cc => cc,
199 :cc => cc,
200 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
200 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
201 end
201 end
202
202
203 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
203 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
204 #
204 #
205 # Example:
205 # Example:
206 # wiki_content_added(wiki_content) => Mail::Message object
206 # wiki_content_added(wiki_content) => Mail::Message object
207 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
207 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
208 def wiki_content_added(wiki_content)
208 def wiki_content_added(wiki_content)
209 redmine_headers 'Project' => wiki_content.project.identifier,
209 redmine_headers 'Project' => wiki_content.project.identifier,
210 'Wiki-Page-Id' => wiki_content.page.id
210 'Wiki-Page-Id' => wiki_content.page.id
211 @author = wiki_content.author
211 @author = wiki_content.author
212 message_id wiki_content
212 message_id wiki_content
213 recipients = wiki_content.recipients
213 recipients = wiki_content.recipients
214 cc = wiki_content.page.wiki.watcher_recipients - recipients
214 cc = wiki_content.page.wiki.watcher_recipients - recipients
215 @wiki_content = wiki_content
215 @wiki_content = wiki_content
216 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
216 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
217 :project_id => wiki_content.project,
217 :project_id => wiki_content.project,
218 :id => wiki_content.page.title)
218 :id => wiki_content.page.title)
219 mail :to => recipients,
219 mail :to => recipients,
220 :cc => cc,
220 :cc => cc,
221 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
221 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
222 end
222 end
223
223
224 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
224 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
225 #
225 #
226 # Example:
226 # Example:
227 # wiki_content_updated(wiki_content) => Mail::Message object
227 # wiki_content_updated(wiki_content) => Mail::Message object
228 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
228 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
229 def wiki_content_updated(wiki_content)
229 def wiki_content_updated(wiki_content)
230 redmine_headers 'Project' => wiki_content.project.identifier,
230 redmine_headers 'Project' => wiki_content.project.identifier,
231 'Wiki-Page-Id' => wiki_content.page.id
231 'Wiki-Page-Id' => wiki_content.page.id
232 @author = wiki_content.author
232 @author = wiki_content.author
233 message_id wiki_content
233 message_id wiki_content
234 recipients = wiki_content.recipients
234 recipients = wiki_content.recipients
235 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
235 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
236 @wiki_content = wiki_content
236 @wiki_content = wiki_content
237 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
237 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
238 :project_id => wiki_content.project,
238 :project_id => wiki_content.project,
239 :id => wiki_content.page.title)
239 :id => wiki_content.page.title)
240 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
240 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
241 :project_id => wiki_content.project, :id => wiki_content.page.title,
241 :project_id => wiki_content.project, :id => wiki_content.page.title,
242 :version => wiki_content.version)
242 :version => wiki_content.version)
243 mail :to => recipients,
243 mail :to => recipients,
244 :cc => cc,
244 :cc => cc,
245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
246 end
246 end
247
247
248 # Builds a Mail::Message object used to email the specified user their account information.
248 # Builds a Mail::Message object used to email the specified user their account information.
249 #
249 #
250 # Example:
250 # Example:
251 # account_information(user, password) => Mail::Message object
251 # account_information(user, password) => Mail::Message object
252 # Mailer.account_information(user, password).deliver => sends account information to the user
252 # Mailer.account_information(user, password).deliver => sends account information to the user
253 def account_information(user, password)
253 def account_information(user, password)
254 set_language_if_valid user.language
254 set_language_if_valid user.language
255 @user = user
255 @user = user
256 @password = password
256 @password = password
257 @login_url = url_for(:controller => 'account', :action => 'login')
257 @login_url = url_for(:controller => 'account', :action => 'login')
258 mail :to => user.mail,
258 mail :to => user.mail,
259 :subject => l(:mail_subject_register, Setting.app_title)
259 :subject => l(:mail_subject_register, Setting.app_title)
260 end
260 end
261
261
262 # Builds a Mail::Message object used to email all active administrators of an account activation request.
262 # Builds a Mail::Message object used to email all active administrators of an account activation request.
263 #
263 #
264 # Example:
264 # Example:
265 # account_activation_request(user) => Mail::Message object
265 # account_activation_request(user) => Mail::Message object
266 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
266 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
267 def account_activation_request(user)
267 def account_activation_request(user)
268 # Send the email to all active administrators
268 # Send the email to all active administrators
269 recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
269 recipients = User.active.where(:admin => true).collect { |u| u.mail }.compact
270 @user = user
270 @user = user
271 @url = url_for(:controller => 'users', :action => 'index',
271 @url = url_for(:controller => 'users', :action => 'index',
272 :status => User::STATUS_REGISTERED,
272 :status => User::STATUS_REGISTERED,
273 :sort_key => 'created_on', :sort_order => 'desc')
273 :sort_key => 'created_on', :sort_order => 'desc')
274 mail :to => recipients,
274 mail :to => recipients,
275 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
275 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
276 end
276 end
277
277
278 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
278 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
279 #
279 #
280 # Example:
280 # Example:
281 # account_activated(user) => Mail::Message object
281 # account_activated(user) => Mail::Message object
282 # Mailer.account_activated(user).deliver => sends an email to the registered user
282 # Mailer.account_activated(user).deliver => sends an email to the registered user
283 def account_activated(user)
283 def account_activated(user)
284 set_language_if_valid user.language
284 set_language_if_valid user.language
285 @user = user
285 @user = user
286 @login_url = url_for(:controller => 'account', :action => 'login')
286 @login_url = url_for(:controller => 'account', :action => 'login')
287 mail :to => user.mail,
287 mail :to => user.mail,
288 :subject => l(:mail_subject_register, Setting.app_title)
288 :subject => l(:mail_subject_register, Setting.app_title)
289 end
289 end
290
290
291 def lost_password(token)
291 def lost_password(token)
292 set_language_if_valid(token.user.language)
292 set_language_if_valid(token.user.language)
293 @token = token
293 @token = token
294 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
294 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
295 mail :to => token.user.mail,
295 mail :to => token.user.mail,
296 :subject => l(:mail_subject_lost_password, Setting.app_title)
296 :subject => l(:mail_subject_lost_password, Setting.app_title)
297 end
297 end
298
298
299 def register(token)
299 def register(token)
300 set_language_if_valid(token.user.language)
300 set_language_if_valid(token.user.language)
301 @token = token
301 @token = token
302 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
302 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
303 mail :to => token.user.mail,
303 mail :to => token.user.mail,
304 :subject => l(:mail_subject_register, Setting.app_title)
304 :subject => l(:mail_subject_register, Setting.app_title)
305 end
305 end
306
306
307 def test_email(user)
307 def test_email(user)
308 set_language_if_valid(user.language)
308 set_language_if_valid(user.language)
309 @url = url_for(:controller => 'welcome')
309 @url = url_for(:controller => 'welcome')
310 mail :to => user.mail,
310 mail :to => user.mail,
311 :subject => 'Redmine test'
311 :subject => 'Redmine test'
312 end
312 end
313
313
314 # Sends reminders to issue assignees
314 # Sends reminders to issue assignees
315 # Available options:
315 # Available options:
316 # * :days => how many days in the future to remind about (defaults to 7)
316 # * :days => how many days in the future to remind about (defaults to 7)
317 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
317 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
318 # * :project => id or identifier of project to process (defaults to all projects)
318 # * :project => id or identifier of project to process (defaults to all projects)
319 # * :users => array of user/group ids who should be reminded
319 # * :users => array of user/group ids who should be reminded
320 def self.reminders(options={})
320 def self.reminders(options={})
321 days = options[:days] || 7
321 days = options[:days] || 7
322 project = options[:project] ? Project.find(options[:project]) : nil
322 project = options[:project] ? Project.find(options[:project]) : nil
323 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
323 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
324 user_ids = options[:users]
324 user_ids = options[:users]
325
325
326 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
326 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
327 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
327 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
328 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
328 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
329 )
329 )
330 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
330 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
331 scope = scope.where(:project_id => project.id) if project
331 scope = scope.where(:project_id => project.id) if project
332 scope = scope.where(:tracker_id => tracker.id) if tracker
332 scope = scope.where(:tracker_id => tracker.id) if tracker
333
333 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
334 group_by(&:assigned_to)
335 issues_by_assignee.keys.each do |assignee|
335 issues_by_assignee.keys.each do |assignee|
336 if assignee.is_a?(Group)
336 if assignee.is_a?(Group)
337 assignee.users.each do |user|
337 assignee.users.each do |user|
338 issues_by_assignee[user] ||= []
338 issues_by_assignee[user] ||= []
339 issues_by_assignee[user] += issues_by_assignee[assignee]
339 issues_by_assignee[user] += issues_by_assignee[assignee]
340 end
340 end
341 end
341 end
342 end
342 end
343
343
344 issues_by_assignee.each do |assignee, issues|
344 issues_by_assignee.each do |assignee, issues|
345 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
345 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
346 end
346 end
347 end
347 end
348
348
349 # Activates/desactivates email deliveries during +block+
349 # Activates/desactivates email deliveries during +block+
350 def self.with_deliveries(enabled = true, &block)
350 def self.with_deliveries(enabled = true, &block)
351 was_enabled = ActionMailer::Base.perform_deliveries
351 was_enabled = ActionMailer::Base.perform_deliveries
352 ActionMailer::Base.perform_deliveries = !!enabled
352 ActionMailer::Base.perform_deliveries = !!enabled
353 yield
353 yield
354 ensure
354 ensure
355 ActionMailer::Base.perform_deliveries = was_enabled
355 ActionMailer::Base.perform_deliveries = was_enabled
356 end
356 end
357
357
358 # Sends emails synchronously in the given block
358 # Sends emails synchronously in the given block
359 def self.with_synched_deliveries(&block)
359 def self.with_synched_deliveries(&block)
360 saved_method = ActionMailer::Base.delivery_method
360 saved_method = ActionMailer::Base.delivery_method
361 if m = saved_method.to_s.match(%r{^async_(.+)$})
361 if m = saved_method.to_s.match(%r{^async_(.+)$})
362 synched_method = m[1]
362 synched_method = m[1]
363 ActionMailer::Base.delivery_method = synched_method.to_sym
363 ActionMailer::Base.delivery_method = synched_method.to_sym
364 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
364 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
365 end
365 end
366 yield
366 yield
367 ensure
367 ensure
368 ActionMailer::Base.delivery_method = saved_method
368 ActionMailer::Base.delivery_method = saved_method
369 end
369 end
370
370
371 def mail(headers={}, &block)
371 def mail(headers={}, &block)
372 headers.merge! 'X-Mailer' => 'Redmine',
372 headers.merge! 'X-Mailer' => 'Redmine',
373 'X-Redmine-Host' => Setting.host_name,
373 'X-Redmine-Host' => Setting.host_name,
374 'X-Redmine-Site' => Setting.app_title,
374 'X-Redmine-Site' => Setting.app_title,
375 'X-Auto-Response-Suppress' => 'OOF',
375 'X-Auto-Response-Suppress' => 'OOF',
376 'Auto-Submitted' => 'auto-generated',
376 'Auto-Submitted' => 'auto-generated',
377 'From' => Setting.mail_from,
377 'From' => Setting.mail_from,
378 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
378 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
379
379
380 # Removes the author from the recipients and cc
380 # Removes the author from the recipients and cc
381 # if the author does not want to receive notifications
381 # if the author does not want to receive notifications
382 # about what the author do
382 # about what the author do
383 if @author && @author.logged? && @author.pref.no_self_notified
383 if @author && @author.logged? && @author.pref.no_self_notified
384 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
384 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
385 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
385 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
386 end
386 end
387
387
388 if @author && @author.logged?
388 if @author && @author.logged?
389 redmine_headers 'Sender' => @author.login
389 redmine_headers 'Sender' => @author.login
390 end
390 end
391
391
392 # Blind carbon copy recipients
392 # Blind carbon copy recipients
393 if Setting.bcc_recipients?
393 if Setting.bcc_recipients?
394 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
394 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
395 headers[:to] = nil
395 headers[:to] = nil
396 headers[:cc] = nil
396 headers[:cc] = nil
397 end
397 end
398
398
399 if @message_id_object
399 if @message_id_object
400 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
400 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
401 end
401 end
402 if @references_objects
402 if @references_objects
403 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
403 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
404 end
404 end
405
405
406 m = if block_given?
406 m = if block_given?
407 super headers, &block
407 super headers, &block
408 else
408 else
409 super headers do |format|
409 super headers do |format|
410 format.text
410 format.text
411 format.html unless Setting.plain_text_mail?
411 format.html unless Setting.plain_text_mail?
412 end
412 end
413 end
413 end
414 set_language_if_valid @initial_language
414 set_language_if_valid @initial_language
415
415
416 m
416 m
417 end
417 end
418
418
419 def initialize(*args)
419 def initialize(*args)
420 @initial_language = current_language
420 @initial_language = current_language
421 set_language_if_valid Setting.default_language
421 set_language_if_valid Setting.default_language
422 super
422 super
423 end
423 end
424
424
425 def self.deliver_mail(mail)
425 def self.deliver_mail(mail)
426 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
426 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
427 begin
427 begin
428 # Log errors when raise_delivery_errors is set to false, Rails does not
428 # Log errors when raise_delivery_errors is set to false, Rails does not
429 mail.raise_delivery_errors = true
429 mail.raise_delivery_errors = true
430 super
430 super
431 rescue Exception => e
431 rescue Exception => e
432 if ActionMailer::Base.raise_delivery_errors
432 if ActionMailer::Base.raise_delivery_errors
433 raise e
433 raise e
434 else
434 else
435 Rails.logger.error "Email delivery error: #{e.message}"
435 Rails.logger.error "Email delivery error: #{e.message}"
436 end
436 end
437 end
437 end
438 end
438 end
439
439
440 def self.method_missing(method, *args, &block)
440 def self.method_missing(method, *args, &block)
441 if m = method.to_s.match(%r{^deliver_(.+)$})
441 if m = method.to_s.match(%r{^deliver_(.+)$})
442 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
442 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
443 send(m[1], *args).deliver
443 send(m[1], *args).deliver
444 else
444 else
445 super
445 super
446 end
446 end
447 end
447 end
448
448
449 private
449 private
450
450
451 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
451 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
452 def redmine_headers(h)
452 def redmine_headers(h)
453 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
453 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
454 end
454 end
455
455
456 def self.token_for(object, rand=true)
456 def self.token_for(object, rand=true)
457 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
457 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
458 hash = [
458 hash = [
459 "redmine",
459 "redmine",
460 "#{object.class.name.demodulize.underscore}-#{object.id}",
460 "#{object.class.name.demodulize.underscore}-#{object.id}",
461 timestamp.strftime("%Y%m%d%H%M%S")
461 timestamp.strftime("%Y%m%d%H%M%S")
462 ]
462 ]
463 if rand
463 if rand
464 hash << Redmine::Utils.random_hex(8)
464 hash << Redmine::Utils.random_hex(8)
465 end
465 end
466 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
466 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
467 host = "#{::Socket.gethostname}.redmine" if host.empty?
467 host = "#{::Socket.gethostname}.redmine" if host.empty?
468 "#{hash.join('.')}@#{host}"
468 "#{hash.join('.')}@#{host}"
469 end
469 end
470
470
471 # Returns a Message-Id for the given object
471 # Returns a Message-Id for the given object
472 def self.message_id_for(object)
472 def self.message_id_for(object)
473 token_for(object, true)
473 token_for(object, true)
474 end
474 end
475
475
476 # Returns a uniq token for a given object referenced by all notifications
476 # Returns a uniq token for a given object referenced by all notifications
477 # related to this object
477 # related to this object
478 def self.references_for(object)
478 def self.references_for(object)
479 token_for(object, false)
479 token_for(object, false)
480 end
480 end
481
481
482 def message_id(object)
482 def message_id(object)
483 @message_id_object = object
483 @message_id_object = object
484 end
484 end
485
485
486 def references(object)
486 def references(object)
487 @references_objects ||= []
487 @references_objects ||= []
488 @references_objects << object
488 @references_objects << object
489 end
489 end
490
490
491 def mylogger
491 def mylogger
492 Rails.logger
492 Rails.logger
493 end
493 end
494 end
494 end
General Comments 0
You need to be logged in to leave comments. Login now