-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmailer_patch.rb
89 lines (77 loc) · 2.78 KB
/
mailer_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module RedminePerProjectSender
module MailerPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
alias_method_chain :mail, :project_sender
end
end
module InstanceMethods
# Overrides the mail method trying to
# get per project mail from configuration
def mail_with_project_sender(headers={}, &block)
headers.reverse_merge! 'X-Mailer' => 'Redmine',
'X-Redmine-Host' => Setting.host_name,
'X-Redmine-Site' => Setting.app_title,
'X-Auto-Response-Suppress' => 'All',
'Auto-Submitted' => 'auto-generated',
'From' => mail_from_project_sender,
'List-Id' => "<#{mail_from_project_sender.to_s.gsub('@', '.')}>"
# Replaces users with their email addresses
[:to, :cc, :bcc].each do |key|
if headers[key].present?
headers[key] = self.class.email_addresses(headers[key])
end
end
# Removes the author from the recipients and cc
# if the author does not want to receive notifications
# about what the author do
if @author && @author.logged? && @author.pref.no_self_notified
addresses = @author.mails
headers[:to] -= addresses if headers[:to].is_a?(Array)
headers[:cc] -= addresses if headers[:cc].is_a?(Array)
end
if @author && @author.logged?
redmine_headers 'Sender' => @author.login
end
# Blind carbon copy recipients
if Setting.bcc_recipients?
headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
headers[:to] = nil
headers[:cc] = nil
end
if @message_id_object
headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
end
if @references_objects
headers[:references] = @references_objects.collect {|o| "<#{self.class.message_id_for(o)}>"}.join(' ')
end
m = if block_given?
super headers, &block
else
super headers do |format|
format.text
format.html unless Setting.plain_text_mail?
end
end
set_language_if_valid @initial_language
m
end
private
# try to return mail_from project configuration
def mail_from_project_sender
sender = if @issue
p = @issue.project
s = CustomField.find_by_name('project-sender-email')
p.custom_value_for(s).try(:value) if p.present? && s.present?
end
(sender.present? && sender) || Setting.mail_from
end
end # module InstanceMethods
end # module MailerPatch
end # module RedminePerProjectSender
# Add module to Mailer class
Mailer.send(:include, RedminePerProjectSender::MailerPatch)
class ActionMailer::Base
alias_method :mail_with_project_sender, :mail
end