home *** CD-ROM | disk | FTP | other *** search
-
- CLASS sendmail
- PUBLIC:
- local session, to, tohost, from, fromhost, msg, subject, port, debug, defserver
-
- METHOD new( session )
- ::session = session
- ::to = ""
- ::tohost = ""
- ::from = ""
- ::fromhost = ""
- ::msg = ""
- ::subject = ""
- ::port = 25
- ::defserver = "mail"
- ::debug = 0
- ::getvars( )
- return( 1 )
- END
-
-
- METHOD AutoRespond( to, from, subject, file )
- return( ::AutoRespondEx( to, "", from, "", subject, file ) )
- END
-
-
- METHOD AutoRespondEx( to, tohost, from, fromhost, subject, file )
- local msg
-
- msg = fileReadAscii( file )
- if ( strempty( msg ) )
- return( "" )
- end
-
- ::to = to
- ::tohost = tohost
- ::from = from
- ::fromhost = fromhost
- ::subject = subject
- ::msg = msg
-
- return( "" + println( ::send( ) ) )
- END
-
-
- METHOD ProcessVars( var, def )
- local val
- val = ::session.var( var )
- if ( strempty( val ) )
- val = def
- end
- return( val )
- END
-
- METHOD GetVars( )
- ::to = ::ProcessVars( "to", ::to )
- ::tohost = ::ProcessVars( "tohost", ::tohost )
- ::from = ::ProcessVars( "from", ::from )
- ::fromhost = ::ProcessVars( "fromhost", ::fromhost )
- ::msg = ::ProcessVars( "msg", ::msg )
- ::subject = ::ProcessVars( "subject", ::subject )
- return( 1 )
- END
-
- METHOD validate( )
- local str
- str = ""
- if ( strempty( ::to ) )
- str += "Must specifify a TO value.<br>"
- end
- if ( strempty( ::from ) )
- str += "Must specifify a FROM value.<br>"
- end
- if ( strempty( ::msg ) )
- str += "Must specifify a MSG value.<br>"
- end
- if ( strempty( ::subject ) )
- str += "Must specifify a SUBJECT value.<br>"
- end
- return( str )
- END
-
-
- METHOD SendDirect( to, from, subject, msg )
- return( ::SendDirectEx( to, "", from, "", subject, msg ) )
- END
-
-
- METHOD SendDirectEx( to, tohost, from, fromhost, subject, msg )
- ::to = to
- ::tohost = tohost
- ::from = from
- ::fromhost = fromhost
- ::subject = subject
- :: msg = msg
- return( ::send( ) )
- END
-
-
- METHOD send( )
- local err, cnt, i, str
-
- err = ::validate( )
- if ( ! strempty( err ) )
- return( err )
- end
-
- str = "<b>SmartDesk's SENDMAIL Program, v1.0<br>"
- str += "(c) 1998-1999 by SmartDesk, Inc., All Rights Reserved.</b><br><br>"
-
- cnt = chrcount( ",", ::to ) + 1
- for ( i=1; i<=cnt; i++ )
- str += "<b>Processing message " + i + " of " + cnt + "...</b><br>"
- str += ::_send( i ) + "<br><br>"
- end
-
- return( str )
- END
-
-
- METHOD ExtractAddr( addr )
- if ( chrcount( "<", addr ) > 0 )
- addr = strextract( strextract( addr, "<", 2 ), ">", 1 )
- end
- return( addr )
- END
-
-
- METHOD _send( index )
- local to_full, to_short, to_host, from_full, from_short, from_host
- local str, hsock, cmd, data
-
- to_full = alltrim( strextract( ::to, ",", index ) )
- to_short = ::ExtractAddr( to_full )
- to_host = alltrim( strextract( ::tohost, ",", index ) )
- if ( strempty( to_host ) )
- if ( index < 2 )
- to_host = strextract( to_short, "@", 2 )
- else
- to_host = alltrim( strextract( ::tohost, ",", 1 ) )
- if ( strempty( to_host ) )
- to_host = strextract( to_short, "@", 2 )
- end
- end
- end
-
- from_full = alltrim( strextract( ::from, ",", index ) )
- from_short = ::ExtractAddr( from_full )
- from_host = alltrim( strextract( ::fromhost, ",", index ) )
- if ( strempty( from_host ) )
- if ( index < 2 )
- from_host = strextract( from_short, "@", 2 )
- else
- from_host = alltrim( strextract( ::fromhost, ",", 1 ) )
- if ( strempty( from_host ) )
- from_host = strextract( from_short, "@", 2 )
- end
- end
- end
-
- hsock = webConnect( to_host, ::port )
- str = "<b>Connect to " + to_host + " at port " + ::port + "</b><br>"
- if ( hsock < 1 )
- hsock = webConnect( ::defserver, ::port )
- str += "<b>Connect to " + ::defserver + " at port " + ::port + "</b><br>"
- if ( hsock < 1 )
- str += "Unable to connect to mail server - " + to_host + " or " + ::defserver
- return( str )
- end
- end
-
- data = webReceiveData( hsock, 1024 )
- str += data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 220 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "HELO " + from_host
- webSend( hsock, cmd + "\r\n" )
- data = webReceiveData( hsock, 1024 )
- str += "<b>" + cmd + "</b><br>" + data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 250 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "MAIL FROM: <" + from_short + ">"
- webSend( hsock, cmd + "\r\n" )
- data = webReceiveData( hsock, 1024 )
- str += "<b>MAIL FROM: <" + from_short + "></b><br>" + data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 250 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "RCPT TO: <" + to_short + ">"
- webSend( hsock, cmd + "\r\n" )
- data = webReceiveData( hsock, 1024 )
- str += "<b>RCPT TO: <" + to_short + "></b><br>" + data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 250 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "DATA"
- webSend( hsock, cmd + "\r\n" )
- data = webReceiveData( hsock, 1024 )
- str += "<b>" + cmd + "</b><br>"
- str += data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 354 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "To: " + to_full + "\r\n"
- cmd += "From: " + from_full + "\r\n"
- // cmd = "To: " + to_short + "\r\n"
- // cmd += "From: " + from_short + "\r\n"
- cmd += "Subject: " + ::subject + "\r\n\r\n" + ::msg
- webSend( hsock, cmd + "\r\n\r\n.\r\n" )
- data = webReceiveData( hsock, 1024 )
- str + "<b>Subject: " + ::subject + "<br>" + ::msg + "</b><br>" + data + "<br>"
- if ( ::debug )
- print( data )
- end
- if ( webReplyCode( data ) != 250 )
- webDisconnect( hsock )
- return( str )
- end
-
- cmd = "QUIT"
- webSend( hsock, cmd + "\r\n" )
- data = webReceiveData( hsock, 1024 )
- str += "<b>" + cmd + "</b><br>" + data + "<br>"
- if ( ::debug )
- print( data )
- end
- webDisconnect( hsock )
-
- return( str )
- END
-
- END
-
-