/*  Class  MailCGISession A subclass of the dB2K WebClass.cc, this
				class implements automatic eMail sending using the SMTP protocol
            for use only on Windows NT 4 with Internet Information Server.

            
 Requires:	Windows NT4 plus the Option Pack. Internet Information Server must
 				be installed and used as the Web Server and Mail Server.

            Note: if you're not using IIS, or have not set up mail, see
            the Samples folder for an example of a Message Server that
            will send eMail from a database row.

 Operation:	This class allows you to output a text file that IIS polls for,
 				looking for mail to send.  See the NT Option Pack documentation
            for more details, including which folder to use to drop the
            mail-formatted text file into.

 Usage		// Instantiate class
            Set Proc to WebIISMailClass.cc additive
            oCGI = new MailCGISession('c:\mymailfolder')

            // Send sender address
            oCGI.From := "webmaster@mycompany.com"

            #define CRLF chr(13)+chr(10)  // Define Carriage Return/linefeed
          
            // Pass the full Windows path to the pickup folder as a parameter
            oMail = new mailCGISession()

            oCGI.From := "webmaster@mycompany.com"
            oCGI.To.add(cEmailAddress) // Add data to oCGI "to" array
            oCGI.Cc.add(cCCAddress1)
            oCGI.Cc.add(cCCAddress2)
            oCGI.Subject := "Thank You!"  // Set Subject 

            // The Body property contains the entire text of the message, and must be
            // pre-formatted - you'll need to control the line length and add end-of-line
            // markers as appropriate
      
            oCGI.Body = "Dear Mr. " + cLastName + CRLF + CRLF
            oCGI.Body += "Thank you very much for your comments." + CRLF
            oCGI.Body += "We will take the appropriate action." + CRLF + CRLF
            oCGI.Body += "Jane Doe" + CRLF
            oCGI.Body += "webmaster@mycompany.com"

            oCGI.PostForPickup()  // Send mail message to text file

   Author:	Bowen Moursund 01/18/2000 dBASE Inc.
   			Adapted into WebClass: A. A. Katz 01/19/2000
*/

class MailCGISession(cMailFolder) of CGISession(cMailFolder) from "WebClass.cc"


   // The complete Windows path to the mail server's pickup folder
   // It may be sent as a param to the new() operator
   if type(cMailFolder) = 'C'
      this.oMail = cMailFolder
   else
      this.oMail = ""
   endif

   // The sender's email address
	this.From = ""

   // The email addresses of the primary recipients
   this.To = new array()

   // The email ddresses of those to receive carbon copies
   this.Cc = new array()

   // The email addresses of those to receive blind carbon copies
   this.Bcc = new array()

   // The subject of the message.
   this.Subject = ""

   // The complete text body of the message, including end of line markers.
   this.Body = ""

   function PostForPickup
      local bPosted, cMailFileName, aReceivers, oFile, cTo, cCc, i
      bPosted = false
      store "" to cTo, cCc
      cMailFileName = funique(this.oMail+"\????????.txt")

      // If the pickup folder does not exist
      if isBlank( cMailFileName )
      

         // Uncomment the following line if you want to quit the
         // application in this case and return an error page to
         // the user. You may not want to do this if your transaction
         // does more than post a single mail message. In that case,
         // handle the error in the calling code.

         //this.sorryPage('Mail Folder Error!')

         return false // message not created or sent

      endif

      try

         aReceivers = new array()

         aReceivers.add(this.To[1])
         cTo = this.To[1]

         if this.To.size > 1
            for i = 2 to this.To.size
               aReceivers.add(this.To[i])
               cTo += ","+chr(13)+chr(10)+"    "+this.To[i]
            next
         endif

         if this.Cc.size > 0
            aReceivers.add(this.Cc[1])
            cCc = this.Cc[1]
            if this.Cc.size > 1
               for i = 2 to this.Cc.size
                  aReceivers.add(this.Cc[i])
                  cCc += ","+chr(13)+chr(10)+"    "+this.Cc[i]
               next
            endif
         endif

         for i = 1 to this.Bcc.size
            aReceivers.add(this.Bcc[i])
         next

         oFile = new file()
         oFile.create(cMailFileName)

         oFile.putS("x-sender: "+this.From)

         for i = 1 to aReceivers.size
            oFile.putS("x-receiver: "+aReceivers[i])
         next

         oFile.putS("From: "+this.From)

         oFile.putS("To: "+cTo)

         if not empty( cCc )
            oFile.putS("Cc: "+cCc)
         endif

         oFile.putS("Subject: "+this.Subject)

         oFile.putS("")  // blank line indicates end of headers

         oFile.write(this.Body)

         oFile.close()

         bPosted := true

      catch (exception e)

         try
            oFile.close()
         catch (exception e)
         endtry

         // Uncomment the following line to quit and return an
         // error message to the user's browser. You may not
         // want to do this in this method - the rest of your
         // transaction may have completed successfully with
         // the exception of a single mail message.
         // this.errorPage(e)

      endtry


      return bPosted

endclass
