diff options
Diffstat (limited to 'lib/net/smtp.rb')
-rw-r--r-- | lib/net/smtp.rb | 178 |
1 files changed, 125 insertions, 53 deletions
diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index 9f534c20c0..7a04aa2aa2 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -1,89 +1,166 @@ -# -# smtp.rb version 1.0.1 -# -# author Minero Aoki <aamine@dp.u-netsurf.ne.jp> -# +=begin + += net/smtp.rb + +written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> + +This library is distributed under the terms of the Ruby license. +You can freely distribute/modify this library. + +=end + require 'net/session' module Net - class SMTPSession < Session - def proto_initialize - @proto_type = SMTPCommand - @port = 25 - end +=begin + +== Net::SMTP + +=== Super Class + +Net::Protocol + +=== Class Methods + +: new( address = 'localhost', port = 25 ) + This method create new SMTP object. + + +=== Methods + +: start( helo_domain = ENV['HOSTNAME'] ) + This method opens TCP connection and start SMTP. + If protocol had been started, do nothing and return false. + +: sendmail( mailsrc, from_domain, to_addrs ) + 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. + + 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) + +: finish + This method ends SMTP. + If protocol had not started, do nothind and return false. + +=end + + class SMTP < Protocol + + protocol_param :port, '25' + protocol_param :command_type, '::Net::SMTPCommand' + def sendmail( mailsrc, fromaddr, toaddrs ) - @proto.mailfrom( fromaddr ) - @proto.rcpt( toaddrs ) - @proto.data - @proto.sendmail( mailsrc ) + @command.mailfrom fromaddr + @command.rcpt toaddrs + @command.data + @command.sendmail mailsrc end private - def do_start( helodom = nil ) + def do_start( helodom = ENV['HOSTNAME'] ) unless helodom then - helodom = ENV[ 'HOSTNAME' ] + raise ArgumentError, "cannot get hostname" end - @proto.helo( helodom ) - end - - def do_finish - @proto.quit + @command.helo helodom end end - SMTP = SMTPSession + SMTPSession = SMTP + + +=begin + +== Net::SMTPCommand + +=== Super Class + +Net::Command + +=== Class Methods + +: new( socket ) + This method creates new SMTPCommand object, and open SMTP. + + +=== Methods + +: helo( helo_domain ) + This method send "HELO" command and start SMTP. + helo_domain is localhost's FQDN. +: mailfrom( from_addr ) + This method sends "MAIL FROM" command. + from_addr is your mail address(????@????). +: rcpt( to_addrs ) + This method sends "RCPT TO" command. + to_addrs is array of mail address(???@???) of destination. + +: data( mailsrc ) + This method send 'mailsrc' as mail. SMTP reads strings from 'mailsrc' + by calling 'each' iterator. + +: quit + This method sends "QUIT" command and ends SMTP session. + +=end class SMTPCommand < Command + def initialize( sock ) + super + check_reply SuccessCode + end + + def helo( fromdom ) - @socket.writeline( 'HELO ' << fromdom ) - check_reply( SuccessCode ) + getok sprintf( 'HELO %s', fromdom ) end def mailfrom( fromaddr ) - @socket.writeline( 'MAIL FROM:<' + fromaddr + '>' ) - check_reply( SuccessCode ) + getok sprintf( 'MAIL FROM:<%s>', fromaddr ) end def rcpt( toaddrs ) toaddrs.each do |i| - @socket.writeline( 'RCPT TO:<' + i + '>' ) - check_reply( SuccessCode ) + getok sprintf( 'RCPT TO:<%s>', i ) end end def data - @socket.writeline( 'DATA' ) - check_reply( ContinueCode ) + getok 'DATA', ContinueCode end - def sendmail( mailsrc ) - @socket.write_pendstr( mailsrc ) - check_reply( SuccessCode ) + def writemail( mailsrc ) + @socket.write_pendstr mailsrc + check_reply SuccessCode end + alias sendmail writemail private def do_quit - @socket.writeline( 'QUIT' ) - check_reply( SuccessCode ) + getok 'QUIT' end @@ -91,19 +168,19 @@ module Net arr = read_reply stat = arr[0][0,3] - cls = UnknownCode - case stat[0] - when ?2 then cls = SuccessCode - when ?3 then cls = ContinueCode - when ?4 then cls = ServerBusyCode - when ?5 then - case stat[1] - when ?0 then cls = SyntaxErrorCode - when ?5 then cls = FatalErrorCode - end - end - - return cls.new( stat, arr.join('') ) + klass = UnknownCode + klass = case stat[0] + when ?2 then SuccessCode + when ?3 then ContinueCode + when ?4 then ServerBusyCode + when ?5 then + case stat[1] + when ?0 then SyntaxErrorCode + when ?5 then FatalErrorCode + end + end + + klass.new( stat, arr.join('') ) end @@ -120,9 +197,4 @@ module Net end - - unless Session::Version == '1.0.1' then - $stderr.puts "WARNING: wrong version of session.rb & smtp.rb" - end - end |