Tips_tutorials   >   Studiojs202   >   Web App Stats Email

Web App Stats Email

I prefer to get an email automatically sent to me each week with the web app stats. In this section of the tutorial we'll add an email object and a timer object to accomplish this for us.

Email Object - Part 1

We could use the SMTPSend Omnis Command directly in our code, but that requires pulling together all of the parameters whenever we want to send an email. An easier way it to wrap the SMTPSend in an object class which we can initialize with the parameters that don't change and a few default parameters, then create a public $sendMail message with a reduced set of parameters. We can instantiate the oEmail object as a Startup_Task task variable, initialize it during startup, and then access it whenever we need to send an email from any method in our application.

  1. F2 Browser > ContactsWeb library > New Class > Object
  2. Name the class, oEmail
  3. Double-click oEmail to get to the methods.
  4. Rename $construct to $initialize
  5. Add the following character type parameters to the $initialize method.
    1. pSMTPServer
    2. pSMTPUser
    3. pSTMPPassword
    4. pPOP3Server
    5. pPOP3User
    6. pPOP3Password
    7. pDefaultFromEmailAddr
    8. pDefaultFromName

  6. Add the exact same set of instance variables, replacing the first letter p with the letter i

  7. Add the following code to the $initialize method.

Calculate iSMTPServer as pSMTPServer
Calculate iSMTPUser as pSMTPUser
Calculate iSMTPPassword as pSMTPPassword
Calculate iPOP3Server as pPOP3Server
Calculate iPOP3Password as pPop3Password
Calculate iPOP3User as pPOP3User
Calculate iDefaultFromEmail as pDefaultFromEmailAddr
Calculate iDefaultFromName as pDefaultFromName

Quit method kTrue

Email Object - Part 2

  1. Add a $sendMail method to oEmail.
  2. Add the following parameters to the $sendMail method.
    1. pSubject Character
    2. pBody Character
    3. pTo - Binary
    4. pCc_opt - Binary
    5. pBcc_opt - Binary
    6. pFromEmailAddr_opt - Character
    7. pFromName_opt - Character

  3. Add the following code to the $sendMail method.

; Ping the SMTP server before we attempt to send an email.
Do method pingSMTPServer Returns FlagOK
If FlagOK
   
   ; Set the optional 'from' parameters
   If len(pFromEmailAddr_opt)
      Calculate FromName as pFromEmailAddr_opt
   Else
      Calculate FromName as iDefaultFromEmail
   End If
   
   If len(pFromName_opt)
      Calculate FromName as pFromName_opt
   Else
      Calculate FromName as iDefaultFromName
   End If
   
   ; Convert any To, Cc, Bcc string parameters to lists.
   If pTo.$colcount
      Calculate ToList as pTo
   Else
      Do method convertCSVToList (pTo) Returns ToList
   End If
   
   If pCc_opt.$colcount
      Calculate CcList as pCc_opt
   Else
      Do method convertCSVToList (pCc_opt) Returns CcList
   End If
   
   If pBcc_opt.$colcount
      Calculate BccList as pBcc_opt
   Else
      Do method convertCSVToList (pBcc_opt) Returns BccList
   End If
   
   ; Add some extra header information to reduce the chance that this is classified as spam.
   Do XtraHdrsList.$define(HeaderType,Value)
   Do XtraHdrsList.$add('Content-Type','text/plain; charset=US-ASCII; format=flowed')
   Do XtraHdrsList.$add('Content-Transfer-Encoding','7bit')
   Do XtraHdrsList.$add('Mime-Version','1.0')
   
   ; Send the email
   ; SMTPSend (iSMTPServer,FromEmailAddr,ToList,pSubject,pBody,CcList,...
   ; ...BccList,FromName,,,XtraHdrsList,iSMTPUser,iSMTPPassword) Returns ErrCode
   SMTPSend (iSMTPServer,FromEmailAddr,ToList,pSubject,pBody,CcList,BccList,FromName,,,XtraHdrsList,iSMTPUser,iSMTPPassword) Returns ErrCode
   If ErrCode<>0
      Calculate Mssg as con("SMTPSend Error - Status error code = ",ErrCode)
      Do errhndlr.$logError($cmethod,Mssg)
      Calculate FlagOK as kFalse
   End If
   
End If
Quit method FlagOK

Email Object - Part 3

  1. Add a convertCSVToList method to oEmail.
  2. Add a parameter, pfTextString, Field reference to the method.
  3. Add the following code to the convertCSVToList method.

Calculate TextString as pfTextString
Do List.$cols.$add('value',kCharacter,kSimplechar,1000000)

While len(TextString)
   
   Calculate Value as trim(strtok('TextString',','))
   If len(Value)
      Do List.$add(Value)
   End If
   
End While

Quit method List

Initialize Email Object

We will instantiate and initialize the oEmail object from the Startup_Task class.

  1. Add the task variable, eml, Object type to the Startup_Task class.
  2. Point the eml task variable to the oEmail object class.
  3. Modify the end of the $construct method of the Startup_Task so that it reads as follows:

    ; Initialize the web monitoring object.
    Do webmon.$initialize() Returns FlagOK
    If FlagOK
       
       ; Initialize the email object.
       ; Do eml.$initialize(SMTPServer,SMTPUser,SMTPPassword,POP3Server,POP3User,...
       ; ...POP3Password,DefaultFromEmailAddr,DefaultFromName) Returns FlagOK
       Do eml.$initialize(SMTPServer,SMTPUser,SMTPPassword,POP3Server,POP3User,POP3Password,DefaultFromEmailAddr,DefaultFromName) Returns FlagOK
       
    End If
    Quit method FlagOK



    You will need to add the local variables that are sent as parameters with the $initialize message sent to the oEmail object.
  4. Set the value of each local variable in the Init Val/Calc column of the variables pane.
    e.g. SMTPServer - 'mail.vencor.ca', SMTPUser = 'doug@vencor.ca'

Test Email Object

You can test the oEmail object by sending an email to yourself using the Programmer Test Method.

  1. Close and reopen the ContactsWeb library in order to initialize the oEmail object.
  2. Contacts menu > Programmer Test Method...
  3. After the Breakpoint and Quit method enter the following code inserting your name and email address in the code.

    Calculate Subject as 'Test Message from oEmail'
    Calculate Body as 'This is a test'
    Calculate To as 'Your Name <your_email_address>'
    Do eml.$sendMail(Subject,Body,To) Returns FlagOK

  4. Contact menu > Programmer Test Method...
  5. Double-click the Calculate To line of code to set the Go point to that line.
  6. Click the Step-in button 4 times to step into the $sendMail method.
  7. Continue stepping through the code all the way through to sending the email. (SMTPSend)
  8. All going well the email will be sent to you through the SMTP server which you specified.
  9. Go to your email application and check your mail. Depending on the speed of the email server you should receive the email sent by the oEmail object within a few minutes.