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