Module: Sendmail
- Defined in:
- lib/egalite/sendmail.rb
Overview
Defined Under Namespace
Classes: AttachmentsTooLarge, QualifiedMailbox
Class Attribute Summary collapse
Class Method Summary
collapse
-
._send(text, envelope_from, to, host = 'localhost') ⇒ Object
-
.address(addrspec, name = nil, header = 'Reply-to') ⇒ Object
-
.atext ⇒ Object
-
.atext_loose ⇒ Object
-
.check_domain(s) ⇒ Object
-
.check_local_loose(s) ⇒ Object
-
.encode_phrase(header, s) ⇒ Object
-
.encode_unstructured(header, s) ⇒ Object
-
.folding(h, s) ⇒ Object
-
.mailboxlist(value, header = 'Reply-to') ⇒ Object
-
.message(body, params) ⇒ Object
-
.mime_combine(parts, type = "mixed") ⇒ Object
-
.mime_header(header, s) ⇒ Object
-
.mime_part(body, content_type = nil, filename = nil) ⇒ Object
-
.multibyte?(s) ⇒ Boolean
-
.multibyte_folding(h, s, encoding = 'UTF-8') ⇒ Object
-
.parse_addrspec(addrspec) ⇒ Object
-
.quote_string(s) ⇒ Object
-
.read_private_key(pem_filename) ⇒ Object
-
.send(body, params, host = 'localhost') ⇒ Object
-
.send_inner_2(body, params, host, dkim, dkim_params) ⇒ Object
-
.send_multipart(parts, params, host = 'localhost') ⇒ Object
-
.send_with_dkim(body, params, host = 'localhost', dkim_params = {}) ⇒ Object
-
.send_with_template(filename, params, host = 'localhost') ⇒ Object
-
.send_with_uploaded_files(body, files, params, host = 'localhost') ⇒ Object
-
.to_addresses(params) ⇒ Object
-
.vchar(s) ⇒ Object
-
.verify_address(email) ⇒ Object
check validity of email address with DNS lookup.
-
.wsp(s) ⇒ Object
Class Attribute Details
.force_dkim ⇒ Object
Returns the value of attribute force_dkim.
37
38
39
|
# File 'lib/egalite/sendmail.rb', line 37
def force_dkim
@force_dkim
end
|
Returns the value of attribute lastmail.
38
39
40
|
# File 'lib/egalite/sendmail.rb', line 38
def lastmail
@lastmail
end
|
Returns the value of attribute mock.
37
38
39
|
# File 'lib/egalite/sendmail.rb', line 37
def mock
@mock
end
|
Class Method Details
._send(text, envelope_from, to, host = 'localhost') ⇒ Object
217
218
219
220
221
222
223
224
225
|
# File 'lib/egalite/sendmail.rb', line 217
def _send(text, envelope_from, to, host = 'localhost')
if @mock
@lastmail = [text, envelope_from, to, host]
else
Net::SMTP.start(host) { |smtp|
smtp.send_message(text, envelope_from, to)
}
end
end
|
.address(addrspec, name = nil, header = 'Reply-to') ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/egalite/sendmail.rb', line 128
def address(addrspec, name = nil, ='Reply-to')
raise 'invalid mail address.' unless parse_addrspec(addrspec)
if name and name.size > 0
QualifiedMailbox.new(folding(, "#{encode_phrase(, name)} <#{addrspec}>"))
else
addrspec
end
end
|
104
|
# File 'lib/egalite/sendmail.rb', line 104
def atext; '[0-9a-zA-Z!#$%&\'*+\-/=?\^_`{|}~]'; end
|
.atext_loose ⇒ Object
105
|
# File 'lib/egalite/sendmail.rb', line 105
def atext_loose; '[0-9a-zA-Z!#$%&\'*+\-/=?\^_`{|}~.]'; end
|
.check_domain(s) ⇒ Object
107
108
109
|
# File 'lib/egalite/sendmail.rb', line 107
def check_domain(s)
s =~ /\A#{atext}+?(\.#{atext}+?)+\Z/
end
|
.check_local_loose(s) ⇒ Object
110
111
112
|
# File 'lib/egalite/sendmail.rb', line 110
def check_local_loose(s)
s =~ /\A#{atext_loose}+\Z/
end
|
.encode_phrase(header, s) ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/egalite/sendmail.rb', line 90
def encode_phrase(, s)
if multibyte?(s)
multibyte_folding(, s)
else
folding(, quote_string(s))
end
end
|
.encode_unstructured(header, s) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/egalite/sendmail.rb', line 97
def encode_unstructured(, s)
if multibyte?(s)
multibyte_folding(, s)
else
folding(, vchar(wsp(s)))
end
end
|
.folding(h, s) ⇒ Object
folding white space. see RFC5322, section 2.3.3 and 3.2.2.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/egalite/sendmail.rb', line 39
def folding(h, s) len = 78 - h.size - ": ".size
len2nd = 78 - " ".size
lines = []
line = ""
s.strip.split(/\s+/).each { |c| if (line+c).size > len
len = len2nd
lines << line.strip
line = c + " "
else
line << c + " "
end
}
lines << line.strip if line.size > 0
lines.join("\n ")
end
|
.mailboxlist(value, header = 'Reply-to') ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/egalite/sendmail.rb', line 137
def mailboxlist(value, = 'Reply-to')
case value
when QualifiedMailbox
value
when String
parse_addrspec(value) ? value : nil
when Hash
folding(, value.map { |name, address|
address(address,name)
}.join(', '))
when Array
folding(, value.map { |v|
v.is_a?(Array) ? address(v[0],v[1]) : mailboxlist(v,)
}.join(', '))
else
nil
end
end
|
.message(body, params) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/egalite/sendmail.rb', line 155
def message(body, params)
= {}
raise "From must be exist." unless params[:from]
raise "The number of sender must be zero or one." if params[:sender].is_a?(Array) and params[:sender].size > 1
raise "When the number of 'from' is more than one, sender must be exist" if params[:from].is_a?(Array) and params[:from].size > 1 and not params[:sender]
%w[From To Sender Reply-To Cc].each { |s|
v = params[s.gsub(/-/,'_').downcase.to_sym]
[s] = mailboxlist(v,s) if v and v.size >= 1
}
["Subject"] = encode_unstructured("Subject",params[:subject].to_s) if params[:subject]
["MIME-Version"] = "1.0"
date = params[:date] || Time.now
["Date"] = date.is_a?(Time) ? date.rfc822 : date
["Content-Type"] = params[:content_type]
["Content-Type"] ||= "text/plain; charset=UTF-8"
if params[:content_type] =~ /multipart/
body = body
elsif multibyte?(body)
["Content-Transfer-Encoding"] = "base64"
body = [body].pack('m')
else
["Content-Transfer-Encoding"] = "7bit"
end
params[:headers].each_pair { |k,v| [k] = v unless .key?(k)} if params.key?(:headers)
text = [.map{|k,v| "#{k}: #{v}"}.join("\n"),body].join("\n\n")
end
|
.mime_combine(parts, type = "mixed") ⇒ Object
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/egalite/sendmail.rb', line 298
def mime_combine(parts, type = "mixed")
boundary = OpenSSL::Random.random_bytes(10).unpack('h*')[0]
content_type = "multipart/#{type}; boundary=\"#{boundary}\""
body = ""
parts.each { |part|
body << "--" + boundary
body << "\n"
body << part
}
body << "--" + boundary
body << "--\n"
[body, content_type]
end
|
312
313
314
315
316
317
318
|
# File 'lib/egalite/sendmail.rb', line 312
def (,s)
if multibyte?(s)
+ '"' + multibyte_folding(, s) + '"'
else
+ folding(, quote_string(s))
end
end
|
.mime_part(body, content_type = nil, filename = nil) ⇒ Object
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/egalite/sendmail.rb', line 319
def mime_part(body, content_type = nil, filename = nil)
content_type = "text/plain; charset=UTF-8" unless content_type
part = ""
if filename
part << "Content-Type: #{content_type};\n"
part << (" name=", filename)
part << "\n"
part << "Content-Disposition: attachment;\n"
part << (" filename=", filename)
part << "\n"
part << "Content-Transfer-Encoding: base64\n"
else
part << "Content-Type: #{content_type}\n"
part << "Content-Transfer-Encoding: base64\n"
end
part << "\n"
part << [body].pack('m')
end
|
.multibyte?(s) ⇒ Boolean
87
88
89
|
# File 'lib/egalite/sendmail.rb', line 87
def multibyte?(s)
s.each_byte.any? { |c| c > 0x7f }
end
|
.multibyte_folding(h, s, encoding = 'UTF-8') ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/egalite/sendmail.rb', line 56
def multibyte_folding(h, s, encoding = 'UTF-8') bracketsize = "=?#{encoding}?B??=".size
len = 76 - h.size - ": ".size - bracketsize
len2nd = 76 - bracketsize
lines = []
line = ""
s = s.gsub(/\s+/, ' ').strip
s.split(//).each { |c| teststr = line+c
teststr = NKF.nkf('-Wj',teststr) if encoding =~ /iso-2022-jp/i
if [teststr].pack('m').chomp.size > len
len = len2nd
lines << line
line = c
else
line << c
end
}
lines << line if line.size > 0
lines = lines.map { |s| "=?#{encoding}?B?#{[s].pack('m').gsub(/\n/,'')}?=" }
lines.join("\n ")
end
|
.parse_addrspec(addrspec) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/egalite/sendmail.rb', line 113
def parse_addrspec(addrspec)
if addrspec[0,1] == '"' addrspec =~ /\A(\".*?[^\\]\")\@(.+)\Z/
(local, domain) = [$1, $2]
return nil if local =~ /[\x00-\x1f\x7f]/
return nil unless check_domain(domain)
[local, domain]
else
(local, domain) = addrspec.split(/@/,2)
return nil unless check_local_loose(local)
return nil unless check_domain(domain)
[local, domain]
end
end
|
.quote_string(s) ⇒ Object
84
85
86
|
# File 'lib/egalite/sendmail.rb', line 84
def quote_string(s)
'"' + vchar(wsp(s)).gsub(/\\/,"\\\\\\").gsub(/\"/,'\\\"') + '"'
end
|
.read_private_key(pem_filename) ⇒ Object
226
227
228
|
# File 'lib/egalite/sendmail.rb', line 226
def read_private_key(pem_filename)
OpenSSL::PKey::RSA.new(open(pem_filename).read)
end
|
.send(body, params, host = 'localhost') ⇒ Object
243
244
245
|
# File 'lib/egalite/sendmail.rb', line 243
def send(body, params, host = 'localhost')
send_inner_2(body, params, host, @force_dkim, {})
end
|
.send_inner_2(body, params, host, dkim, dkim_params) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/egalite/sendmail.rb', line 229
def send_inner_2(body, params, host, dkim, dkim_params)
text = message(body, params)
if dkim
text = Dkim.sign(text,dkim_params)
end
envelope_from = (params[:envelope_from] || params[:sender] || params[:from])
envelope_from = envelope_from[0] if envelope_from.is_a?(Array)
_send(
text,
envelope_from,
to_addresses(params),
host
)
end
|
.send_multipart(parts, params, host = 'localhost') ⇒ Object
249
250
251
252
253
|
# File 'lib/egalite/sendmail.rb', line 249
def send_multipart(parts, params, host = 'localhost')
(body, content_type) = mime_combine(parts)
params[:content_type] = content_type
send_inner_2(body, params, host, @force_dkim, {})
end
|
.send_with_dkim(body, params, host = 'localhost', dkim_params = {}) ⇒ Object
246
247
248
|
# File 'lib/egalite/sendmail.rb', line 246
def send_with_dkim(body, params, host = 'localhost', dkim_params = {})
send_inner_2(body, params, host, true, dkim_params)
end
|
.send_with_template(filename, params, host = 'localhost') ⇒ Object
269
270
271
272
273
274
275
276
277
|
# File 'lib/egalite/sendmail.rb', line 269
def send_with_template(filename, params, host = 'localhost')
File.open("mail/"+ filename ,"r") { |f|
text = f.read
tengine = Egalite::HTMLTemplate.new
tengine.default_escape = false
text = tengine.handleTemplate(text,params)
send(text, params, host)
}
end
|
.send_with_uploaded_files(body, files, params, host = 'localhost') ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/egalite/sendmail.rb', line 254
def send_with_uploaded_files(body, files, params, host = 'localhost')
sum_size = 0
files = [files].flatten
parts = [mime_part(body)]
parts += files.map { |f|
File.open(f[:tempfile].path, "r") {|tf|
binary = tf.read
sum_size += binary.size
raise AttachmentsTooLarge if sum_size >= 25 * 1000 * 1000
mime_part(binary, f[:type], f[:filename])
}
}
send_multipart(parts, params, host)
end
|
.to_addresses(params) ⇒ Object
211
212
213
214
215
216
|
# File 'lib/egalite/sendmail.rb', line 211
def to_addresses(params)
addresses = [:to, :cc, :bcc].map { |s|
(params[s])
}
addresses.flatten.compact.uniq
end
|
78
79
80
|
# File 'lib/egalite/sendmail.rb', line 78
def vchar(s)
s.gsub(/[\x00-\x1f\x7f]/,'')
end
|
.verify_address(email) ⇒ Object
check validity of email address with DNS lookup.
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/egalite/sendmail.rb', line 282
def verify_address(email)
(local,domain) = parse_addrspec(email)
return false unless domain
mx = Resolv::DNS.new.getresource(domain, Resolv::DNS::Resource::IN::MX) rescue nil
return true if mx
a = Resolv::DNS.new.getresource(domain, Resolv::DNS::Resource::IN::A) rescue nil
return true if a
aaaa = Resolv::DNS.new.getresource(domain, Resolv::DNS::Resource::IN::AAAA) rescue nil
return true if aaaa
false
end
|
81
82
83
|
# File 'lib/egalite/sendmail.rb', line 81
def wsp(s)
s.gsub(/\s+/,' ')
end
|