##// END OF EJS Templates
Raise an error if version is not found (#18983)....
Jean-Philippe Lang -
r13583:3bd2b43a2f73
parent child
Show More
@@ -1,526 +1,529
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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,
42 mail :to => to_users,
43 :cc => cc_users,
43 :cc => cc_users,
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,
74 mail :to => to_users,
75 :cc => cc_users,
75 :cc => cc_users,
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 - to
83 cc = journal.notified_watchers - to
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,
98 mail :to => user,
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.notified_users,
112 mail :to => document.notified_users,
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)}
130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}
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)}
134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}
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.notified_users
138 recipients = container.notified_users
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.notified_users,
160 mail :to => news.notified_users,
161 :cc => news.notified_watchers_for_added_news,
161 :cc => news.notified_watchers_for_added_news,
162 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
162 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
163 end
163 end
164
164
165 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
165 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
166 #
166 #
167 # Example:
167 # Example:
168 # news_comment_added(comment) => Mail::Message object
168 # news_comment_added(comment) => Mail::Message object
169 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
169 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
170 def news_comment_added(comment)
170 def news_comment_added(comment)
171 news = comment.commented
171 news = comment.commented
172 redmine_headers 'Project' => news.project.identifier
172 redmine_headers 'Project' => news.project.identifier
173 @author = comment.author
173 @author = comment.author
174 message_id comment
174 message_id comment
175 references news
175 references news
176 @news = news
176 @news = news
177 @comment = comment
177 @comment = comment
178 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
178 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
179 mail :to => news.notified_users,
179 mail :to => news.notified_users,
180 :cc => news.notified_watchers,
180 :cc => news.notified_watchers,
181 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
181 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
182 end
182 end
183
183
184 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
184 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
185 #
185 #
186 # Example:
186 # Example:
187 # message_posted(message) => Mail::Message object
187 # message_posted(message) => Mail::Message object
188 # Mailer.message_posted(message).deliver => sends an email to the recipients
188 # Mailer.message_posted(message).deliver => sends an email to the recipients
189 def message_posted(message)
189 def message_posted(message)
190 redmine_headers 'Project' => message.project.identifier,
190 redmine_headers 'Project' => message.project.identifier,
191 'Topic-Id' => (message.parent_id || message.id)
191 'Topic-Id' => (message.parent_id || message.id)
192 @author = message.author
192 @author = message.author
193 message_id message
193 message_id message
194 references message.root
194 references message.root
195 recipients = message.notified_users
195 recipients = message.notified_users
196 cc = ((message.root.notified_watchers + message.board.notified_watchers).uniq - recipients)
196 cc = ((message.root.notified_watchers + message.board.notified_watchers).uniq - recipients)
197 @message = message
197 @message = message
198 @message_url = url_for(message.event_url)
198 @message_url = url_for(message.event_url)
199 mail :to => recipients,
199 mail :to => recipients,
200 :cc => cc,
200 :cc => cc,
201 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
201 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
202 end
202 end
203
203
204 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
204 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
205 #
205 #
206 # Example:
206 # Example:
207 # wiki_content_added(wiki_content) => Mail::Message object
207 # wiki_content_added(wiki_content) => Mail::Message object
208 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
208 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
209 def wiki_content_added(wiki_content)
209 def wiki_content_added(wiki_content)
210 redmine_headers 'Project' => wiki_content.project.identifier,
210 redmine_headers 'Project' => wiki_content.project.identifier,
211 'Wiki-Page-Id' => wiki_content.page.id
211 'Wiki-Page-Id' => wiki_content.page.id
212 @author = wiki_content.author
212 @author = wiki_content.author
213 message_id wiki_content
213 message_id wiki_content
214 recipients = wiki_content.notified_users
214 recipients = wiki_content.notified_users
215 cc = wiki_content.page.wiki.notified_watchers - recipients
215 cc = wiki_content.page.wiki.notified_watchers - recipients
216 @wiki_content = wiki_content
216 @wiki_content = wiki_content
217 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
217 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
218 :project_id => wiki_content.project,
218 :project_id => wiki_content.project,
219 :id => wiki_content.page.title)
219 :id => wiki_content.page.title)
220 mail :to => recipients,
220 mail :to => recipients,
221 :cc => cc,
221 :cc => cc,
222 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
222 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
223 end
223 end
224
224
225 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
225 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
226 #
226 #
227 # Example:
227 # Example:
228 # wiki_content_updated(wiki_content) => Mail::Message object
228 # wiki_content_updated(wiki_content) => Mail::Message object
229 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
229 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
230 def wiki_content_updated(wiki_content)
230 def wiki_content_updated(wiki_content)
231 redmine_headers 'Project' => wiki_content.project.identifier,
231 redmine_headers 'Project' => wiki_content.project.identifier,
232 'Wiki-Page-Id' => wiki_content.page.id
232 'Wiki-Page-Id' => wiki_content.page.id
233 @author = wiki_content.author
233 @author = wiki_content.author
234 message_id wiki_content
234 message_id wiki_content
235 recipients = wiki_content.notified_users
235 recipients = wiki_content.notified_users
236 cc = wiki_content.page.wiki.notified_watchers + wiki_content.page.notified_watchers - recipients
236 cc = wiki_content.page.wiki.notified_watchers + wiki_content.page.notified_watchers - recipients
237 @wiki_content = wiki_content
237 @wiki_content = wiki_content
238 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
238 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
239 :project_id => wiki_content.project,
239 :project_id => wiki_content.project,
240 :id => wiki_content.page.title)
240 :id => wiki_content.page.title)
241 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
241 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
242 :project_id => wiki_content.project, :id => wiki_content.page.title,
242 :project_id => wiki_content.project, :id => wiki_content.page.title,
243 :version => wiki_content.version)
243 :version => wiki_content.version)
244 mail :to => recipients,
244 mail :to => recipients,
245 :cc => cc,
245 :cc => cc,
246 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
246 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
247 end
247 end
248
248
249 # Builds a Mail::Message object used to email the specified user their account information.
249 # Builds a Mail::Message object used to email the specified user their account information.
250 #
250 #
251 # Example:
251 # Example:
252 # account_information(user, password) => Mail::Message object
252 # account_information(user, password) => Mail::Message object
253 # Mailer.account_information(user, password).deliver => sends account information to the user
253 # Mailer.account_information(user, password).deliver => sends account information to the user
254 def account_information(user, password)
254 def account_information(user, password)
255 set_language_if_valid user.language
255 set_language_if_valid user.language
256 @user = user
256 @user = user
257 @password = password
257 @password = password
258 @login_url = url_for(:controller => 'account', :action => 'login')
258 @login_url = url_for(:controller => 'account', :action => 'login')
259 mail :to => user.mail,
259 mail :to => user.mail,
260 :subject => l(:mail_subject_register, Setting.app_title)
260 :subject => l(:mail_subject_register, Setting.app_title)
261 end
261 end
262
262
263 # Builds a Mail::Message object used to email all active administrators of an account activation request.
263 # Builds a Mail::Message object used to email all active administrators of an account activation request.
264 #
264 #
265 # Example:
265 # Example:
266 # account_activation_request(user) => Mail::Message object
266 # account_activation_request(user) => Mail::Message object
267 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
267 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
268 def account_activation_request(user)
268 def account_activation_request(user)
269 # Send the email to all active administrators
269 # Send the email to all active administrators
270 recipients = User.active.where(:admin => true)
270 recipients = User.active.where(:admin => true)
271 @user = user
271 @user = user
272 @url = url_for(:controller => 'users', :action => 'index',
272 @url = url_for(:controller => 'users', :action => 'index',
273 :status => User::STATUS_REGISTERED,
273 :status => User::STATUS_REGISTERED,
274 :sort_key => 'created_on', :sort_order => 'desc')
274 :sort_key => 'created_on', :sort_order => 'desc')
275 mail :to => recipients,
275 mail :to => recipients,
276 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
276 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
277 end
277 end
278
278
279 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
279 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
280 #
280 #
281 # Example:
281 # Example:
282 # account_activated(user) => Mail::Message object
282 # account_activated(user) => Mail::Message object
283 # Mailer.account_activated(user).deliver => sends an email to the registered user
283 # Mailer.account_activated(user).deliver => sends an email to the registered user
284 def account_activated(user)
284 def account_activated(user)
285 set_language_if_valid user.language
285 set_language_if_valid user.language
286 @user = user
286 @user = user
287 @login_url = url_for(:controller => 'account', :action => 'login')
287 @login_url = url_for(:controller => 'account', :action => 'login')
288 mail :to => user.mail,
288 mail :to => user.mail,
289 :subject => l(:mail_subject_register, Setting.app_title)
289 :subject => l(:mail_subject_register, Setting.app_title)
290 end
290 end
291
291
292 def lost_password(token, recipient=nil)
292 def lost_password(token, recipient=nil)
293 set_language_if_valid(token.user.language)
293 set_language_if_valid(token.user.language)
294 recipient ||= token.user.mail
294 recipient ||= token.user.mail
295 @token = token
295 @token = token
296 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
296 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
297 mail :to => recipient,
297 mail :to => recipient,
298 :subject => l(:mail_subject_lost_password, Setting.app_title)
298 :subject => l(:mail_subject_lost_password, Setting.app_title)
299 end
299 end
300
300
301 def register(token)
301 def register(token)
302 set_language_if_valid(token.user.language)
302 set_language_if_valid(token.user.language)
303 @token = token
303 @token = token
304 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
304 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
305 mail :to => token.user.mail,
305 mail :to => token.user.mail,
306 :subject => l(:mail_subject_register, Setting.app_title)
306 :subject => l(:mail_subject_register, Setting.app_title)
307 end
307 end
308
308
309 def test_email(user)
309 def test_email(user)
310 set_language_if_valid(user.language)
310 set_language_if_valid(user.language)
311 @url = url_for(:controller => 'welcome')
311 @url = url_for(:controller => 'welcome')
312 mail :to => user.mail,
312 mail :to => user.mail,
313 :subject => 'Redmine test'
313 :subject => 'Redmine test'
314 end
314 end
315
315
316 # Sends reminders to issue assignees
316 # Sends reminders to issue assignees
317 # Available options:
317 # Available options:
318 # * :days => how many days in the future to remind about (defaults to 7)
318 # * :days => how many days in the future to remind about (defaults to 7)
319 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
319 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
320 # * :project => id or identifier of project to process (defaults to all projects)
320 # * :project => id or identifier of project to process (defaults to all projects)
321 # * :users => array of user/group ids who should be reminded
321 # * :users => array of user/group ids who should be reminded
322 # * :version => name of target version for filtering issues (defaults to none)
322 # * :version => name of target version for filtering issues (defaults to none)
323 def self.reminders(options={})
323 def self.reminders(options={})
324 days = options[:days] || 7
324 days = options[:days] || 7
325 project = options[:project] ? Project.find(options[:project]) : nil
325 project = options[:project] ? Project.find(options[:project]) : nil
326 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
326 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
327 target_versions = options[:version] ? Version.where(name: options[:version]).select(:id).map(&:id) : nil
327 target_version_id = options[:version] ? Version.named(options[:version]).pluck(:id) : nil
328 if options[:version] && target_version_id.blank?
329 raise ActiveRecord::RecordNotFound.new("Couldn't find Version with named #{options[:version]}")
330 end
328 user_ids = options[:users]
331 user_ids = options[:users]
329
332
330 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
333 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
331 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
334 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
332 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
335 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
333 )
336 )
334 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
337 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
335 scope = scope.where(:project_id => project.id) if project
338 scope = scope.where(:project_id => project.id) if project
336 scope = scope.where(:fixed_version_id => target_versions) unless target_versions.blank?
339 scope = scope.where(:fixed_version_id => target_version_id) if target_version_id.present?
337 scope = scope.where(:tracker_id => tracker.id) if tracker
340 scope = scope.where(:tracker_id => tracker.id) if tracker
338 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
341 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
339 group_by(&:assigned_to)
342 group_by(&:assigned_to)
340 issues_by_assignee.keys.each do |assignee|
343 issues_by_assignee.keys.each do |assignee|
341 if assignee.is_a?(Group)
344 if assignee.is_a?(Group)
342 assignee.users.each do |user|
345 assignee.users.each do |user|
343 issues_by_assignee[user] ||= []
346 issues_by_assignee[user] ||= []
344 issues_by_assignee[user] += issues_by_assignee[assignee]
347 issues_by_assignee[user] += issues_by_assignee[assignee]
345 end
348 end
346 end
349 end
347 end
350 end
348
351
349 issues_by_assignee.each do |assignee, issues|
352 issues_by_assignee.each do |assignee, issues|
350 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
353 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
351 end
354 end
352 end
355 end
353
356
354 # Activates/desactivates email deliveries during +block+
357 # Activates/desactivates email deliveries during +block+
355 def self.with_deliveries(enabled = true, &block)
358 def self.with_deliveries(enabled = true, &block)
356 was_enabled = ActionMailer::Base.perform_deliveries
359 was_enabled = ActionMailer::Base.perform_deliveries
357 ActionMailer::Base.perform_deliveries = !!enabled
360 ActionMailer::Base.perform_deliveries = !!enabled
358 yield
361 yield
359 ensure
362 ensure
360 ActionMailer::Base.perform_deliveries = was_enabled
363 ActionMailer::Base.perform_deliveries = was_enabled
361 end
364 end
362
365
363 # Sends emails synchronously in the given block
366 # Sends emails synchronously in the given block
364 def self.with_synched_deliveries(&block)
367 def self.with_synched_deliveries(&block)
365 saved_method = ActionMailer::Base.delivery_method
368 saved_method = ActionMailer::Base.delivery_method
366 if m = saved_method.to_s.match(%r{^async_(.+)$})
369 if m = saved_method.to_s.match(%r{^async_(.+)$})
367 synched_method = m[1]
370 synched_method = m[1]
368 ActionMailer::Base.delivery_method = synched_method.to_sym
371 ActionMailer::Base.delivery_method = synched_method.to_sym
369 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
372 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
370 end
373 end
371 yield
374 yield
372 ensure
375 ensure
373 ActionMailer::Base.delivery_method = saved_method
376 ActionMailer::Base.delivery_method = saved_method
374 end
377 end
375
378
376 def mail(headers={}, &block)
379 def mail(headers={}, &block)
377 headers.reverse_merge! 'X-Mailer' => 'Redmine',
380 headers.reverse_merge! 'X-Mailer' => 'Redmine',
378 'X-Redmine-Host' => Setting.host_name,
381 'X-Redmine-Host' => Setting.host_name,
379 'X-Redmine-Site' => Setting.app_title,
382 'X-Redmine-Site' => Setting.app_title,
380 'X-Auto-Response-Suppress' => 'OOF',
383 'X-Auto-Response-Suppress' => 'OOF',
381 'Auto-Submitted' => 'auto-generated',
384 'Auto-Submitted' => 'auto-generated',
382 'From' => Setting.mail_from,
385 'From' => Setting.mail_from,
383 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
386 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
384
387
385 # Replaces users with their email addresses
388 # Replaces users with their email addresses
386 [:to, :cc, :bcc].each do |key|
389 [:to, :cc, :bcc].each do |key|
387 if headers[key].present?
390 if headers[key].present?
388 headers[key] = self.class.email_addresses(headers[key])
391 headers[key] = self.class.email_addresses(headers[key])
389 end
392 end
390 end
393 end
391
394
392 # Removes the author from the recipients and cc
395 # Removes the author from the recipients and cc
393 # if the author does not want to receive notifications
396 # if the author does not want to receive notifications
394 # about what the author do
397 # about what the author do
395 if @author && @author.logged? && @author.pref.no_self_notified
398 if @author && @author.logged? && @author.pref.no_self_notified
396 addresses = @author.mails
399 addresses = @author.mails
397 headers[:to] -= addresses if headers[:to].is_a?(Array)
400 headers[:to] -= addresses if headers[:to].is_a?(Array)
398 headers[:cc] -= addresses if headers[:cc].is_a?(Array)
401 headers[:cc] -= addresses if headers[:cc].is_a?(Array)
399 end
402 end
400
403
401 if @author && @author.logged?
404 if @author && @author.logged?
402 redmine_headers 'Sender' => @author.login
405 redmine_headers 'Sender' => @author.login
403 end
406 end
404
407
405 # Blind carbon copy recipients
408 # Blind carbon copy recipients
406 if Setting.bcc_recipients?
409 if Setting.bcc_recipients?
407 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
410 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
408 headers[:to] = nil
411 headers[:to] = nil
409 headers[:cc] = nil
412 headers[:cc] = nil
410 end
413 end
411
414
412 if @message_id_object
415 if @message_id_object
413 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
416 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
414 end
417 end
415 if @references_objects
418 if @references_objects
416 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
419 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
417 end
420 end
418
421
419 m = if block_given?
422 m = if block_given?
420 super headers, &block
423 super headers, &block
421 else
424 else
422 super headers do |format|
425 super headers do |format|
423 format.text
426 format.text
424 format.html unless Setting.plain_text_mail?
427 format.html unless Setting.plain_text_mail?
425 end
428 end
426 end
429 end
427 set_language_if_valid @initial_language
430 set_language_if_valid @initial_language
428
431
429 m
432 m
430 end
433 end
431
434
432 def initialize(*args)
435 def initialize(*args)
433 @initial_language = current_language
436 @initial_language = current_language
434 set_language_if_valid Setting.default_language
437 set_language_if_valid Setting.default_language
435 super
438 super
436 end
439 end
437
440
438 def self.deliver_mail(mail)
441 def self.deliver_mail(mail)
439 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
442 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
440 begin
443 begin
441 # Log errors when raise_delivery_errors is set to false, Rails does not
444 # Log errors when raise_delivery_errors is set to false, Rails does not
442 mail.raise_delivery_errors = true
445 mail.raise_delivery_errors = true
443 super
446 super
444 rescue Exception => e
447 rescue Exception => e
445 if ActionMailer::Base.raise_delivery_errors
448 if ActionMailer::Base.raise_delivery_errors
446 raise e
449 raise e
447 else
450 else
448 Rails.logger.error "Email delivery error: #{e.message}"
451 Rails.logger.error "Email delivery error: #{e.message}"
449 end
452 end
450 end
453 end
451 end
454 end
452
455
453 def self.method_missing(method, *args, &block)
456 def self.method_missing(method, *args, &block)
454 if m = method.to_s.match(%r{^deliver_(.+)$})
457 if m = method.to_s.match(%r{^deliver_(.+)$})
455 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
458 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
456 send(m[1], *args).deliver
459 send(m[1], *args).deliver
457 else
460 else
458 super
461 super
459 end
462 end
460 end
463 end
461
464
462 # Returns an array of email addresses to notify by
465 # Returns an array of email addresses to notify by
463 # replacing users in arg with their notified email addresses
466 # replacing users in arg with their notified email addresses
464 #
467 #
465 # Example:
468 # Example:
466 # Mailer.email_addresses(users)
469 # Mailer.email_addresses(users)
467 # => ["foo@example.net", "bar@example.net"]
470 # => ["foo@example.net", "bar@example.net"]
468 def self.email_addresses(arg)
471 def self.email_addresses(arg)
469 arr = Array.wrap(arg)
472 arr = Array.wrap(arg)
470 mails = arr.reject {|a| a.is_a? Principal}
473 mails = arr.reject {|a| a.is_a? Principal}
471 users = arr - mails
474 users = arr - mails
472 if users.any?
475 if users.any?
473 mails += EmailAddress.
476 mails += EmailAddress.
474 where(:user_id => users.map(&:id)).
477 where(:user_id => users.map(&:id)).
475 where("is_default = ? OR notify = ?", true, true).
478 where("is_default = ? OR notify = ?", true, true).
476 pluck(:address)
479 pluck(:address)
477 end
480 end
478 mails
481 mails
479 end
482 end
480
483
481 private
484 private
482
485
483 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
486 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
484 def redmine_headers(h)
487 def redmine_headers(h)
485 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
488 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
486 end
489 end
487
490
488 def self.token_for(object, rand=true)
491 def self.token_for(object, rand=true)
489 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
492 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
490 hash = [
493 hash = [
491 "redmine",
494 "redmine",
492 "#{object.class.name.demodulize.underscore}-#{object.id}",
495 "#{object.class.name.demodulize.underscore}-#{object.id}",
493 timestamp.strftime("%Y%m%d%H%M%S")
496 timestamp.strftime("%Y%m%d%H%M%S")
494 ]
497 ]
495 if rand
498 if rand
496 hash << Redmine::Utils.random_hex(8)
499 hash << Redmine::Utils.random_hex(8)
497 end
500 end
498 host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
501 host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
499 host = "#{::Socket.gethostname}.redmine" if host.empty?
502 host = "#{::Socket.gethostname}.redmine" if host.empty?
500 "#{hash.join('.')}@#{host}"
503 "#{hash.join('.')}@#{host}"
501 end
504 end
502
505
503 # Returns a Message-Id for the given object
506 # Returns a Message-Id for the given object
504 def self.message_id_for(object)
507 def self.message_id_for(object)
505 token_for(object, true)
508 token_for(object, true)
506 end
509 end
507
510
508 # Returns a uniq token for a given object referenced by all notifications
511 # Returns a uniq token for a given object referenced by all notifications
509 # related to this object
512 # related to this object
510 def self.references_for(object)
513 def self.references_for(object)
511 token_for(object, false)
514 token_for(object, false)
512 end
515 end
513
516
514 def message_id(object)
517 def message_id(object)
515 @message_id_object = object
518 @message_id_object = object
516 end
519 end
517
520
518 def references(object)
521 def references(object)
519 @references_objects ||= []
522 @references_objects ||= []
520 @references_objects << object
523 @references_objects << object
521 end
524 end
522
525
523 def mylogger
526 def mylogger
524 Rails.logger
527 Rails.logger
525 end
528 end
526 end
529 end
General Comments 0
You need to be logged in to leave comments. Login now