Qlik Community

Qlik Architecture Deep Dive Blog

Deep dives into specific back-end technologies which allow for the extension of Qlik to fit the needs of the enterprise.

Employee
Employee

Qlik SMTP / Notification Connector

A review of the Qlik SMTP / Notification Connector which is a part of the  Qlik Web Connectors package

This is a more in-depth post as a follow up to the overview post: Getting Notified from Qlik Sense.

The SMTP / Notification Connector is a part of the Qlik Web Connectors package. This connector allows a developer to send emails in the context of a Qlik load script. As opposed to the other approaches outlined (smtp or udp Appenders, QRS Notification API), this approach is ideally used inside of scripts where the ErrorMode variable is set to 0 to ignore script errors. Otherwise, the faulting error will cause the script execution to halt and the push to the Connector will not occur.

The main drawback of this approach is that it require the Qlik Web Connectors package to be installed and, ideally, running as a service. If you already use the Qlik Web Connectors then this approach can be easily integrated. If you do not, then do refer to the documentation on the Qlik Web Connectors.

In an adapted script re-purposed and adapted from Richard Byard’s excellent Qlik Community Post, we use the following logic in the context of the load script to send a heavily customized HTML email on failure:

 

/**************************************************************************************/
/*************** Create Variables for Email Sender and Recipients *********************/
/**************************************************************************************/
// Identify the recipients of the alert emails. Multiple recipients should be separated by a semicolon ;
Let vAlertRecipients = 'you@company.tld';

// Setup the sent from email account - This can be a dummy account if required.
Let vAlertFromName = 'Qlik Alerts';
Let vAlertFromEmail = 'you@company.tld';

/**** Manipulate these variable text strings to URL Encode the text ****/
/*DO NOT EDIT*/ Let vRecipients = replace(replace('$(vAlertRecipients)','@','%40'),';','%3b'); // replace @ and semicolon ; in the string
/*DO NOT EDIT*/ Let vFromName = replace('$(vAlertFromName)',' ','+'); // replace spaces with + signs
/*DO NOT EDIT*/ Let vFromEmail = replace('$(vAlertFromEmail)','@','%40'); // replace @ with %40



/**************************************************************************************/
/*************** Create Content for Reload Commenced Email ****************************/
/**************************************************************************************/
// Enter the text that you wish to send in the RELOAD START email
Let vTimestampSTART = timestamp(now());
Let vAlertSubjectInputSTART = 'RELOAD COMMENCED: '&documentname()&' at '&'$(vTimestampSTART)';
Let vAlertMessageInputSTART = '<p>Application reload: <br><br>'&documentname()&'<br><br>'&'$(vTimestampSTART)'&'</p>';

/**** Manipulate these variable text strings to URL Encode the text ****/
/*DO NOT EDIT*/ Let vSubject = replace('$(vAlertSubjectInputSTART)',' ','+');
/*DO NOT EDIT*/ Let vMessage = replace('$(vAlertMessageInputSTART)',' ','+');

/**************************************************************************************/
/*************** Actual Data Load *****************************************************/
/**************************************************************************************/

LOAD * INLINE [
name, value
1,100
2,100
3,100
4,100
5,100
6,100];

// Purposefully erroneous statement to create an error message.
LOAD a dual b;

// add a sleep for 60 seconds to simulate a more realistic reload window to show times between emails.
sleep 100;

// Enter the text that you wish to send in the RELOAD SUCCESSFUL email
Let vTimestampEND = timestamp(now());
Let vReloadDuration = time(num('$(vTimestampEND)')-num('$(vTimestampSTART)'),'hh:mm:ss');
Let vScriptErrorCount = ScriptErrorCount;
//Let vScriptErrorList = ScriptErrorList; // this returns characters that cause the load to fail as do not pass in URL
Let vAlertSubjectInputEND = 'Reload Completed:'&DocumentTitle()&' at '&'$(vTimestampEND)';
Let vAlertMessageInputEND = '<p>Application reloaded in <b>'&'$(vReloadDuration)'&'</b> with <b>'&'$(vScriptErrorCount)'&'</b> errors:<br><br>'&DocumentTitle()&'<br><br>'&'$(vTimestampSTART)'&'<br><br>'&'To Review, <a href="https://QLIKSENSESERVER/qmc">Go to the QMC</a>'/*&'<br><br>Script Error List:<br>'&'$(vScriptErrorList)'*/&'</p>';
//'The '&documentname()&' application completed reloading taking '&'$(vReloadDuration)'&' (hh:mm:ss) at '&'$(vTimestampEND)';
Let vQwcConnectionName = 'lib://QlikWebConnectors';



/**** Manipulate these variable text strings to URL Encode the text ****/
/*DO NOT EDIT*/ Let vSubject = replace('$(vAlertSubjectInputEND)',' ','+');
/*DO NOT EDIT*/ Let vMessage = replace('$(vAlertMessageInputEND)',' ','+');

NOTIFICATIONS:
LOAD
    '$(vTimestampSTART)' as TimestampSTART,
    '$(vTimestampEND)' as TimestampEND,
    '$(vReloadDuration)' as ReloadDuration,
    status as status_END,
    result as result_END//,
FROM [$(vQwcConnectionName)]
(URL IS [http://localhost:5555/data?connectorID=SMTPConnector&table=SendEmail&UserName=you%40company.tld&SMTPServer=localhost&Port=25&SSLmode=None&to=$(vRecipients)&subject=$(vSubject)&message=$(vMessage)&html=True&fromName=$(vFromName)&fromEmail=$(vFromEmail)&delayInSeconds=0&appID=], qvx);

 

Since this config is leveraging a local demo email server, adjustments of the UserNameSMTPServerSSLMode params will be required.

A copy of the app can be found on this post series’ GitHub Repository.

The email that is sent will look like this:

notified_6.png

 

1 Comment
j_star
New Contributor

Every time i work with the smtp Connector,  i wonder what the "&appID=" syntax in the URL is....

I tried to fill it with the APP GUID but nothing happens. 🙂 


0 Likes
142 Views