Skip to content

Instantly share code, notes, and snippets.

@prichelle
Created January 26, 2015 16:52
Show Gist options
  • Save prichelle/2b172867313ca0ac1a41 to your computer and use it in GitHub Desktop.
Save prichelle/2b172867313ca0ac1a41 to your computer and use it in GitHub Desktop.
ESQL to prepare mail with attachments from broker events
CALL CopyMessageHeaders();
-- CALL CopyEntireMessage();
/*
emailoutput node requires the following (with attachments):
- LocalEnvironment->Destination->Email->Attachment
-- Attachment -> Content (BLOB)/ContentType (CHAR)/ContentName(CHAR)/ContentEncoding (CHAR)
- Root -> EmailOutput -> Subject (if you would like to be overriden at runtime
- Root -> BLOB (this is the actual body of the mail
*/
-- Create the LocalEnvironment folders
DECLARE ptrMail REFERENCE TO OutputLocalEnvironment;
CREATE LASTCHILD OF ptrMail AS ptrMail TYPE Name NAME 'Destination';
CREATE LASTCHILD OF ptrMail AS ptrMail TYPE Name NAME 'Email';
DECLARE ptrAttachment REFERENCE TO ptrMail;
-- Get the information from the broker events
DECLARE ptrEvents REFERENCE TO InputRoot.XMLNSC.ns:event.ns:applicationData.ns:complexContent;
WHILE LASTMOVE(ptrEvents) DO
CREATE LASTCHILD OF ptrMail AS ptrAttachment TYPE Name NAME 'Attachment';
SET OutputRoot.XMLNSC.Data = ptrEvents;
-- Content has to be BLOB to be taken as an attachment.
-- Serialize the broker event payload available as complexContent
SET ptrAttachment.Content = ASBITSTREAM(OutputRoot.XMLNSC.Data OPTIONS FolderBitStream);
SET ptrAttachment.ContentType = 'text/xml';
-- Specify the attanchment name
SET ptrAttachment.ContentName = ptrEvents.ns:elementName;
-- Set as binary
SET ptrAttachment.ContentEncoding = 'binary';
DELETE FIELD OutputRoot.XMLNSC;
-- move to the next complexContent
MOVE ptrEvents NEXTSIBLING REPEAT NAME;
END WHILE;
MOVE ptrEvents TO InputRoot.XMLNSC.ns:event.ns:bitstreamData.ns:bitstream;
IF LASTMOVE(ptrEvents) THEN
-- If there is the bitstream available, send it over as attachment
CREATE LASTCHILD OF ptrMail AS ptrAttachment TYPE Name NAME 'Attachment';
--bitstream is base64 encoded, so first we need to decode it
-- ** the node allows to encode the data in Base64, it doesn't allows to provide a already base64 data though
SET ptrAttachment.Content =
BASE64DECODE(InputRoot.XMLNSC.ns:event.ns:bitstreamData.ns:bitstream);
SET ptrAttachment.ContentType = 'text/plain';
SET ptrAttachment.ContentName = 'payload';
-- ask the node to convert it into Base64
SET ptrAttachment.ContentEncoding = 'Base64';
END IF;
-- Set the subject
SET OutputRoot.EmailOutputHeader.Subject = 'Mail Service info';
-- In order to have a pretty message layout, a DFDL is used with CRLF terminator.
SET OutputRoot.Properties.MessageFormat = 'DFDL';
SET OutputRoot.Properties.MessageType = '{}:mailMessage';
SET OutputRoot.DFDL.mailMessage.Header.MessageType = 'INFO';
MOVE ptrEvents TO InputRoot.XMLNSC.ns:event.ns:eventPointData.ns:messageFlowData;
SET OutputRoot.DFDL.mailMessage.Header.Origin = ptrEvents.ns:node.ns:nodeLabel || ptrEvents.ns:node.ns:terminal;
SET OutputRoot.DFDL.mailMessage.Header.Server = ptrEvents.ns:messageFlow.ns:uniqueFlowName;
SET OutputRoot.DFDL.mailMessage.Header.MessageId = InputRoot.XMLNSC.ns:event.ns:eventPointData.ns:eventData.ns:eventCorrelation.ns:localTransactionId;
SET OutputRoot.DFDL.mailMessage.Messages.Message = 'simple content';
-- Serialize the DFDL into BLOB for the OutputMail node.
-- This will be the mail body.
SET OutputRoot.BLOB.BLOB = ASBITSTREAM(OutputRoot.DFDL.mailMessage OPTIONS RootBitStream);
DELETE FIELD OutputRoot.DFDL;
RETURN TRUE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment