Tips_tutorials   >   Studiojs202   >   Web App Stats Email
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.
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
; 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
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
We will instantiate and initialize the oEmail object from the Startup_Task class.
; 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 can test the oEmail object by sending an email to yourself using the .
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