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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
=begin
= net/smtp.rb
Copyright (c) 1999-2003 Yukihiro Matsumoto
Copyright (c) 1999-2003 Minero Aoki
written & maintained by Minero Aoki <aamine@loveruby.net>
This program is free software. You can re-distribute and/or
modify this program under the same terms as Ruby itself,
Ruby Distribute License or GNU General Public License.
NOTE: You can find Japanese version of this document in
the doc/net directory of the standard ruby interpreter package.
$Id$
== What is This Module?
This module provides your program the functions to send internet
mail via SMTP, Simple Mail Transfer Protocol. For details of
SMTP itself, refer [RFC2821] ((<URL:http://www.ietf.org/rfc/rfc2821.txt>)).
== What is NOT This Module?
This module does NOT provide functions to compose internet mails.
You must create it by yourself. If you want better mail support,
try RubyMail or TMail. You can get both libraries from RAA.
((<URL:http://www.ruby-lang.org/en/raa.html>))
FYI: official documentation of internet mail is:
[RFC2822] ((<URL:http://www.ietf.org/rfc/rfc2822.txt>)).
== Examples
=== Sending Message
You must open connection to SMTP server before sending messages.
First argument is the address of SMTP server, and second argument
is port number. Using SMTP.start with block is the most simple way
to do it. SMTP connection is closed automatically after block is
executed.
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) {|smtp|
# use a SMTP object only in this block
}
Replace 'your.smtp.server' by your SMTP server. Normally
your system manager or internet provider is supplying a server
for you.
Then you can send messages.
msgstr = <<END_OF_MESSAGE
From: Your Name <your@mail.address>
To: Destination Address <someone@example.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>
This is a test message.
END_OF_MESSAGE
require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) {|smtp|
smtp.send_message msgstr,
'your@mail.address',
'his_addess@example.com'
}
=== Closing Session
You MUST close SMTP session after sending messages, by calling #finish
method:
# using SMTP#finish
smtp = Net::SMTP.start('your.smtp.server', 25)
smtp.send_message msgstr, 'from@address', 'to@address'
smtp.finish
You can also use block form of SMTP.start/SMTP#start. They closes
SMTP session automatically:
# using block form of SMTP.start
Net::SMTP.start('your.smtp.server', 25) {|smtp|
smtp.send_message msgstr, 'from@address', 'to@address'
}
I strongly recommend this scheme. This form is more simple and robust.
=== HELO domain
In almost all situation, you must designate the third argument
of SMTP.start/SMTP#start. It is the domain name which you are on
(the host to send mail from). It is called "HELO domain".
SMTP server will judge if he/she should send or reject
the SMTP session by inspecting HELO domain.
Net::SMTP.start('your.smtp.server', 25,
'mail.from.domain') {|smtp|
=== SMTP Authentication
The Net::SMTP class supports three authentication schemes;
PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
To use SMTP authentication, pass extra arguments to
SMTP.start/SMTP#start methods.
# PLAIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from,domain',
'Your Account', 'Your Password', :plain)
# LOGIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from,domain',
'Your Account', 'Your Password', :login)
# CRAM MD5
Net::SMTP.start('your.smtp.server', 25, 'mail.from,domain',
'Your Account', 'Your Password', :cram_md5)
== class Net::SMTP
=== Class Methods
: new( address, port = 25 )
creates a new Net::SMTP object.
This method does not open TCP connection.
: start( address, port = 25, helo_domain = 'localhost.localdomain', account = nil, password = nil, authtype = nil )
: start( address, port = 25, helo_domain = 'localhost.localdomain', account = nil, password = nil, authtype = nil ) {|smtp| .... }
is equal to:
Net::SMTP.new(address,port).start(helo_domain,account,password,authtype)
# example
Net::SMTP.start('your.smtp.server') {
smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
}
This method may raise:
* Net::SMTPAuthenticationError
* Net::SMTPServerBusy
* Net::SMTPSyntaxError
* Net::SMTPFatalError
* Net::SMTPUnknownError
* IOError
* TimeoutError
=== Instance Methods
: start( helo_domain = <local host name>, account = nil, password = nil, authtype = nil )
: start( helo_domain = <local host name>, account = nil, password = nil, authtype = nil ) {|smtp| .... }
opens TCP connection and starts SMTP session.
HELO_DOMAIN is a domain that you'll dispatch mails from.
If protocol had been started, raises IOError.
When this methods is called with block, give a SMTP object to block and
close session after block call finished.
If both of account and password are given, is trying to get
authentication by using AUTH command. AUTHTYPE is an either of
:login, :plain, and :cram_md5.
This method may raise:
* Net::SMTPAuthenticationError
* Net::SMTPServerBusy
* Net::SMTPSyntaxError
* Net::SMTPFatalError
* Net::SMTPUnknownError
* IOError
* TimeoutError
: started?
: active? OBSOLETE
true if SMTP session is started.
: esmtp?
true if the SMTP object uses ESMTP.
: esmtp=(b)
set wheather SMTP should use ESMTP.
: address
the address to connect
: port
the port number to connect
: open_timeout
: open_timeout=(n)
seconds to wait until connection is opened.
If SMTP object cannot open a conection in this seconds,
it raises TimeoutError exception.
: read_timeout
: read_timeout=(n)
seconds to wait until reading one block (by one read(2) call).
If SMTP object cannot open a conection in this seconds,
it raises TimeoutError exception.
: finish
finishes SMTP session.
If SMTP session had not started, raises an IOError.
If SMTP session timed out, raises TimeoutError.
: send_message( msgstr, from_addr, *dest_addrs )
: send_mail( msgstr, from_addr, *dest_addrs )
: sendmail( msgstr, from_addr, *dest_addrs ) OBSOLETE
sends a String MSGSTR. If a single CR ("\r") or LF ("\n") found
in the MEGSTR, converts it to the CR LF pair. You cannot send a
binary message with this class.
FROM_ADDR must be a String, representing source mail address.
TO_ADDRS must be Strings or an Array of Strings, representing
destination mail addresses.
# example
Net::SMTP.start('smtp.example.com') {|smtp|
smtp.send_message msgstr,
'from@example.com',
['dest@example.com', 'dest2@example.com']
}
This method may raise:
* Net::SMTPServerBusy
* Net::SMTPSyntaxError
* Net::SMTPFatalError
* Net::SMTPUnknownError
* IOError
* TimeoutError
: open_message_stream( from_addr, *dest_addrs ) {|stream| .... }
: ready( from_addr, *dest_addrs ) {|stream| .... } OBSOLETE
opens a message writer stream and gives it to the block.
STREAM is valid only in the block, and has these methods:
: puts(str = '')
outputs STR and CR LF.
: print(str)
outputs STR.
: printf(fmt, *args)
outputs sprintf(fmt,*args).
: write(str)
outputs STR and returns the length of written bytes.
: <<(str)
outputs STR and returns self.
If a single CR ("\r") or LF ("\n") found in the message,
converts it to the CR LF pair. You cannot send a binary
message with this class.
FROM_ADDR must be a String, representing source mail address.
TO_ADDRS must be Strings or an Array of Strings, representing
destination mail addresses.
# example
Net::SMTP.start('smtp.example.com', 25) {|smtp|
smtp.open_message_stream('from@example.com', ['dest@example.com']) {|f|
f.puts 'From: from@example.com'
f.puts 'To: dest@example.com'
f.puts 'Subject: test message'
f.puts
f.puts 'This is a test message.'
}
}
This method may raise:
* Net::SMTPServerBusy
* Net::SMTPSyntaxError
* Net::SMTPFatalError
* Net::SMTPUnknownError
* IOError
* TimeoutError
: set_debug_output( output )
WARNING: This method causes serious security holes.
Use this method for only debugging.
set an output stream for debug logging.
You must call this before #start.
# example
smtp = Net::SMTP.new(addr, port)
smtp.set_debug_output $stderr
smtp.start {
....
}
== SMTP Related Exception Classes
: Net::SMTPAuthenticationError
SMTP authentication error.
ancestors: SMTPError, ProtoAuthError (obsolete), ProtocolError (obsolete)
: Net::SMTPServerBusy
Temporal error; error number 420/450.
ancestors: SMTPError, ProtoServerError (obsolete), ProtocolError (obsolete)
: Net::SMTPSyntaxError
SMTP command syntax error (error number 500)
ancestors: SMTPError, ProtoSyntaxError (obsolete), ProtocolError (obsolete)
: Net::SMTPFatalError
Fatal error (error number 5xx, except 500)
ancestors: SMTPError, ProtoFatalError (obsolete), ProtocolError (obsolete)
: Net::SMTPUnknownError
Unexpected reply code returned from server
(might be a bug of this library).
ancestors: SMTPError, ProtoUnkownError (obsolete), ProtocolError (obsolete)
=end
require 'net/protocol'
require 'digest/md5'
module Net
module SMTPError
# This *class* is module for some reason.
# In ruby 1.9.x, this module becomes a class.
end
class SMTPAuthenticationError < ProtoAuthError
include SMTPError
end
class SMTPServerBusy < ProtoServerError
include SMTPError
end
class SMTPSyntaxError < ProtoSyntaxError
include SMTPError
end
class SMTPFatalError < ProtoFatalError
include SMTPError
end
class SMTPUnknownError < ProtoUnknownError
include SMTPError
end
class SMTP
Revision = %q$Revision$.split[1]
def SMTP.default_port
25
end
def initialize( address, port = nil )
@address = address
@port = (port || SMTP.default_port)
@esmtp = true
@socket = nil
@started = false
@open_timeout = 30
@read_timeout = 60
@error_occured = false
@debug_output = nil
end
def inspect
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
end
def esmtp?
@esmtp
end
def esmtp=( bool )
@esmtp = bool
end
alias esmtp esmtp?
attr_reader :address
attr_reader :port
attr_accessor :open_timeout
attr_reader :read_timeout
def read_timeout=( sec )
@socket.read_timeout = sec if @socket
@read_timeout = sec
end
def set_debug_output( arg )
@debug_output = arg
end
#
# SMTP session control
#
def SMTP.start( address, port = nil,
helo = 'localhost.localdomain',
user = nil, secret = nil, authtype = nil,
&block)
new(address, port).start(helo, user, secret, authtype, &block)
end
def started?
@started
end
def start( helo = 'localhost.localdomain',
user = nil, secret = nil, authtype = nil )
if block_given?
begin
do_start(helo, user, secret, authtype)
return yield(self)
ensure
finish if @started
end
else
do_start(helo, user, secret, authtype)
return self
end
end
def do_start( helodomain, user, secret, authtype )
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
@socket = InternetMessageIO.open(@address, @port,
@open_timeout, @read_timeout,
@debug_output)
check_response(critical { recv_response() })
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
authenticate user, secret, authtype if user
end
private :do_start
def finish
raise IOError, 'closing already closed SMTP session' unless @started
quit if @socket and not @socket.closed? and not @error_occured
@socket.close if @socket and not @socket.closed?
@socket = nil
@error_occured = false
@started = false
end
#
# message send
#
public
def send_message( msgstr, from_addr, *to_addrs )
send0(from_addr, to_addrs.flatten) {
@socket.write_message msgstr
}
end
alias send_mail send_message
alias sendmail send_message # obsolete
def open_message_stream( from_addr, *to_addrs, &block )
send0(from_addr, to_addrs.flatten) {
@socket.write_message_by_block(&block)
}
end
alias ready open_message_stream # obsolete
private
def send0( from_addr, to_addrs )
raise IOError, "closed session" unless @socket
raise ArgumentError, 'mail destination does not given' if to_addrs.empty?
mailfrom from_addr
to_addrs.each do |to|
rcptto to
end
res = critical {
check_response(get_response('DATA'), true)
yield
recv_response()
}
check_response(res)
end
#
# auth
#
private
def check_auth_args( user, secret, authtype )
raise ArgumentError, 'both of user and secret are required'\
unless user and secret
auth_method = "auth_#{authtype || 'cram_md5'}"
raise ArgumentError, "wrong auth type #{authtype}"\
unless respond_to?(auth_method)
end
def authenticate( user, secret, authtype )
__send__("auth_#{authtype || 'cram_md5'}", user, secret)
end
def auth_plain( user, secret )
res = critical { get_response('AUTH PLAIN %s',
base64_encode("\0#{user}\0#{secret}")) }
raise SMTPAuthenticationError, res unless /\A2../ === res
end
def auth_login( user, secret )
res = critical {
check_response(get_response('AUTH LOGIN'), true)
check_response(get_response(base64_encode(user)), true)
get_response(base64_encode(secret))
}
raise SMTPAuthenticationError, res unless /\A2../ === res
end
def auth_cram_md5( user, secret )
# CRAM-MD5: [RFC2195]
res = nil
critical {
res = check_response(get_response('AUTH CRAM-MD5'), true)
challenge = res.split(/ /)[1].unpack('m')[0]
secret = Digest::MD5.digest(secret) if secret.size > 64
isecret = secret + "\0" * (64 - secret.size)
osecret = isecret.dup
0.upto(63) do |i|
isecret[i] ^= 0x36
osecret[i] ^= 0x5c
end
tmp = Digest::MD5.digest(isecret + challenge)
tmp = Digest::MD5.hexdigest(osecret + tmp)
res = get_response(base64_encode(user + ' ' + tmp))
}
raise SMTPAuthenticationError, res unless /\A2../ === res
end
def base64_encode( str )
# expects "str" may not become too long
[str].pack('m').gsub(/\s+/, '')
end
#
# SMTP command dispatcher
#
private
def helo( domain )
getok('HELO %s', domain)
end
def ehlo( domain )
getok('EHLO %s', domain)
end
def mailfrom( fromaddr )
getok('MAIL FROM:<%s>', fromaddr)
end
def rcptto( to )
getok('RCPT TO:<%s>', to)
end
def quit
getok('QUIT')
end
#
# row level library
#
private
def getok( fmt, *args )
res = critical {
@socket.writeline sprintf(fmt, *args)
recv_response()
}
return check_response(res)
end
def get_response( fmt, *args )
@socket.writeline sprintf(fmt, *args)
recv_response()
end
def recv_response
res = ''
while true
line = @socket.readline
res << line << "\n"
break unless line[3] == ?- # "210-PIPELINING"
end
res
end
def check_response( res, allow_continue = false )
return res if /\A2/ === res
return res if allow_continue and /\A354/ === res
err = case res
when /\A4/ then SMTPServerBusy
when /\A50/ then SMTPSyntaxError
when /\A55/ then SMTPFatalError
else SMTPUnknownError
end
raise err, res
end
def critical( &block )
return '200 dummy reply code' if @error_occured
begin
return yield()
rescue Exception
@error_occured = true
raise
end
end
end # class SMTP
SMTPSession = SMTP
end # module Net
|