3 = Net provides SMTP wrapping
7 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
29 # with permission of Minero Aoki.
45 def send_text_to( smtp )
52 def do_send_to( smtp )
53 from = from_address or raise ArgumentError, 'no from address'
54 (dests = destinations).empty? and raise ArgumentError, 'no receipient'
56 send_to_0 smtp, from, dests
60 def send_to_0( smtp, from, to )
61 smtp.ready(from, to) do |f|
62 encoded "\r\n", 'j', f, ''
77 def delete_no_send_fields
78 NOSEND_FIELDS.each do |nm|
81 delete_if {|n,v| v.empty? }
84 def add_message_id( fqdn = nil )
85 self.message_id = ::TMail::new_message_id(fqdn)
94 mime_encode_singlepart
96 mime_encode_multipart true
100 def mime_encode_singlepart
101 self.mime_version = '1.0'
103 if NKF.guess(b) != NKF::BINARY
110 def mime_encode_text( body )
111 self.body = NKF.nkf('-j -m0', body)
112 self.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
113 self.encoding = '7bit'
116 def mime_encode_binary( body )
117 self.body = [body].pack('m')
118 self.set_content_type 'application', 'octet-stream'
119 self.encoding = 'Base64'
122 def mime_encode_multipart( top = true )
123 self.mime_version = '1.0' if top
124 self.set_content_type 'multipart', 'mixed'
126 if e and not /\A(?:7bit|8bit|binary)\z/i === e
128 'using C.T.Encoding with multipart mail is not permitted'
132 def create_empty_mail
133 self.class.new(StringPort.new(''), @config)
137 setup_reply create_empty_mail()
141 if tmp = reply_addresses(nil)
145 mid = message_id(nil)
146 tmp = references(nil) || []
148 m.in_reply_to = [mid] if mid
149 m.references = tmp unless tmp.empty?
150 m.subject = 'Re: ' + subject('').sub(/\A(?:\s*re:)+/i, '')
156 setup_forward create_empty_mail()
159 def setup_forward( mail )
160 m = Mail.new(StringPort.new(''))
162 m.set_content_type 'message', 'rfc822'
163 m.encoding = encoding('7bit')
177 def initialize( nosend = nil, delempty = true )
178 @no_send_fields = nosend || NOSEND_FIELDS.dup
179 @delete_empty_fields = delempty
183 attr :delete_empty_fields, true
186 @no_send_fields.each do |nm|
189 delete_if {|n,v| v.empty? } if @delete_empty_fields
197 def initialize( fqdn = nil )
204 mail.message_id = ::TMail::new_msgid(@fqdn)
221 def initialize( s = nil, m = nil )
222 @singlepart_composer = s || MimeEncodeSingle.new
223 @multipart_composer = m || MimeEncodeMulti.new
226 attr :singlepart_composer
227 attr :multipart_composer
230 if mail._builtin_multipart?
231 then @multipart_composer
232 else @singlepart_composer end.exec mail
238 class MimeEncodeSingle
241 mail.mime_version = '1.0'
243 if NKF.guess(b) != NKF::BINARY
251 mail.body = NKF.nkf('-j -m0', body)
252 mail.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
253 mail.encoding = '7bit'
256 def on_binary( body )
257 mail.body = [body].pack('m')
258 mail.set_content_type 'application', 'octet-stream'
259 mail.encoding = 'Base64'
265 class MimeEncodeMulti
267 def exec( mail, top = true )
268 mail.mime_version = '1.0' if top
269 mail.set_content_type 'multipart', 'mixed'
271 if e and not /\A(?:7bit|8bit|binary)\z/i === e
273 'using C.T.Encoding with multipart mail is not permitted'
275 mail.parts.each do |m|
276 exec m, false if m._builtin_multipart?