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