1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
2001-12-13 14:15:21 -05:00
|
|
|
= net/smtp.rb
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2003-03-11 06:54:30 -05:00
|
|
|
Copyright (c) 1999-2003 Yukihiro Matsumoto
|
|
|
|
Copyright (c) 1999-2003 Minero Aoki
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
written & maintained by Minero Aoki <aamine@loveruby.net>
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 19: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-23 23:53:50 -05:00
|
|
|
|
2001-12-07 05:04:25 -05:00
|
|
|
NOTE: You can find Japanese version of this document in
|
|
|
|
the doc/net directory of the standard ruby interpreter package.
|
2000-09-21 02:58:01 -04:00
|
|
|
|
2001-12-13 14:15:21 -05:00
|
|
|
$Id$
|
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
== What is This Module?
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 19: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 03:29:15 -04:00
|
|
|
|
2002-11-21 06:50:09 -05:00
|
|
|
== What is NOT This Module?
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2002-11-21 06:50:09 -05: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-26 19:49:21 -04:00
|
|
|
|
|
|
|
== Examples
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
=== Sending Message
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
You must open connection to SMTP server before sending messages.
|
2001-06-26 19:49:21 -04:00
|
|
|
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 06:50:09 -05:00
|
|
|
to do it. SMTP connection is closed automatically after block is
|
2001-06-26 19:49:21 -04:00
|
|
|
executed.
|
|
|
|
|
2001-06-26 20:59:08 -04:00
|
|
|
require 'net/smtp'
|
2002-11-21 06:50:09 -05:00
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
2003-07-01 22:39:53 -04:00
|
|
|
# use a SMTP object only in this block
|
2001-06-26 20:59:08 -04:00
|
|
|
}
|
2001-06-26 19: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.
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
Then you can send messages.
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
msgstr = <<END_OF_MESSAGE
|
2001-06-26 20:59:08 -04:00
|
|
|
From: Your Name <your@mail.address>
|
2003-07-01 22:39:53 -04:00
|
|
|
To: Destination Address <someone@example.com>
|
|
|
|
Subject: test message
|
2001-06-26 20:59:08 -04:00
|
|
|
Date: Sat, 23 Jun 2001 16:26:43 +0900
|
2003-07-01 22:39:53 -04:00
|
|
|
Message-Id: <unique.message.id.string@example.com>
|
2001-06-26 20:59:08 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
This is a test message.
|
|
|
|
END_OF_MESSAGE
|
2002-11-21 06:50:09 -05:00
|
|
|
|
|
|
|
require 'net/smtp'
|
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
2003-07-01 22:39:53 -04:00
|
|
|
smtp.send_message msgstr,
|
|
|
|
'your@mail.address',
|
|
|
|
'his_addess@example.com'
|
2001-06-26 20:59:08 -04:00
|
|
|
}
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2001-12-09 03:58:30 -05:00
|
|
|
=== Closing Session
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
You MUST close SMTP session after sending messages, by calling #finish
|
|
|
|
method:
|
2001-12-09 03:58:30 -05:00
|
|
|
|
|
|
|
# using SMTP#finish
|
2002-11-21 06:50:09 -05:00
|
|
|
smtp = Net::SMTP.start('your.smtp.server', 25)
|
2003-07-01 22:39:53 -04:00
|
|
|
smtp.send_message msgstr, 'from@address', 'to@address'
|
2001-12-09 03:58:30 -05:00
|
|
|
smtp.finish
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
You can also use block form of SMTP.start/SMTP#start. They closes
|
|
|
|
SMTP session automatically:
|
|
|
|
|
2001-12-09 03:58:30 -05:00
|
|
|
# using block form of SMTP.start
|
2002-11-21 06:50:09 -05:00
|
|
|
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
2003-07-01 22:39:53 -04:00
|
|
|
smtp.send_message msgstr, 'from@address', 'to@address'
|
2001-12-09 03:58:30 -05:00
|
|
|
}
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
I strongly recommend this scheme. This form is more simple and robust.
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
=== HELO domain
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2001-12-30 14:18:45 -05: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-26 19:49:21 -04:00
|
|
|
|
2003-05-24 14:24:26 -04:00
|
|
|
Net::SMTP.start('your.smtp.server', 25,
|
|
|
|
'mail.from.domain') {|smtp|
|
|
|
|
|
|
|
|
=== SMTP Authentication
|
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
The Net::SMTP class supports three authentication schemes;
|
2003-05-24 14:24:26 -04:00
|
|
|
PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554])
|
2003-07-01 22:39:53 -04:00
|
|
|
To use SMTP authentication, pass extra arguments to
|
|
|
|
SMTP.start/SMTP#start methods.
|
2003-05-24 14:24:26 -04:00
|
|
|
|
|
|
|
# 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-26 19:49:21 -04:00
|
|
|
|
|
|
|
== class Net::SMTP
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
2001-08-16 23:08:45 -04:00
|
|
|
: new( address, port = 25 )
|
2001-06-26 20:59:08 -04:00
|
|
|
creates a new Net::SMTP object.
|
2003-07-01 22:39:53 -04:00
|
|
|
This method does not open TCP connection.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-12-30 14:18:45 -05: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| .... }
|
2003-07-01 22:39:53 -04:00
|
|
|
is equal to:
|
2001-06-26 20:59:08 -04:00
|
|
|
Net::SMTP.new(address,port).start(helo_domain,account,password,authtype)
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 20:59:08 -04:00
|
|
|
# example
|
2003-07-01 22:39:53 -04:00
|
|
|
Net::SMTP.start('your.smtp.server') {
|
|
|
|
smtp.send_message msgstr, 'from@example.com', ['dest@example.com']
|
2001-06-26 20:59:08 -04:00
|
|
|
}
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
This method may raise:
|
|
|
|
|
|
|
|
* Net::SMTPAuthenticationError
|
|
|
|
* Net::SMTPServerBusy
|
|
|
|
* Net::SMTPSyntaxError
|
|
|
|
* Net::SMTPFatalError
|
|
|
|
* Net::SMTPUnknownError
|
|
|
|
* IOError
|
|
|
|
* TimeoutError
|
|
|
|
|
2001-06-26 19: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-26 20: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 15:03:16 -04:00
|
|
|
If protocol had been started, raises IOError.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 20: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 04:57:37 -04:00
|
|
|
|
2001-06-26 20:59:08 -04:00
|
|
|
If both of account and password are given, is trying to get
|
2003-05-24 14:24:26 -04:00
|
|
|
authentication by using AUTH command. AUTHTYPE is an either of
|
|
|
|
:login, :plain, and :cram_md5.
|
2000-04-25 05:23:21 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
This method may raise:
|
|
|
|
|
|
|
|
* Net::SMTPAuthenticationError
|
|
|
|
* Net::SMTPServerBusy
|
|
|
|
* Net::SMTPSyntaxError
|
|
|
|
* Net::SMTPFatalError
|
|
|
|
* Net::SMTPUnknownError
|
|
|
|
* IOError
|
|
|
|
* TimeoutError
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
: started?
|
2003-07-01 22:39:53 -04:00
|
|
|
: active? OBSOLETE
|
2001-06-26 19:49:21 -04:00
|
|
|
true if SMTP session is started.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
: esmtp?
|
|
|
|
true if the SMTP object uses ESMTP.
|
|
|
|
|
|
|
|
: esmtp=(b)
|
|
|
|
set wheather SMTP should use ESMTP.
|
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
: address
|
|
|
|
the address to connect
|
2000-07-01 14:28:24 -04:00
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
: port
|
|
|
|
the port number to connect
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2001-06-26 19: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 15:26:07 -04:00
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
: read_timeout
|
|
|
|
: read_timeout=(n)
|
2003-07-01 22:39:53 -04:00
|
|
|
seconds to wait until reading one block (by one read(2) call).
|
2001-06-26 19:49:21 -04:00
|
|
|
If SMTP object cannot open a conection in this seconds,
|
|
|
|
it raises TimeoutError exception.
|
2000-08-16 15:26:07 -04:00
|
|
|
|
2001-06-26 19:49:21 -04:00
|
|
|
: finish
|
|
|
|
finishes SMTP session.
|
2001-07-03 15:03:16 -04:00
|
|
|
If SMTP session had not started, raises an IOError.
|
2003-07-01 22:39:53 -04:00
|
|
|
If SMTP session timed out, raises TimeoutError.
|
2000-02-21 10:25:37 -05:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
: 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.
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2001-06-26 20: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-26 19:49:21 -04:00
|
|
|
|
2001-06-26 20:59:08 -04:00
|
|
|
# example
|
2003-07-01 22:39:53 -04:00
|
|
|
Net::SMTP.start('smtp.example.com') {|smtp|
|
|
|
|
smtp.send_message msgstr,
|
|
|
|
'from@example.com',
|
|
|
|
['dest@example.com', 'dest2@example.com']
|
2001-06-26 20:59:08 -04:00
|
|
|
}
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
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.
|
2001-06-26 20: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
|
2003-07-01 22:39:53 -04:00
|
|
|
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.'
|
|
|
|
}
|
2001-06-26 20:59:08 -04:00
|
|
|
}
|
2000-02-21 10:25:37 -05:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
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)
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
: Net::SMTPUnknownError
|
|
|
|
Unexpected reply code returned from server
|
|
|
|
(might be a bug of this library).
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2003-07-01 22:39:53 -04:00
|
|
|
ancestors: SMTPError, ProtoUnkownError (obsolete), ProtocolError (obsolete)
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
2001-12-01 09:07:01 -05:00
|
|
|
require 'digest/md5'
|
2000-06-12 12:42:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
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
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
Revision = %q$Revision$.split[1]
|
|
|
|
|
|
|
|
def SMTP.default_port
|
|
|
|
25
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize( address, port = nil )
|
|
|
|
@address = address
|
2003-07-01 22:34:39 -04:00
|
|
|
@port = (port || SMTP.default_port)
|
2000-04-18 05:39:02 -04:00
|
|
|
@esmtp = true
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
2003-07-01 22:34:39 -04:00
|
|
|
@error_occured = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2003-07-01 22:34:39 -04:00
|
|
|
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
|
2000-04-18 05:39:02 -04:00
|
|
|
end
|
|
|
|
|
2002-02-19 07:33:52 -05:00
|
|
|
def esmtp?
|
|
|
|
@esmtp
|
|
|
|
end
|
|
|
|
|
|
|
|
def esmtp=( bool )
|
|
|
|
@esmtp = bool
|
|
|
|
end
|
|
|
|
|
|
|
|
alias esmtp esmtp?
|
2000-04-18 05:39:02 -04:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :address
|
|
|
|
attr_reader :port
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_accessor :open_timeout
|
|
|
|
attr_reader :read_timeout
|
2000-02-21 10:25:37 -05:00
|
|
|
|
2003-05-02 10: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 )
|
|
|
|
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
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
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
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = InternetMessageIO.open(@address, @port,
|
|
|
|
@open_timeout, @read_timeout,
|
|
|
|
@debug_output)
|
2003-07-01 22:34:39 -04:00
|
|
|
check_response(critical { recv_response() })
|
2000-02-21 10:25:37 -05:00
|
|
|
begin
|
2002-11-21 06:50:09 -05:00
|
|
|
if @esmtp
|
2003-07-01 22:34:39 -04:00
|
|
|
ehlo helodomain
|
2000-04-18 05:39:02 -04:00
|
|
|
else
|
2003-07-01 22:34:39 -04:00
|
|
|
helo helodomain
|
2000-04-18 05:39:02 -04:00
|
|
|
end
|
2000-02-21 10:25:37 -05:00
|
|
|
rescue ProtocolError
|
2002-11-21 06:50:09 -05:00
|
|
|
if @esmtp
|
2000-04-18 05:39:02 -04:00
|
|
|
@esmtp = false
|
2003-07-01 22:34:39 -04:00
|
|
|
@error_occured = false
|
2000-04-18 05:39:02 -04:00
|
|
|
retry
|
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
raise
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
2003-07-01 22:34:39 -04:00
|
|
|
authenticate user, secret, authtype if user
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
private :do_start
|
|
|
|
|
|
|
|
def finish
|
|
|
|
raise IOError, 'closing already closed SMTP session' unless @started
|
2003-07-01 22:34:39 -04:00
|
|
|
quit if @socket and not @socket.closed? and not @error_occured
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
2003-07-01 22:34:39 -04:00
|
|
|
@error_occured = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@started = false
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
2003-07-01 22:34:39 -04:00
|
|
|
# message send
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
public
|
|
|
|
|
|
|
|
def send_message( msgstr, from_addr, *to_addrs )
|
|
|
|
send0(from_addr, to_addrs.flatten) {
|
|
|
|
@socket.write_message msgstr
|
|
|
|
}
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
alias send_mail send_message
|
|
|
|
alias sendmail send_message # obsolete
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def open_message_stream( from_addr, *to_addrs, &block )
|
|
|
|
send0(from_addr, to_addrs.flatten) {
|
|
|
|
@socket.write_message_by_block(&block)
|
|
|
|
}
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
alias ready open_message_stream # obsolete
|
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
private
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def send0( from_addr, to_addrs )
|
|
|
|
raise IOError, "closed session" unless @socket
|
2002-02-19 07:33:52 -05:00
|
|
|
raise ArgumentError, 'mail destination does not given' if to_addrs.empty?
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
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)
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
|
|
|
# auth
|
|
|
|
#
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
private
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
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)
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def authenticate( user, secret, authtype )
|
|
|
|
__send__("auth_#{authtype || 'cram_md5'}", user, secret)
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
|
|
|
|
2000-04-25 05:23:21 -04:00
|
|
|
def auth_plain( user, secret )
|
2003-05-02 10:35:01 -04:00
|
|
|
res = critical { get_response('AUTH PLAIN %s',
|
2003-05-24 14:24:26 -04:00
|
|
|
base64_encode("\0#{user}\0#{secret}")) }
|
|
|
|
raise SMTPAuthenticationError, res unless /\A2../ === res
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_login( user, secret )
|
|
|
|
res = critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
check_response(get_response('AUTH LOGIN'), true)
|
|
|
|
check_response(get_response(base64_encode(user)), true)
|
|
|
|
get_response(base64_encode(secret))
|
2003-05-24 14:24:26 -04:00
|
|
|
}
|
2003-05-02 10:35:01 -04:00
|
|
|
raise SMTPAuthenticationError, res unless /\A2../ === res
|
2000-04-25 05:23:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def auth_cram_md5( user, secret )
|
2003-05-24 14:24:26 -04:00
|
|
|
# CRAM-MD5: [RFC2195]
|
2003-05-02 10:35:01 -04:00
|
|
|
res = nil
|
|
|
|
critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
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))
|
2000-04-25 05:23:21 -04:00
|
|
|
}
|
2003-05-02 10:35:01 -04:00
|
|
|
raise SMTPAuthenticationError, res unless /\A2../ === res
|
2000-04-25 05:23:21 -04:00
|
|
|
end
|
|
|
|
|
2003-05-24 14:24:26 -04:00
|
|
|
def base64_encode( str )
|
|
|
|
# expects "str" may not become too long
|
|
|
|
[str].pack('m').gsub(/\s+/, '')
|
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
|
|
|
# SMTP command dispatcher
|
|
|
|
#
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def helo( domain )
|
|
|
|
getok('HELO %s', domain)
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def ehlo( domain )
|
|
|
|
getok('EHLO %s', domain)
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def mailfrom( fromaddr )
|
|
|
|
getok('MAIL FROM:<%s>', fromaddr)
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
def rcptto( to )
|
|
|
|
getok('RCPT TO:<%s>', to)
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
def quit
|
2003-05-02 10:35:01 -04:00
|
|
|
getok('QUIT')
|
2000-03-27 10:52:27 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
#
|
|
|
|
# row level library
|
|
|
|
#
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
private
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def getok( fmt, *args )
|
2003-07-01 22:34:39 -04:00
|
|
|
res = critical {
|
|
|
|
@socket.writeline sprintf(fmt, *args)
|
|
|
|
recv_response()
|
|
|
|
}
|
|
|
|
return check_response(res)
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def get_response( fmt, *args )
|
|
|
|
@socket.writeline sprintf(fmt, *args)
|
|
|
|
recv_response()
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def recv_response
|
|
|
|
res = ''
|
2002-11-21 06:50:09 -05:00
|
|
|
while true
|
2003-05-02 10:35:01 -04:00
|
|
|
line = @socket.readline
|
|
|
|
res << line << "\n"
|
|
|
|
break unless line[3] == ?- # "210-PIPELINING"
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
res
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-05-24 14:24:26 -04:00
|
|
|
def check_response( res, allow_continue = false )
|
2003-07-01 22:34:39 -04:00
|
|
|
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
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
end # class SMTP
|
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 02:04:40 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
SMTPSession = SMTP
|
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 02:04:40 -04:00
|
|
|
|
|
|
|
end # module Net
|