1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
= net/smtp.rb
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2003-03-11 14:54:30 +03:00
|
|
|
Copyright (c) 1999-2003 Yukihiro Matsumoto
|
|
|
|
Copyright (c) 1999-2003 Minero Aoki
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
written & maintained by Minero Aoki <aamine@loveruby.net>
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
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.
|
2001-02-24 07:53:50 +03:00
|
|
|
|
2001-12-07 13:04:25 +03:00
|
|
|
NOTE: You can find Japanese version of this document in
|
|
|
|
the doc/net directory of the standard ruby interpreter package.
|
2000-09-21 10:58:01 +04:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
$Id$
|
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
== What is This Module?
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
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>)).
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
== What is NOT This Module?
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
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>)).
|
2001-06-27 03:49:21 +04:00
|
|
|
|
|
|
|
== Examples
|
|
|
|
|
|
|
|
=== Sending Mail
|
|
|
|
|
|
|
|
You must open connection to SMTP server before sending mails.
|
|
|
|
First argument is the address of SMTP server, and second argument
|
|
|
|
is port number. Using SMTP.start with block is the most simple way
|
2002-11-21 14:50:09 +03:00
|
|
|
to do it. SMTP connection is closed automatically after block is
|
2001-06-27 03:49:21 +04:00
|
|
|
executed.
|
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
require 'net/smtp'
|
2002-11-21 14:50:09 +03:00
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
2003-05-02 18:35:01 +04:00
|
|
|
# use SMTP object only in this block
|
2001-06-27 04:59:08 +04:00
|
|
|
}
|
2001-06-27 03:49:21 +04:00
|
|
|
|
|
|
|
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 mail.
|
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
mail_text = <<END_OF_MAIL
|
2001-06-27 04:59:08 +04:00
|
|
|
From: Your Name <your@mail.address>
|
|
|
|
To: Dest Address <to@some.domain>
|
|
|
|
Subject: test mail
|
|
|
|
Date: Sat, 23 Jun 2001 16:26:43 +0900
|
|
|
|
Message-Id: <unique.message.id.string@some.domain>
|
|
|
|
|
|
|
|
This is test mail.
|
2002-11-21 14:50:09 +03:00
|
|
|
END_OF_MAIL
|
|
|
|
|
|
|
|
require 'net/smtp'
|
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
|
|
|
smtp.send_mail mail_text,
|
|
|
|
'your@mail.address',
|
|
|
|
'his_addess@example.com'
|
2001-06-27 04:59:08 +04:00
|
|
|
}
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2001-12-09 11:58:30 +03:00
|
|
|
=== Closing Session
|
|
|
|
|
|
|
|
You MUST close SMTP session after sending mails, by calling #finish
|
|
|
|
method. You can also use block form of SMTP.start/SMTP#start, which
|
|
|
|
closes session automatically. I strongly recommend later one. It is
|
|
|
|
more beautiful and simple.
|
|
|
|
|
|
|
|
# using SMTP#finish
|
2002-11-21 14:50:09 +03:00
|
|
|
smtp = Net::SMTP.start('your.smtp.server', 25)
|
2001-12-09 11:58:30 +03:00
|
|
|
smtp.send_mail mail_string, 'from@address', 'to@address'
|
|
|
|
smtp.finish
|
|
|
|
|
|
|
|
# using block form of SMTP.start
|
2002-11-21 14:50:09 +03:00
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
2001-12-09 11:58:30 +03:00
|
|
|
smtp.send_mail mail_string, 'from@address', 'to@address'
|
|
|
|
}
|
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
=== Sending Mails From non-String Sources
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
In an example above I has sent mail from String (here document literal).
|
2001-06-27 03:49:21 +04:00
|
|
|
SMTP#send_mail accepts any objects which has "each" method
|
|
|
|
like File and Array.
|
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
require 'net/smtp'
|
2002-11-21 14:50:09 +03:00
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
|
|
|
File.open('Mail/draft/1') {|f|
|
2001-12-09 11:58:30 +03:00
|
|
|
smtp.send_mail f, 'your@mail.address', 'to@some.domain'
|
|
|
|
}
|
2001-06-27 03:49:21 +04:00
|
|
|
}
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
=== HELO domain
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
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.
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2003-05-24 22:24:26 +04:00
|
|
|
Net::SMTP.start('your.smtp.server', 25,
|
|
|
|
'mail.from.domain') {|smtp|
|
|
|
|
|
|
|
|
=== SMTP Authentication
|
|
|
|
|
|
|
|
net/smtp supports three authentication scheme.
|
|
|
|
PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
|
|
|
|
|
|
|
|
# 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)
|
2001-06-27 03:49:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
== class Net::SMTP
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
2001-08-17 07:08:45 +04:00
|
|
|
: new( address, port = 25 )
|
2001-06-27 04:59:08 +04:00
|
|
|
creates a new Net::SMTP object.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
: 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| .... }
|
2001-06-27 04:59:08 +04:00
|
|
|
is equal to
|
|
|
|
Net::SMTP.new(address,port).start(helo_domain,account,password,authtype)
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
# example
|
|
|
|
Net::SMTP.start( 'your.smtp.server' ) {
|
2001-12-09 11:58:30 +03:00
|
|
|
smtp.send_mail mail_string, 'from@mail.address', 'dest@mail.address'
|
2001-06-27 04:59:08 +04:00
|
|
|
}
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
=== 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| .... }
|
2001-06-27 04:59:08 +04:00
|
|
|
opens TCP connection and starts SMTP session.
|
|
|
|
HELO_DOMAIN is a domain that you'll dispatch mails from.
|
2001-07-03 23:03:16 +04:00
|
|
|
If protocol had been started, raises IOError.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
When this methods is called with block, give a SMTP object to block and
|
|
|
|
close session after block call finished.
|
2000-05-18 12:57:37 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
If both of account and password are given, is trying to get
|
2003-05-24 22:24:26 +04:00
|
|
|
authentication by using AUTH command. AUTHTYPE is an either of
|
|
|
|
:login, :plain, and :cram_md5.
|
2000-04-25 13:23:21 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
: started?
|
2001-06-27 03:49:21 +04:00
|
|
|
true if SMTP session is started.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
: esmtp?
|
|
|
|
true if the SMTP object uses ESMTP.
|
|
|
|
|
|
|
|
: esmtp=(b)
|
|
|
|
set wheather SMTP should use ESMTP.
|
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: address
|
|
|
|
the address to connect
|
2000-07-01 22:28:24 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: port
|
|
|
|
the port number to connect
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: 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.
|
2000-08-16 23:26:07 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: read_timeout
|
|
|
|
: read_timeout=(n)
|
|
|
|
seconds to wait until reading one block (by one read(1) call).
|
|
|
|
If SMTP object cannot open a conection in this seconds,
|
|
|
|
it raises TimeoutError exception.
|
2000-08-16 23:26:07 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: finish
|
|
|
|
finishes SMTP session.
|
2001-07-03 23:03:16 +04:00
|
|
|
If SMTP session had not started, raises an IOError.
|
2000-02-21 18:25:37 +03:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: send_mail( mailsrc, from_addr, *to_addrs )
|
2001-06-27 04:59:08 +04:00
|
|
|
This method sends MAILSRC as mail. A SMTP object read strings
|
|
|
|
from MAILSRC by calling "each" iterator, with converting them
|
|
|
|
into CRLF ("\r\n") terminated string when write.
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
FROM_ADDR must be a String, representing source mail address.
|
|
|
|
TO_ADDRS must be Strings or an Array of Strings, representing
|
|
|
|
destination mail addresses.
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
# example
|
|
|
|
Net::SMTP.start( 'your.smtp.server' ) {|smtp|
|
2001-12-09 11:58:30 +03:00
|
|
|
smtp.send_mail mail_string,
|
|
|
|
'from@mail.address',
|
|
|
|
'dest@mail.address' 'dest2@mail.address'
|
2001-06-27 04:59:08 +04:00
|
|
|
}
|
2001-06-27 03:49:21 +04:00
|
|
|
|
2001-06-27 04:59:08 +04:00
|
|
|
: ready( from_addr, *to_addrs ) {|adapter| .... }
|
|
|
|
This method stands by the SMTP object for sending mail and
|
2001-12-30 22:18:45 +03:00
|
|
|
gives adapter object to the block. ADAPTER has these 5 methods:
|
|
|
|
|
|
|
|
puts print printf write <<
|
2001-06-27 04:59:08 +04:00
|
|
|
|
|
|
|
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( 'your.smtp.server', 25 ) {|smtp|
|
2001-12-30 22:18:45 +03:00
|
|
|
smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) {|f|
|
|
|
|
f.puts 'From: aamine@loveruby.net'
|
|
|
|
f.puts 'To: someone@somedomain.org'
|
|
|
|
f.puts 'Subject: test mail'
|
|
|
|
f.puts
|
|
|
|
f.puts 'This is test mail.'
|
|
|
|
}
|
2001-06-27 04:59:08 +04:00
|
|
|
}
|
2000-02-21 18:25:37 +03:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
== Exceptions
|
|
|
|
|
|
|
|
SMTP objects raise these exceptions:
|
2003-05-02 18:35:01 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
: Net::ProtoSyntaxError
|
2003-05-02 18:35:01 +04:00
|
|
|
Syntax error (errno.500)
|
2001-06-27 03:49:21 +04:00
|
|
|
: Net::ProtoFatalError
|
2003-05-02 18:35:01 +04:00
|
|
|
Fatal error (errno.550)
|
2001-06-27 03:49:21 +04:00
|
|
|
: Net::ProtoUnknownError
|
2003-05-02 18:35:01 +04:00
|
|
|
Unknown error. (is probably bug)
|
2001-06-27 03:49:21 +04:00
|
|
|
: Net::ProtoServerBusy
|
2003-05-02 18:35:01 +04:00
|
|
|
Temporal error (errno.420/450)
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
require 'net/protocol'
|
2001-12-01 17:07:01 +03:00
|
|
|
require 'digest/md5'
|
2000-06-12 20:42:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
2003-05-02 19:19:20 +04:00
|
|
|
class SMTP < Protocol
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
Revision = %q$Revision$.split[1]
|
|
|
|
|
|
|
|
def SMTP.default_port
|
|
|
|
25
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize( address, port = nil )
|
|
|
|
@address = address
|
|
|
|
@port = port || SMTP.default_port
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-04-18 13:39:02 +04:00
|
|
|
@esmtp = true
|
2003-05-02 18:35:01 +04:00
|
|
|
|
|
|
|
@command = nil
|
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
|
|
|
|
|
|
|
@debug_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{self.class} #{address}:#{@port} open=#{@started}>"
|
2000-04-18 13:39:02 +04:00
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def esmtp?
|
|
|
|
@esmtp
|
|
|
|
end
|
|
|
|
|
|
|
|
def esmtp=( bool )
|
|
|
|
@esmtp = bool
|
|
|
|
end
|
|
|
|
|
|
|
|
alias esmtp esmtp?
|
2000-04-18 13:39:02 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_reader :address
|
|
|
|
attr_reader :port
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
attr_accessor :open_timeout
|
|
|
|
attr_reader :read_timeout
|
2000-02-21 18:25:37 +03:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
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 )
|
2003-05-04 10:18:19 +04:00
|
|
|
raise IOError, 'SMTP session already started' if @started
|
2003-05-02 18:35:01 +04:00
|
|
|
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( helo, user, secret, authtype )
|
|
|
|
@socket = InternetMessageIO.open(@address, @port,
|
|
|
|
@open_timeout, @read_timeout,
|
|
|
|
@debug_output)
|
|
|
|
@command = SMTPCommand.new(@socket)
|
2000-02-21 18:25:37 +03:00
|
|
|
begin
|
2002-11-21 14:50:09 +03:00
|
|
|
if @esmtp
|
2003-05-02 18:35:01 +04:00
|
|
|
@command.ehlo helo
|
2000-04-18 13:39:02 +04:00
|
|
|
else
|
2003-05-02 18:35:01 +04:00
|
|
|
@command.helo helo
|
2000-04-18 13:39:02 +04:00
|
|
|
end
|
2000-02-21 18:25:37 +03:00
|
|
|
rescue ProtocolError
|
2002-11-21 14:50:09 +03:00
|
|
|
if @esmtp
|
2000-04-18 13:39:02 +04:00
|
|
|
@esmtp = false
|
2003-05-04 10:18:19 +04:00
|
|
|
@command = SMTPCommand.new(@socket)
|
2000-04-18 13:39:02 +04:00
|
|
|
retry
|
|
|
|
end
|
2003-05-02 18:35:01 +04:00
|
|
|
raise
|
2000-02-21 18:25:37 +03:00
|
|
|
end
|
2000-04-25 13:23:21 +04:00
|
|
|
|
2002-11-21 14:50:09 +03:00
|
|
|
if user or secret
|
|
|
|
raise ArgumentError, 'both of account and password are required'\
|
|
|
|
unless user and secret
|
2000-06-01 17:43:43 +04:00
|
|
|
mid = 'auth_' + (authtype || 'cram_md5').to_s
|
2002-11-21 14:50:09 +03:00
|
|
|
raise ArgumentError, "wrong auth type #{authtype}"\
|
|
|
|
unless command().respond_to?(mid)
|
2003-05-02 18:35:01 +04:00
|
|
|
@command.__send__ mid, user, secret
|
2000-04-25 13:23:21 +04:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2003-05-02 18:35:01 +04:00
|
|
|
private :do_start
|
|
|
|
|
|
|
|
def finish
|
|
|
|
raise IOError, 'closing already closed SMTP session' unless @started
|
|
|
|
@command.quit if @command
|
|
|
|
@command = nil
|
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
|
|
|
@started = false
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
2003-05-02 18:35:01 +04:00
|
|
|
# SMTP wrapper
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
def send_mail( mailsrc, from_addr, *to_addrs )
|
|
|
|
do_ready from_addr, to_addrs.flatten
|
2002-11-21 14:50:09 +03:00
|
|
|
command().write_mail mailsrc
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
alias sendmail send_mail # backward compatibility
|
2001-12-30 22:18:45 +03:00
|
|
|
|
|
|
|
def ready( from_addr, *to_addrs, &block )
|
|
|
|
do_ready from_addr, to_addrs.flatten
|
2002-07-29 10:14:10 +04:00
|
|
|
command().through_mail(&block)
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def do_ready( from_addr, to_addrs )
|
2002-02-19 15:33:52 +03:00
|
|
|
raise ArgumentError, 'mail destination does not given' if to_addrs.empty?
|
2001-12-30 22:18:45 +03:00
|
|
|
command().mailfrom from_addr
|
2002-11-21 14:50:09 +03:00
|
|
|
command().rcpt to_addrs
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def command
|
|
|
|
raise IOError, "closed session" unless @command
|
|
|
|
@command
|
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
SMTPSession = SMTP
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
class SMTPCommand
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def initialize( sock )
|
2003-05-02 18:35:01 +04:00
|
|
|
@socket = sock
|
|
|
|
@in_critical_block = false
|
|
|
|
check_response(critical { recv_response() })
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{self.class} socket=#{@socket.inspect}>"
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def helo( domain )
|
2003-05-02 18:35:01 +04:00
|
|
|
getok('HELO %s', domain)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def ehlo( domain )
|
2003-05-02 18:35:01 +04:00
|
|
|
getok('EHLO %s', domain)
|
2000-02-21 18:25:37 +03:00
|
|
|
end
|
|
|
|
|
2000-04-25 13:23:21 +04:00
|
|
|
def auth_plain( user, secret )
|
2003-05-02 18:35:01 +04:00
|
|
|
res = critical { get_response('AUTH PLAIN %s',
|
2003-05-24 22:24:26 +04:00
|
|
|
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))
|
|
|
|
}
|
2003-05-02 18:35:01 +04:00
|
|
|
raise SMTPAuthenticationError, res unless /\A2../ === res
|
2000-04-25 13:23:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def auth_cram_md5( user, secret )
|
2003-05-24 22:24:26 +04:00
|
|
|
# CRAM-MD5: [RFC2195]
|
2003-05-02 18:35:01 +04:00
|
|
|
res = nil
|
|
|
|
critical {
|
|
|
|
res = check_response(get_response('AUTH CRAM-MD5'), true)
|
|
|
|
challenge = res.split(/ /)[1].unpack('m')[0]
|
2001-12-30 22:18:45 +03:00
|
|
|
secret = Digest::MD5.digest(secret) if secret.size > 64
|
|
|
|
|
|
|
|
isecret = secret + "\0" * (64 - secret.size)
|
|
|
|
osecret = isecret.dup
|
2002-11-21 14:50:09 +03:00
|
|
|
0.upto(63) do |i|
|
2001-12-30 22:18:45 +03:00
|
|
|
isecret[i] ^= 0x36
|
|
|
|
osecret[i] ^= 0x5c
|
|
|
|
end
|
2002-11-21 14:50:09 +03:00
|
|
|
tmp = Digest::MD5.digest(isecret + challenge)
|
|
|
|
tmp = Digest::MD5.hexdigest(osecret + tmp)
|
2001-12-30 22:18:45 +03:00
|
|
|
|
2003-05-24 22:24:26 +04:00
|
|
|
res = get_response(base64_encode(user + ' ' + tmp))
|
2000-04-25 13:23:21 +04:00
|
|
|
}
|
2003-05-02 18:35:01 +04:00
|
|
|
raise SMTPAuthenticationError, res unless /\A2../ === res
|
2000-04-25 13:23:21 +04:00
|
|
|
end
|
|
|
|
|
2003-05-24 22:24:26 +04:00
|
|
|
def base64_encode( str )
|
|
|
|
# expects "str" may not become too long
|
|
|
|
[str].pack('m').gsub(/\s+/, '')
|
|
|
|
end
|
|
|
|
private :base64_encode
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
def mailfrom( fromaddr )
|
2003-05-02 18:35:01 +04:00
|
|
|
getok('MAIL FROM:<%s>', fromaddr)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rcpt( toaddrs )
|
|
|
|
toaddrs.each do |i|
|
2003-05-02 18:35:01 +04:00
|
|
|
getok('RCPT TO:<%s>', i)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def write_mail( src )
|
2003-05-02 18:35:01 +04:00
|
|
|
res = critical {
|
|
|
|
check_response(get_response('DATA'), true)
|
2002-02-19 15:33:52 +03:00
|
|
|
@socket.write_message src
|
2003-05-02 18:35:01 +04:00
|
|
|
recv_response()
|
2002-02-19 15:33:52 +03:00
|
|
|
}
|
2003-05-02 18:35:01 +04:00
|
|
|
check_response(res)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def through_mail( &block )
|
2003-05-02 18:35:01 +04:00
|
|
|
res = critical {
|
|
|
|
check_response(get_response('DATA'), true)
|
2002-02-19 15:33:52 +03:00
|
|
|
@socket.through_message(&block)
|
2003-05-02 18:35:01 +04:00
|
|
|
recv_response()
|
2002-02-19 15:33:52 +03:00
|
|
|
}
|
2003-05-02 18:35:01 +04:00
|
|
|
check_response(res)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
def quit
|
2003-05-02 18:35:01 +04:00
|
|
|
getok('QUIT')
|
2000-03-27 19:52:27 +04:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
private
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def getok( fmt, *args )
|
|
|
|
@socket.writeline sprintf(fmt, *args)
|
|
|
|
check_response(critical { recv_response() })
|
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def get_response( fmt, *args )
|
|
|
|
@socket.writeline sprintf(fmt, *args)
|
|
|
|
recv_response()
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def recv_response
|
|
|
|
res = ''
|
2002-11-21 14:50:09 +03:00
|
|
|
while true
|
2003-05-02 18:35:01 +04:00
|
|
|
line = @socket.readline
|
|
|
|
res << line << "\n"
|
|
|
|
break unless line[3] == ?- # "210-PIPELINING"
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2003-05-02 18:35:01 +04:00
|
|
|
res
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2003-05-24 22:24:26 +04:00
|
|
|
def check_response( res, allow_continue = false )
|
2003-05-02 18:35:01 +04:00
|
|
|
etype = case res[0]
|
|
|
|
when ?2 then nil
|
2003-05-24 22:24:26 +04:00
|
|
|
when ?3 then allow_continue ? nil : ProtoUnknownError
|
2003-05-02 18:35:01 +04:00
|
|
|
when ?4 then ProtoServerError
|
|
|
|
when ?5 then
|
|
|
|
case res[1]
|
|
|
|
when ?0 then ProtoSyntaxError
|
|
|
|
when ?3 then ProtoAuthError
|
|
|
|
when ?5 then ProtoFatalError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise etype, res if etype
|
|
|
|
res
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2003-05-02 18:35:01 +04:00
|
|
|
def critical
|
|
|
|
return if @in_critical_block
|
|
|
|
@in_critical_block = true
|
|
|
|
result = yield()
|
|
|
|
@in_critical_block = false
|
|
|
|
result
|
|
|
|
end
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
end
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
|
|
|
end # module Net
|