Introduction
It is very common to send email notification for important event in the enterprise integration. Mule AnyPoint Platform come with parse-template component, which allow us to pass flow variables, payload, etc to the html template. This approach is very simple and straight forward for the simple email notification. The another approach is to using apache velocity engine to parse more complicated data set to the html template.
In this post, I am going to introduce the both. For the email server, I am going to using Gmail.
The complete code is available at my github repository
The Requirements
The requirement is that clients will pass a json request contains the informaiton as shown in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { "subject" : "Connection To Oracle Database Not Available" , "emailTo" : "gary.liu1119@gmail.com" , "emailFrom" : "guojiang.liu1119@gmail.com" , "replyTo" : "gary.liu1119@gmail.com" , "integrationId" : "OracleDB-To-SFDC" , "body" : "This is the body message" , "channel" : "slack channel" , "footer" : "This is generated email. Please DO NOT Reply to this email Best Regards, Mule Integration Team" , "payload" : { "header" : [ "column_a" , "column_b" , "column_c" , "column_d" , "column_f" ], "data" : [ [ "column_a_data1" , "column_b_data1" , "column_c_data1" , "column_d_data1" , "column_f_data1" ], [ "column_a_data2" , "column_b_data2" , "column_c_data2" , "column_d_data2" , "column_f_data2" ], [ "column_a_data3" , "column_b_data3" , "column_c_data3" , "column_d_data3" , "column_f_data3" ] ] } } |

Using Template Parser
This is very simple scenario. We will use the <parse-template ...=""> component as the following:1 2 | < parse-template doc:name = "Parse Template" location = "email-notification-template-plain.html" > </ parse-template > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < html > < head > < title >#[original_payload.subject]</ title > </ head > < body > Environment: #[environment] Date: #[system_date] Integration Case ID: #[original_payload.integrationId] #[original_payload.body] #[original_payload.footer] </ body > </ html > |
Using Velocity
Apache Velocity Engine is a powerful tool for build complicated html template. Here I only introduce the for loop. For more directives, you may refer the following document.The html template is shown as the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | < HTML > < HEAD > < TITLE >$emailInfo.subject</ TITLE > </ HEAD > < BODY > Environment: $emailInfo.environment Integration ID: $emailInfo.integrationId $emailInfo.body < TABLE width = "70% " border = "1" cellspacing = "0" cellpadding = "2" font = "5" > < TR style = "text-align: left; font-size: 14px; font-weight: bold; color: #000000;" > #foreach ($headerCol in $emailInfo.payload.header) < TH >$headerCol</ TH > #end </ TR > #foreach ($data in $emailInfo.payload.data) < TR style = "text-align: left; font-size: 13px; font-weight: bold; color: #488AC7;" > #foreach ($col in $data) < TD >$col</ TD > #end </ TR > #end </ TABLE > < I >$emailInfo.footer</ I > </ BODY > </ HTML > |
As you can see that for loop takes the following form:
1 2 3 | #foreach ($headerCol in $emailInfo.payload.header) < TH >$headerCol</ TH > #end |
Where the emailInfo is a java object in the form of: LinkedHashMap
The java code is in the following form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class VelocityComponent implements Callable { private static Logger logger = LoggerFactory.getLogger(VelocityComponent. class ); @SuppressWarnings ( "unchecked" ) @Override public Object onCall(MuleEventContext eventContext) throws Exception { LinkedHashMap<string object= "" > payload = (LinkedHashMap<string object= "" >)eventContext.getMessage().getPayload(); String emailHtml = this .buildEmailHtml(payload); return emailHtml; } public String buildEmailHtml(LinkedHashMap<string object= "" > emailInfo) throws Exception { VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath" ); ve.setProperty( "classpath.resource.loader.class" , ClasspathResourceLoader. class .getName()); ve.init(); String environment = System.getProperty( "mule.env" ); emailInfo.put( "environment" , environment); VelocityContext context = new VelocityContext(); context.put( "emailInfo" , emailInfo); Template t = ve.getTemplate( "email-notification-template-velocity.vm" ); StringWriter writer = new StringWriter(); t.merge( context, writer ); logger.info( writer.toString() ); return writer.toString(); } } </string></string></string> |
Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDelete