forked from zdavatz/oddb.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.rb
170 lines (157 loc) · 6.34 KB
/
mail.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby
# encoding: utf-8
# Small helper class to unify sending mails
require 'mail'
require 'config'
require 'util/logfile'
require 'yaml'
module ODDB
module Util
# see also the file test/data/oddb_mailing_test.yml
MailingTestConfiguration = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test', 'data', 'oddb_mailing_test.yml'))
MailingDefaultConfiguration = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'etc', 'oddb.yml'))
MailingListIds = 'mailling_list_ids'
MailingRecipients = 'mail_recipients'
@mail_configured = false
@mailing_list_configuration = MailingTestConfiguration
def Util.use_mailing_list_configuration(path)
@mailing_list_configuration = path
end
def Util.mailing_configuration_file
@mailing_list_configuration
end
def Util.mail_from
Util.configure_mail unless @mail_configured
@cfg['mail_from']
end
def Util.mail_to
Util.configure_mail unless @mail_configured
@cfg['mail_to']
end
def Util.get_mailing_list_receivers(list_id)
Util.configure_mail unless @mail_configured
return [] unless @cfg and @cfg[MailingListIds] and @cfg[MailingListIds].index(list_id)
receivers = []
@cfg[MailingRecipients].each { |recipient| receivers << recipient[:email] if recipient[:lists].index(list_id) }
receivers.sort
end
def Util.get_mailing_list_anrede(list_id)
Util.configure_mail unless @mail_configured
return [] unless @cfg and @cfg[MailingListIds] and @cfg[MailingListIds].index(list_id)
anreden = []
@cfg[MailingRecipients].each { |recipient| anreden << recipient[:anrede] if recipient[:lists].index(list_id) and recipient[:anrede]}
anreden.sort
end
# one time initialisation for delivering according to setup in etc/oddb.yml
# can be overriden by calling Util.configure_mail(:test)
# return default_from address
def Util.configure_mail(deliver_using = :oddb_yml)
return if @mail_configured == deliver_using
@mail_configured = deliver_using
if deliver_using == :test
@mailing_list_configuration = MailingTestConfiguration
@cfg = YAML.load_file(@mailing_list_configuration)
Mail.defaults do delivery_method :test end
else
@mailing_list_configuration = MailingDefaultConfiguration
unless File.exists?(@mailing_list_configuration)
@cfg = nil
else
@cfg = YAML.load_file(@mailing_list_configuration)
cfg = @cfg.clone
Mail.defaults do
delivery_method :smtp, {
:address => cfg['smtp_server'],
:port => cfg['smtp_port'],
:domain => cfg['smtp_domain'],
:user_name => cfg['smtp_user'],
:password => cfg['smtp_pass'],
:authentication => cfg['smtp_auth']
}
end
end
end
msg = "#{__FILE__}: Configured email using #{@cfg ? @cfg['smtp_server'].inspect : 'nil' } #{@cfg ? @cfg['smtp_port'].inspect : ''} #{@cfg ? @cfg['smtp_user'].inspect : ''}"
Util.debug_msg(msg)
@mail_configured
end
# Parts must be of form content_type => body, e.g. 'text/html; charset=UTF-8' => '<h1>This is HTML</h1>'
def Util.send_mail(list_and_recipients, mail_subject, mail_body, override_from = nil, parts = {})
LogFile.append('oddb/debug', "Util.send_mail list_and_recipients #{list_and_recipients}", Time.now)
recipients = Util.check_and_get_all_recipients(list_and_recipients)
mail = Mail.new
mail.from override_from ? override_from : @cfg['mail_from']
mail.to recipients
mail.subject mail_subject
mail.body mail_body
log_and_deliver_mail(mail)
rescue => e
msg = "Util.send_mail rescue: error is #{e.inspect}"
Util.debug_msg(msg)
raise e
end
def Util.send_mail_with_attachments(list_and_recipients, subject, body, attachments)
LogFile.append('oddb/debug', "Util.send_mail send_mail_with_attachments #{list_and_recipients}", Time.now)
recipients = Util.check_and_get_all_recipients(list_and_recipients)
mail = Mail.new
mail.from = @cfg['mail_from']
mail.to recipients
mail.subject = subject
mail.body = body
attachments.each { |attachment|
mail.attachments[attachment[:filename]] = {
:mime_type => attachment[:mime_type],
:content => attachment[:content],
}
}
log_and_deliver_mail(mail)
rescue => e
msg = "Util.send_mail_with_attachments rescue: error is #{e.inspect}"
Util.debug_msg(msg)
raise e
end
# Utility methods for checking mails in unit-tests
def Util.sent_mails
Mail::TestMailer.deliveries
end
# Utility methods for clearing mails in unit-tests
def Util.clear_sent_mails
Mail::TestMailer.deliveries.clear
end
private
def Util.check_and_get_all_recipients(list_and_recipients)
Util.configure_mail unless @mail_configured
recipients = []
if list_and_recipients and list_and_recipients.is_a?(Array)
foundList = false
list_and_recipients.each{
|id|
recvs = Util.get_mailing_list_receivers(id)
if recvs.size > 0
foundList = true
recipients += recvs
else
recipients << id
end
}
else
recipients = Util.get_mailing_list_receivers(list_and_recipients)
foundList = recipients.size > 0
end
raise "At least one recipient must be a list #{list_and_recipients}" unless foundList
raise "No recipients defined for list_and_recipients #{list_and_recipients}" unless recipients.size > 0
recipients.sort
end
def Util.log_and_deliver_mail(mail)
Util.configure_mail unless @mail_configured
mail.from << @cfg.mail_from unless mail.from.size > 0
Util.debug_msg("Util.log_and_deliver_mail to=#{mail.to} subject #{mail.subject} size #{mail.body.inspect}")
mail.deliver
end
def Util.debug_msg(msg)
LogFile.append('oddb/debug', ' ' + msg, Time.now)
system("logger '#{msg}'")
$stderr.puts msg unless defined?(MiniTest)
end
end
end