home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 September / CHIPCD_9_99.iso / software / serwery_www / websuite / websuite.exe / SDMAIL.FG_ / SDMAIL.FG
Text File  |  1999-01-12  |  6KB  |  261 lines

  1.  
  2. CLASS sendmail
  3.   PUBLIC:
  4.     local session, to, tohost, from, fromhost, msg, subject, port, debug, defserver
  5.     
  6.     METHOD new( session )
  7.         ::session = session
  8.         ::to = ""
  9.         ::tohost = ""
  10.         ::from = ""
  11.         ::fromhost = ""
  12.         ::msg = ""
  13.         ::subject = ""
  14.         ::port = 25
  15.         ::defserver = "mail"
  16.         ::debug = 0
  17.         ::getvars( )
  18.         return( 1 )
  19.     END
  20.  
  21.  
  22.     METHOD AutoRespond( to, from, subject, file )
  23.         return( ::AutoRespondEx( to, "", from, "", subject, file ) )
  24.     END
  25.  
  26.  
  27.     METHOD AutoRespondEx( to, tohost, from, fromhost, subject, file )
  28.         local msg
  29.  
  30.         msg = fileReadAscii( file )
  31.         if ( strempty( msg ) )
  32.             return( "" )
  33.         end
  34.         
  35.         ::to = to
  36.         ::tohost = tohost
  37.         ::from = from
  38.         ::fromhost = fromhost
  39.         ::subject = subject
  40.         ::msg = msg
  41.         
  42.         return( "" + println( ::send( ) ) )
  43.     END
  44.  
  45.  
  46.     METHOD ProcessVars( var, def )
  47.         local val
  48.         val = ::session.var( var )
  49.         if ( strempty( val ) )
  50.             val = def
  51.         end
  52.         return( val )
  53.     END
  54.  
  55.     METHOD GetVars( )
  56.         ::to = ::ProcessVars( "to", ::to )
  57.         ::tohost = ::ProcessVars( "tohost", ::tohost )
  58.         ::from = ::ProcessVars( "from", ::from )
  59.         ::fromhost = ::ProcessVars( "fromhost", ::fromhost )
  60.         ::msg = ::ProcessVars( "msg", ::msg )
  61.         ::subject = ::ProcessVars( "subject", ::subject )
  62.         return( 1 )
  63.     END
  64.     
  65.     METHOD validate( )
  66.         local str
  67.         str = ""
  68.         if ( strempty( ::to ) )
  69.             str += "Must specifify a TO value.<br>"
  70.         end
  71.         if ( strempty( ::from ) )
  72.             str += "Must specifify a FROM value.<br>"
  73.         end
  74.         if ( strempty( ::msg ) )
  75.             str += "Must specifify a MSG value.<br>"
  76.         end
  77.         if ( strempty( ::subject ) )
  78.             str += "Must specifify a SUBJECT value.<br>"
  79.         end
  80.         return( str )
  81.     END
  82.  
  83.     
  84.     METHOD SendDirect( to, from, subject, msg )
  85.         return( ::SendDirectEx( to, "", from, "", subject, msg ) )
  86.     END
  87.     
  88.  
  89.     METHOD SendDirectEx( to, tohost, from, fromhost, subject, msg )
  90.         ::to = to
  91.         ::tohost = tohost
  92.         ::from = from
  93.         ::fromhost = fromhost
  94.         ::subject = subject
  95.         :: msg = msg
  96.         return( ::send( ) )
  97.     END
  98.  
  99.  
  100.     METHOD send( )
  101.         local err, cnt, i, str
  102.  
  103.         err = ::validate( )
  104.         if ( ! strempty( err ) )
  105.             return( err )
  106.         end
  107.  
  108.         str = "<b>SmartDesk's SENDMAIL Program, v1.0<br>"
  109.         str += "(c) 1998-1999 by SmartDesk, Inc., All Rights Reserved.</b><br><br>"
  110.  
  111.         cnt = chrcount( ",", ::to ) + 1
  112.         for ( i=1; i<=cnt; i++ )
  113.             str += "<b>Processing message " + i + " of " + cnt + "...</b><br>"
  114.             str += ::_send( i ) + "<br><br>"
  115.         end
  116.  
  117.         return( str )        
  118.     END
  119.  
  120.  
  121.     METHOD ExtractAddr( addr )
  122.         if ( chrcount( "<", addr ) > 0 )
  123.             addr = strextract( strextract( addr, "<", 2 ), ">", 1 )
  124.         end
  125.         return( addr )
  126.     END
  127.     
  128.  
  129.     METHOD _send( index )
  130.         local to_full, to_short, to_host, from_full, from_short, from_host
  131.         local str, hsock, cmd, data
  132.  
  133.         to_full = alltrim( strextract( ::to, ",", index ) )
  134.         to_short = ::ExtractAddr( to_full )
  135.         to_host = alltrim( strextract( ::tohost, ",", index ) )
  136.         if ( strempty( to_host ) )
  137.             if ( index < 2 )
  138.                 to_host = strextract( to_short, "@", 2 )
  139.             else
  140.                 to_host = alltrim( strextract( ::tohost, ",", 1 ) )
  141.                 if ( strempty( to_host ) )
  142.                     to_host = strextract( to_short, "@", 2 )
  143.                 end
  144.             end
  145.         end
  146.         
  147.         from_full = alltrim( strextract( ::from, ",", index ) )
  148.         from_short = ::ExtractAddr( from_full )
  149.         from_host = alltrim( strextract( ::fromhost, ",", index ) )
  150.         if ( strempty( from_host ) )
  151.             if ( index < 2 )
  152.                 from_host = strextract( from_short, "@", 2 )
  153.             else
  154.                 from_host = alltrim( strextract( ::fromhost, ",", 1 ) )
  155.                 if ( strempty( from_host ) )
  156.                     from_host = strextract( from_short, "@", 2 )
  157.                 end
  158.             end
  159.         end
  160.  
  161.         hsock = webConnect( to_host, ::port )
  162.         str = "<b>Connect to " + to_host + " at port " + ::port + "</b><br>"
  163.         if ( hsock < 1 )
  164.             hsock = webConnect( ::defserver, ::port )
  165.             str += "<b>Connect to " + ::defserver + " at port " + ::port + "</b><br>"
  166.             if ( hsock < 1 )
  167.                 str += "Unable to connect to mail server - " + to_host + " or " + ::defserver
  168.                 return( str )
  169.             end
  170.         end
  171.     
  172.         data = webReceiveData( hsock, 1024 )
  173.         str += data + "<br>"
  174.         if ( ::debug )
  175.             print( data )
  176.         end
  177.         if ( webReplyCode( data ) != 220 )
  178.             webDisconnect( hsock )
  179.             return( str )
  180.         end
  181.     
  182.         cmd = "HELO " + from_host
  183.         webSend( hsock, cmd + "\r\n" )
  184.         data = webReceiveData( hsock, 1024 )
  185.         str += "<b>" + cmd + "</b><br>" + data + "<br>"
  186.         if ( ::debug )
  187.             print( data )
  188.         end
  189.         if ( webReplyCode( data ) != 250 )
  190.             webDisconnect( hsock )
  191.             return( str )
  192.         end
  193.     
  194.         cmd = "MAIL FROM: <" + from_short + ">"
  195.         webSend( hsock, cmd + "\r\n" )
  196.         data = webReceiveData( hsock, 1024 )
  197.         str += "<b>MAIL FROM: <" + from_short + "></b><br>" + data + "<br>"
  198.         if ( ::debug )
  199.             print( data )
  200.         end
  201.         if ( webReplyCode( data ) != 250 )
  202.             webDisconnect( hsock )
  203.             return( str )
  204.         end
  205.     
  206.         cmd = "RCPT TO: <" + to_short + ">"
  207.         webSend( hsock, cmd + "\r\n" )
  208.         data = webReceiveData( hsock, 1024 )
  209.         str += "<b>RCPT TO: <" + to_short + "></b><br>" + data + "<br>"
  210.         if ( ::debug )
  211.             print( data )
  212.         end
  213.         if ( webReplyCode( data ) != 250 )
  214.             webDisconnect( hsock )
  215.             return( str )
  216.         end
  217.  
  218.         cmd = "DATA"
  219.         webSend( hsock, cmd + "\r\n" )
  220.         data = webReceiveData( hsock, 1024 )
  221.         str += "<b>" + cmd + "</b><br>"
  222.         str += data + "<br>"
  223.         if ( ::debug )
  224.             print( data )
  225.         end
  226.         if ( webReplyCode( data ) != 354 )
  227.             webDisconnect( hsock )
  228.             return( str )
  229.         end
  230.  
  231.         cmd = "To: " + to_full + "\r\n"
  232.         cmd += "From: " + from_full + "\r\n"
  233. //        cmd = "To: " + to_short + "\r\n"
  234. //        cmd += "From: " + from_short + "\r\n"
  235.         cmd += "Subject: " + ::subject + "\r\n\r\n" + ::msg
  236.         webSend( hsock, cmd + "\r\n\r\n.\r\n" )
  237.         data = webReceiveData( hsock, 1024 )
  238.         str + "<b>Subject: " + ::subject + "<br>" + ::msg + "</b><br>" + data + "<br>"
  239.         if ( ::debug )
  240.             print( data )
  241.         end
  242.         if ( webReplyCode( data ) != 250 )
  243.             webDisconnect( hsock )
  244.             return( str )
  245.         end
  246.     
  247.         cmd = "QUIT"
  248.         webSend( hsock, cmd + "\r\n" )
  249.         data = webReceiveData( hsock, 1024 )
  250.         str += "<b>" + cmd + "</b><br>" + data + "<br>"
  251.         if ( ::debug )
  252.             print( data )
  253.         end
  254.         webDisconnect( hsock )
  255.  
  256.         return( str )
  257.     END
  258.     
  259. END
  260.  
  261.