Sending Event Based Email Notifications in Silk Performer
In this example:
- An event handler is defined to handle specific errors and if a condition is met an email is sent with details of the error.
- Project attributes are used to store the email function parameters such as SMTP server, recipient, sender etc. This makes changing these settings very easy.
- HTML files are added to the data files section and account for most of the HTML-formatted email. Using HTML files allows for easy customization of the email look and feel in an external HTML editor rather than coding the entire HTML body in the function itself which would be messy.
- The body of the HTML message contains placeholders that are replaced by dynamic values about the error at runtime. Defining these placeholders in the HTML makes it easy to find and replace them using string replace functions.
- An include file (.bdh) is used to define a function used by the event handler to compose and send the email.
- The email functionality resides in a DLL generated in Visual studio which leverages the .NET Framework System.Net.Mail namespace. This DLL receives the email parameters specified in the attributes and also the error details generated at runtime, sends the email message and returns a string about the status of that operation.
To use the script, first configure the email parameters. From the Workbench, go to PROJECT | PROJECT ATTRIBUTES, and enter the information suitable to your environment.
The script raises the error ‘BrowserEngine: 32’ by requesting a url which will return 404 file not found. The event handler captures any error and the time it occurs and calls the custom SendMail() function defined in the include file ‘email.bdh’. This is the event handler code:
The SendMail() function receives the last error and the time the error occurred as input parameters. Both HTML files, ‘header.html’ and ‘body.html’ are used by the function to create the HTML-formatted message, but before that, the ‘body.html’ is loaded in memory and edited.
The following placeholders exist in the ‘body.html’ file:
The script uses FStrReplace() to find and replace the placeholders with the actual values obtained from the event handler and Silk Performer control functions such as GetAgent(), GetScriptName(), GetUserGroup() and GetProfile().
Edit the ‘body.html’ file and add new columns and placeholders, and extend the SendMail() instructions to comprise the new values.
This is how the placeholders are populated with the actual values of the error and runtime information:
The final step is sending the email. This sample code shows the contents of the DLL in Visual Studio:
Calling the .Net function involves:
- load the .NET object (DLL).
- define each parameter in the order the DLL expects to receive, using the DotNetSetString() function.
- call the method using DotNetCallMethod().
- get the return value status message using DotNetGetString().
- Clear memory allocated to the dll and function using DotNetFreeObject().
The resulting email looks like this:
Download sample project here: EmailOnError.zip