1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
= net/smtp.rb
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
This library is distributed under the terms of the Ruby license.
|
|
|
|
You can freely distribute/modify this library.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
== Net::SMTP
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Net::Protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = 25 )
|
2000-05-18 04:57:37 -04:00
|
|
|
creates a new Net::SMTP object.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-05-18 04:57:37 -04:00
|
|
|
: start( address = 'localhost', port = 25, *protoargs )
|
|
|
|
: start( address = 'localhost', port = 25, *protoargs ) {|smtp| .... }
|
|
|
|
same to Net::SMTP.new( address, port ).start( *protoargs )
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
2000-05-18 04:57:37 -04:00
|
|
|
: start( helo_domain = Socket.gethostname, \
|
|
|
|
account = nil, password = nil, authtype = nil )
|
|
|
|
: start( helo_domain = Socket.gethostname, \
|
|
|
|
account = nil, password = nil, authtype = nil ) {|smtp| .... }
|
|
|
|
opens TCP connection and starts SMTP session.
|
1999-12-29 06:14:04 -05:00
|
|
|
If protocol had been started, do nothing and return false.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-05-18 04:57:37 -04:00
|
|
|
When this methods is called as iterator, give a SMTP object to block and
|
|
|
|
close session after block call finished.
|
|
|
|
|
2000-04-25 05:23:21 -04:00
|
|
|
If account and password are given, is trying to get authentication
|
|
|
|
by using AUTH command. "authtype" is :plain (symbol) or :cram_md5.
|
|
|
|
|
2000-07-01 14:28:24 -04:00
|
|
|
: send_mail( mailsrc, from_addr, to_addrs )
|
2000-03-05 05:25:53 -05:00
|
|
|
: sendmail( mailsrc, from_addr, to_addrs )
|
2000-01-05 02:55:36 -05:00
|
|
|
This method sends 'mailsrc' as mail. SMTPSession read strings
|
|
|
|
from 'mailsrc' by calling 'each' iterator, and convert them
|
|
|
|
into "\r\n" terminated string when write.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-07-01 14:28:24 -04:00
|
|
|
from_addr must be String.
|
|
|
|
to_addrs must be Array of String, or String.
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Exceptions which SMTP raises are:
|
|
|
|
* Net::ProtoSyntaxError: syntax error (errno.500)
|
|
|
|
* Net::ProtoFatalError: fatal error (errno.550)
|
|
|
|
* Net::ProtoUnknownError: unknown error
|
|
|
|
* Net::ProtoServerBusy: temporary error (errno.420/450)
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-03-05 05:25:53 -05:00
|
|
|
: ready( from_addr, to_addrs ) {|adapter| .... }
|
2000-02-21 10:25:37 -05:00
|
|
|
This method stands by the SMTP object for sending mail.
|
|
|
|
In the block of this iterator, you can call ONLY 'write' method
|
|
|
|
for 'adapter'.
|
|
|
|
|
|
|
|
# usage example
|
|
|
|
|
|
|
|
SMTP.start( 'localhost', 25 ) do |smtp|
|
|
|
|
smtp.ready( from, to ) do |adapter|
|
|
|
|
adapter.write str1
|
|
|
|
adapter.write str2
|
|
|
|
adapter.write str3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
: finish
|
1999-12-29 06:14:04 -05:00
|
|
|
This method ends SMTP.
|
|
|
|
If protocol had not started, do nothind and return false.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
|
|
|
require 'md5'
|
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
class SMTP < Protocol
|
1999-12-17 10:00:13 -05:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
protocol_param :port, '25'
|
|
|
|
protocol_param :command_type, '::Net::SMTPCommand'
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
|
2000-04-18 05:39:02 -04:00
|
|
|
def initialize( addr = nil, port = nil )
|
|
|
|
super
|
|
|
|
@esmtp = true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
attr :esmtp
|
|
|
|
|
2000-07-01 14:28:24 -04:00
|
|
|
def send_mail( mailsrc, from_addr, to_addrs )
|
|
|
|
do_ready from_addr, to_addrs
|
2000-03-31 08:02:40 -05:00
|
|
|
@command.write_mail mailsrc, nil
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
2000-07-01 14:28:24 -04:00
|
|
|
alias sendmail send_mail
|
2000-02-21 10:25:37 -05:00
|
|
|
|
2000-07-01 14:28:24 -04:00
|
|
|
def ready( from_addr, to_addrs, &block )
|
|
|
|
do_ready from_addr, to_addrs
|
2000-03-31 08:02:40 -05:00
|
|
|
@command.write_mail nil, block
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
2000-07-01 14:28:24 -04:00
|
|
|
def do_ready( from_addr, to_addrs )
|
|
|
|
to_addrs = [to_addrs] if String === to_addrs
|
|
|
|
@command.mailfrom from_addr
|
|
|
|
@command.rcpt to_addrs
|
2000-03-26 03:48:15 -05:00
|
|
|
@command.data
|
|
|
|
end
|
|
|
|
|
2000-04-25 05:23:21 -04:00
|
|
|
def do_start( helodom = nil,
|
|
|
|
user = nil, secret = nil, authtype = nil )
|
1999-09-22 03:32:33 -04:00
|
|
|
unless helodom then
|
2000-05-17 14:39:43 -04:00
|
|
|
helodom = ::Socket.gethostname
|
2000-04-25 05:23:21 -04:00
|
|
|
unless helodom then
|
2000-05-17 14:39:43 -04:00
|
|
|
raise ArgumentError,
|
|
|
|
"cannot get localhost name; try 'smtp.start(local_host_name)'"
|
2000-04-25 05:23:21 -04:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2000-02-21 10:25:37 -05:00
|
|
|
|
|
|
|
begin
|
2000-04-18 05:39:02 -04:00
|
|
|
if @esmtp then
|
|
|
|
@command.ehlo helodom
|
|
|
|
else
|
|
|
|
@command.helo helodom
|
|
|
|
end
|
2000-02-21 10:25:37 -05:00
|
|
|
rescue ProtocolError
|
2000-04-18 05:39:02 -04:00
|
|
|
if @esmtp then
|
|
|
|
@esmtp = false
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
2000-04-25 05:23:21 -04:00
|
|
|
|
|
|
|
if user and secret then
|
2000-06-01 09:43:43 -04:00
|
|
|
mid = 'auth_' + (authtype || 'cram_md5').to_s
|
|
|
|
unless @command.respond_to? mid then
|
2000-04-25 05:23:21 -04:00
|
|
|
raise ArgumentError, "wrong auth type #{authtype.to_s}"
|
|
|
|
end
|
2000-06-01 09:43:43 -04:00
|
|
|
@command.send mid, user, secret
|
2000-04-25 05:23:21 -04:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
SMTPSession = SMTP
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SMTPCommand < Command
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def initialize( sock )
|
|
|
|
super
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
check_reply SuccessCode
|
|
|
|
}
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def helo( fromdom )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'HELO %s', fromdom )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-02-21 10:25:37 -05:00
|
|
|
def ehlo( fromdom )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'EHLO %s', fromdom )
|
|
|
|
}
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-04-25 05:23:21 -04:00
|
|
|
# "PLAIN" authentication [RFC2554]
|
|
|
|
def auth_plain( user, secret )
|
|
|
|
critical {
|
|
|
|
getok sprintf( 'AUTH PLAIN %s',
|
|
|
|
["\0#{user}\0#{secret}"].pack('m').chomp )
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# "CRAM-MD5" authentication [RFC2195]
|
|
|
|
def auth_cram_md5( user, secret )
|
|
|
|
critical {
|
|
|
|
rep = getok( 'AUTH CRAM-MD5', ContinueCode )
|
|
|
|
challenge = rep.msg.split(' ')[1].unpack('m')[0]
|
|
|
|
secret = MD5.new( secret ).digest 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 = MD5.new( isecret + challenge ).digest
|
|
|
|
tmp = MD5.new( osecret + tmp ).hexdigest
|
|
|
|
|
|
|
|
getok [user + ' ' + tmp].pack('m').chomp
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def mailfrom( fromaddr )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'MAIL FROM:<%s>', fromaddr )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def rcpt( toaddrs )
|
|
|
|
toaddrs.each do |i|
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'RCPT TO:<%s>', i )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def data
|
2000-03-27 10:52:27 -05:00
|
|
|
return unless begin_critical
|
1999-12-29 06:14:04 -05:00
|
|
|
getok 'DATA', ContinueCode
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-31 08:02:40 -05:00
|
|
|
def write_mail( mailsrc, block )
|
|
|
|
@socket.write_pendstr mailsrc, block
|
1999-12-29 06:14:04 -05:00
|
|
|
check_reply SuccessCode
|
2000-03-27 10:52:27 -05:00
|
|
|
end_critical
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2000-02-21 10:25:37 -05:00
|
|
|
alias sendmail write_mail
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
def quit
|
|
|
|
critical {
|
|
|
|
getok 'QUIT'
|
|
|
|
}
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
private
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_reply
|
|
|
|
arr = read_reply
|
|
|
|
stat = arr[0][0,3]
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
klass = case stat[0]
|
|
|
|
when ?2 then SuccessCode
|
|
|
|
when ?3 then ContinueCode
|
2000-03-31 08:02:40 -05:00
|
|
|
when ?4 then ServerErrorCode
|
1999-12-29 06:14:04 -05:00
|
|
|
when ?5 then
|
|
|
|
case stat[1]
|
|
|
|
when ?0 then SyntaxErrorCode
|
2000-04-25 05:23:21 -04:00
|
|
|
when ?3 then AuthErrorCode
|
1999-12-29 06:14:04 -05:00
|
|
|
when ?5 then FatalErrorCode
|
|
|
|
end
|
|
|
|
end
|
2000-04-25 05:23:21 -04:00
|
|
|
klass ||= UnknownCode
|
1999-12-29 06:14:04 -05:00
|
|
|
|
2000-03-31 08:02:40 -05:00
|
|
|
Response.new( klass, stat, arr.join('') )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def read_reply
|
|
|
|
arr = []
|
|
|
|
|
2000-04-25 05:23:21 -04:00
|
|
|
while true do
|
|
|
|
str = @socket.readline
|
|
|
|
break unless str[3] == ?- # ex: "210-..."
|
1999-09-22 03:32:33 -04:00
|
|
|
arr.push str
|
|
|
|
end
|
|
|
|
arr.push str
|
|
|
|
|
|
|
|
return arr
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|