Clarity

  • 1.  How to code an HTML formatted email using GEL script

    Posted Sep 26, 2013 09:52 PM
    I'd like to share my findings about how to write a HTML formatted email using GEL script. If any of you have a better way to do this, or if you can optimise the code, please share with us. Thanks
    <gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    
    xmlns:core="jelly:core"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:sql="jelly:sql" xmlns:util="jelly:util"
    
    xmlns:xog="http://www.niku.com/xog"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    
    <!-- Variables and Parameter declarations... -->
    
    <core:set value="${true}" var="debug" />
    
    <gel:parameter default="clarityAdmin@mycompany.com" var="FromEmailAddress"/>
    
    
    
    <!--Read Property File... -->
    
    <core:invokeStatic className="com.niku.union.config.ConfigurationManager"
    
    
    method="getInstance" var="config" />
    
    <core:set value="${config.getProperties()}" var="config" />
    
    <core:set value="${config.getMailServer().getHost()}" var="mailServer" />
    
    
    
    <!-- Set MailAgent -->
    
    <core:invokeStatic className="java.lang.Class" method="forName" var="recipientType">
    
    
    <core:arg type="java.lang.String" value="javax.mail.Message$RecipientType" />
    
    </core:invokeStatic>
    
    <core:set value="${recipientType.getField('TO')}" var="recipientTypeTO" />
    
    <core:set value="${recipientTypeTO.TO}" var="recipientTypeTO" />
    
    <core:set value="${recipientType.getField('CC')}" var="recipientTypeCC" />
    
    <core:set value="${recipientTypeCC.CC}" var="recipientTypeCC" />
    
    <core:set value="${recipientType.getField('BCC')}" var="recipientTypeBCC" />
    
    <core:set value="${recipientTypeBCC.BCC}" var="recipientTypeBCC" />
    
    
    
    
    
    <!-- Initiate a new java mail session -->
    
    <core:new className="java.util.Properties" var="props" />
    
    <core:set value="${props.put('mail.transport.protocol', 'smtp')}" var="void" />
    
    <core:set value="${props.put('mail.smtp.host', mailServer)}" var="void" />
    
    <core:set value="${props.put('mail.smtp.auth', 'true')}" var="void" />
    
    <core:new className="com.niku.union.notification.MailAuthenticator" var="clarity_auth" />
    
    <core:invokeStatic className="javax.mail.Session" method="getInstance" var="mailSession">
    
    
    <core:arg type="java.util.Properties" value="${props}" />
    
    
    <core:arg type="javax.mail.Authenticator" value="${clarity_auth}" />
    
    </core:invokeStatic>
    
    
    
    <!-- Add sample HTML email message -->
    
    <gel:parse var="htmlMessage">
    
    
    <message><![CDATA[
    
    
    <html>
    
    
    <h1>Hello World</h1>
    
    
    
    
    </html>
    
    
    ]]></message>
    
    </gel:parse>
    
    <gel:forEach select="$htmlMessage/message/node()" var="thisMessageNode">
    
    
    <gel:set asString="true" select="$thisMessageNode" var="thisMessageNodeText" />
    
    
    <core:set value="${thisMessageText}${thisMessageNodeText}" var="thisMessageText" />
    
    </gel:forEach>
    
    
    
    <!-- Add sender -->
    
    <core:new className="javax.mail.internet.InternetAddress" var="sender">
    
    
    <core:arg type="java.lang.String" value="${FromEmailAddress}" />
    
    </core:new>
    
                                   
     
    <!-- Add recipients -->
     
    <core:new className="javax.mail.internet.InternetAddress" var="internetAddress"/>
     
    <core:set value="user1@mycompany.com,user2@mycompany.com" var="recipientsTO" />
    
    <core:set value="me@mycompany.com" var="recipientsCC" />
                                 
    
    <!-- Add subject -->
    
    <core:set value="Hello World!" var="subject" />
    
    
    
    
    <!-- Add email body text and format -->
    
    <core:new className="javax.mail.internet.MimeMultipart" var="multiPart" />
    
    <core:new className="javax.mail.internet.MimeBodyPart" var="bodyPart" />
    
    <core:set value="${bodyPart.setContent(thisMessageText, 'text/html')}" var="void" />
    
    <core:set value="${multiPart.addBodyPart(bodyPart)}" var="void" />
    
    
    
    <!-- Send email message -->
    
    <core:new className="javax.mail.internet.MimeMessage" var="message">
    
    
    <core:arg type="javax.mail.Session" value="${mailSession}" />
    
    </core:new>
    
    <core:set value="${mailSession.setDebug(debug)}" var="void" />
    
    <core:set value="${message.setFrom(sender)}" var="void" />
    
    
    
    <core:choose>
    
    
    <core:when test="${recipientsTO.indexOf(',') > 0}">
    
    
    
    <core:set value="${message.setRecipients(recipientTypeTO, internetAddress.parse(recipientsTO))}" var="void" />
                            
    
    
    
    
    </core:when>
    
    
    <core:otherwise>
    
    
    
    <core:new className="javax.mail.internet.InternetAddress" var="recipientsTO">
    
    
    
    
    <core:arg type="java.lang.String" value="${recipientsTO}" />
    
    
    
    </core:new>
    
    
    
    
    <core:set value="${message.setRecipient(recipientTypeTO, recipientsTO)}" var="void" />
    
    
    </core:otherwise>
    
    </core:choose>
    
    
    
    <core:choose>
    
    
    <core:when test="${recipientsCC.indexOf(',') > 0}">
    
    
    
    <core:set value="${message.setRecipients(recipientTypeCC, internetAddress.parse(recipientsCC))}" var="void" />
                            
    
    
    
    
    </core:when>
    
    
    <core:otherwise>
    
    
    
    <core:new className="javax.mail.internet.InternetAddress" var="recipientsCC">
    
    
    
    
    <core:arg type="java.lang.String" value="${recipientsCC}" />
    
    
    
    </core:new>
    
    
    
    
    <core:set value="${message.setRecipient(recipientTypeCC, recipientsCC)}" var="void" />
    
    
    </core:otherwise>
    
    </core:choose>
    
    
    
    
    <core:set value="${message.setSubject(subject)}" var="void" />
    
    <core:set value="${message.setContent(multiPart)}" var="void" />
    
    <core:set value="${mailSession.getTransport()}" var="transport" />
    
    <core:set value="${transport.connect()}" var="void" />
    
    
    
    <core:switch on="${transport.isConnected()}">
    
    
    <core:case value="${true}">
    
    
    
    <core:set value="${transport.send(message)}" var="void" />
    
    
    
    <core:set value="${transport.close()}" var="void" /> 
    
    
    </core:case>
    
    
    <core:default>
    
    
    
    <core:new className="java.lang.Exception" var="exception">
    
    
    
    
    <gel:log level="ERROR">"Cannot connect to mail server: ${MailServer}"</gel:log>
    
    
    
    </core:new>
    
    
    </core:default>
    
    </core:switch>
    </gel:script>


  • 2.  RE: How to code an HTML formatted email using GEL script

     
    Posted Sep 30, 2013 01:14 PM
    Hi all,

    Anyone have input here for William?

    Thanks!
    Chris


    william_w wrote:

    I'd like to share my findings about how to write a HTML formatted email using GEL script. If any of you have a better way to do this, or if you can optimise the code, please share with us. Thanks
    <gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    
    xmlns:core="jelly:core"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:sql="jelly:sql" xmlns:util="jelly:util"
    
    xmlns:xog="http://www.niku.com/xog"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    
    <!-- Variables and Parameter declarations... -->
    
    <core:set value="${true}" var="debug" />
    
    <gel:parameter default="clarityAdmin@mycompany.com" var="FromEmailAddress"/>
    
    
    
    <!--Read Property File... -->
    
    <core:invokeStatic className="com.niku.union.config.ConfigurationManager"
    
    
    method="getInstance" var="config" />
    
    <core:set value="${config.getProperties()}" var="config" />
    
    <core:set value="${config.getMailServer().getHost()}" var="mailServer" />
    
    
    
    <!-- Set MailAgent -->
    
    <core:invokeStatic className="java.lang.Class" method="forName" var="recipientType">
    
    
    <core:arg type="java.lang.String" value="javax.mail.Message$RecipientType" />
    
    </core:invokeStatic>
    
    <core:set value="${recipientType.getField('TO')}" var="recipientTypeTO" />
    
    <core:set value="${recipientTypeTO.TO}" var="recipientTypeTO" />
    
    <core:set value="${recipientType.getField('CC')}" var="recipientTypeCC" />
    
    <core:set value="${recipientTypeCC.CC}" var="recipientTypeCC" />
    
    <core:set value="${recipientType.getField('BCC')}" var="recipientTypeBCC" />
    
    <core:set value="${recipientTypeBCC.BCC}" var="recipientTypeBCC" />
    
    
    
    
    
    <!-- Initiate a new java mail session -->
    
    <core:new className="java.util.Properties" var="props" />
    
    <core:set value="${props.put('mail.transport.protocol', 'smtp')}" var="void" />
    
    <core:set value="${props.put('mail.smtp.host', mailServer)}" var="void" />
    
    <core:set value="${props.put('mail.smtp.auth', 'true')}" var="void" />
    
    <core:new className="com.niku.union.notification.MailAuthenticator" var="clarity_auth" />
    
    <core:invokeStatic className="javax.mail.Session" method="getInstance" var="mailSession">
    
    
    <core:arg type="java.util.Properties" value="${props}" />
    
    
    <core:arg type="javax.mail.Authenticator" value="${clarity_auth}" />
    
    </core:invokeStatic>
    
    
    
    <!-- Add sample HTML email message -->
    
    <gel:parse var="htmlMessage">
    
    
    <message><![CDATA[
    
    
    <html>
    
    
    <h1>Hello World</h1>
    
    
    
    
    </html>
    
    
    ]]></message>
    
    </gel:parse>
    
    <gel:forEach select="$htmlMessage/message/node()" var="thisMessageNode">
    
    
    <gel:set asString="true" select="$thisMessageNode" var="thisMessageNodeText" />
    
    
    <core:set value="${thisMessageText}${thisMessageNodeText}" var="thisMessageText" />
    
    </gel:forEach>
    
    
    
    <!-- Add sender -->
    
    <core:new className="javax.mail.internet.InternetAddress" var="sender">
    
    
    <core:arg type="java.lang.String" value="${FromEmailAddress}" />
    
    </core:new>
    
                                   
     
    <!-- Add recipients -->
     
    <core:new className="javax.mail.internet.InternetAddress" var="internetAddress"/>
     
    <core:set value="user1@mycompany.com,user2@mycompany.com" var="recipientsTO" />
    
    <core:set value="me@mycompany.com" var="recipientsCC" />
                                 
    
    <!-- Add subject -->
    
    <core:set value="Hello World!" var="subject" />
    
    
    
    
    <!-- Add email body text and format -->
    
    <core:new className="javax.mail.internet.MimeMultipart" var="multiPart" />
    
    <core:new className="javax.mail.internet.MimeBodyPart" var="bodyPart" />
    
    <core:set value="${bodyPart.setContent(thisMessageText, 'text/html')}" var="void" />
    
    <core:set value="${multiPart.addBodyPart(bodyPart)}" var="void" />
    
    
    
    <!-- Send email message -->
    
    <core:new className="javax.mail.internet.MimeMessage" var="message">
    
    
    <core:arg type="javax.mail.Session" value="${mailSession}" />
    
    </core:new>
    
    <core:set value="${mailSession.setDebug(debug)}" var="void" />
    
    <core:set value="${message.setFrom(sender)}" var="void" />
    
    
    
    <core:choose>
    
    
    <core:when test="${recipientsTO.indexOf(',') > 0}">
    
    
    
    <core:set value="${message.setRecipients(recipientTypeTO, internetAddress.parse(recipientsTO))}" var="void" />
                            
    
    
    
    
    </core:when>
    
    
    <core:otherwise>
    
    
    
    <core:new className="javax.mail.internet.InternetAddress" var="recipientsTO">
    
    
    
    
    <core:arg type="java.lang.String" value="${recipientsTO}" />
    
    
    
    </core:new>
    
    
    
    
    <core:set value="${message.setRecipient(recipientTypeTO, recipientsTO)}" var="void" />
    
    
    </core:otherwise>
    
    </core:choose>
    
    
    
    <core:choose>
    
    
    <core:when test="${recipientsCC.indexOf(',') > 0}">
    
    
    
    <core:set value="${message.setRecipients(recipientTypeCC, internetAddress.parse(recipientsCC))}" var="void" />
                            
    
    
    
    
    </core:when>
    
    
    <core:otherwise>
    
    
    
    <core:new className="javax.mail.internet.InternetAddress" var="recipientsCC">
    
    
    
    
    <core:arg type="java.lang.String" value="${recipientsCC}" />
    
    
    
    </core:new>
    
    
    
    
    <core:set value="${message.setRecipient(recipientTypeCC, recipientsCC)}" var="void" />
    
    
    </core:otherwise>
    
    </core:choose>
    
    
    
    
    <core:set value="${message.setSubject(subject)}" var="void" />
    
    <core:set value="${message.setContent(multiPart)}" var="void" />
    
    <core:set value="${mailSession.getTransport()}" var="transport" />
    
    <core:set value="${transport.connect()}" var="void" />
    
    
    
    <core:switch on="${transport.isConnected()}">
    
    
    <core:case value="${true}">
    
    
    
    <core:set value="${transport.send(message)}" var="void" />
    
    
    
    <core:set value="${transport.close()}" var="void" /> 
    
    
    </core:case>
    
    
    <core:default>
    
    
    
    <core:new className="java.lang.Exception" var="exception">
    
    
    
    
    <gel:log level="ERROR">"Cannot connect to mail server: ${MailServer}"</gel:log>
    
    
    
    </core:new>
    
    
    </core:default>
    
    </core:switch>
    </gel:script>


  • 3.  RE: How to code an HTML formatted email using GEL script
    Best Answer

    Posted Sep 30, 2013 03:55 PM
    Hello William,

    Are you on V12 or v13?
    In V13 you can send HTML emails without so much coding..

    Here is a sample script which could be optimized and formatted but works absolutely fine...


    <gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" xmlns:j="jelly:core" xmlns:mail="jelly:email"
    xmlns:sql="jelly:sql" xmlns:util="jelly:util" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <gel:setDataSource dbId="Niku"/>
    <!-- GET APPLICATION URL FROM PROPERTIES.XML FILE -->
    <core:invokeStatic className="java.lang.System" method="getenv" var="nikuHome">
    <core:arg value="NIKU_HOME"/>
    </core:invokeStatic>
    <gel:parse file="${nikuHome}/config/properties.xml" var="propertiesXML"/>
    <gel:set asString="true" select="$propertiesXML/properties/webServer/webServerInstance[@id='app']/@entryUrl" var="appURL"/>

    <gel:log level="INFO">App URL: ${appURL}</gel:log>
    <sql:query escapeText="0" var="result"><![CDATA[
    SELECT distinct manager.email email, manager.full_name, manager.id
    FROM SRM_resources res
    INNER JOIN SRM_resources manager ON res.Manager_ID = manager.user_id
    WHERE res.person_type = 301
    AND res.date_of_termination <= trunc(SYSDATE) + 28
    AND res.is_active = 1

    ]]></sql:query>
    <core:forEach items="${result.rowsByIndex}" trim="true" var="row">
    <core:set value="${row[0]}" var="mgrEmail"/>
    <core:set value="${row[1]}" var="mgrName"/>
    <core:set value="${row[2]}" var="rm_Id"/>
    <gel:log trim="false"> Resource: ${resName} Actual Email Receipients: ${mgrEmail}</gel:log>
    <gel:email from="abc@abc.ca" subject="Clarity - Contractor(s) With Termination Date in 4 weeks or less"
    to="xyz@abc.ca">


    &lt;b&gt;
    Resource Manager: ${mgrName}&lt;/b&gt;
    &lt;BR&gt;
    &lt;BR&gt;
    The contracts for the following resources will terminate on the identified date:
    &lt;BR&gt;
    &lt;BR&gt;



    <sql:query
    escapeText="0"
    var="result2">

    SELECT manager.email email, manager.full_name, res.full_name, res.email, To_Char(res.date_of_termination, 'DD/MM/YYYY') termination_date,
    '${appURL}' || '/niku/nu#action:projmgr.editResource' || '&amp;id=' || to_char(res.id)
    FROM SRM_resources res
    INNER JOIN SRM_resources manager ON res.Manager_ID = manager.user_id
    WHERE res.person_type = 301
    AND res.date_of_termination &lt; trunc(SYSDATE) + 28
    AND res.is_active = 1
    AND manager.id = ${rm_Id}

    </sql:query>
    &lt;table border="1"&gt;
    &lt;tr&gt;
    &lt;th bgcolor="#D3D3D3"&gt;Resource &lt;/th&gt;
    &lt;th bgcolor="#D3D3D3"&gt;Termination Date &lt;/th&gt;
    &lt;th bgcolor="#D3D3D3"&gt;Email &lt;/th&gt;
    &lt;/tr&gt;


    <core:forEach
    items="${result2.rowsByIndex}" trim="true" var="row2">
    <core:set value="${row2[0]}" var="mgrEmail"/>
    <core:set value="${row2[1]}" var="mgrName"/>
    <core:set value="${row2[2]}" var="resName"/>
    <core:set value="${row2[3]}" var="resEmail"/>
    <core:set value="${row2[4]}" var="termDate"/>
    <core:set value="${row2[5]}" var="tmlink"/>
    &lt;tr&gt;
    &lt;td&gt; &lt;center&gt; &lt;a href=${tmlink}&gt;${resName} &lt;/a&gt; &lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;${termDate} &lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;${resEmail} &lt;/center&gt;&lt;/td&gt;
    &lt;/tr&gt;


    </core:forEach>
    &lt;/table&gt;
    &lt;BR&gt;
    &lt;BR&gt;
    &lt;BR&gt;
    Technical issues should be reported to Clarity administrators. Thank you in advance for your time and attention.
    &lt;BR&gt;
    &lt;BR&gt;


    &lt;BR&gt;
    -------------------------------------------------------------------
    &lt;BR&gt;
    This is an automated message. Please do not reply.


  • 4.  Re: How to code an HTML formatted email using GEL script

    Posted Feb 13, 2015 02:42 AM

    Hi All,

     

    I thought I'd pitch in with another way that I got working, no java or other special things needed, just gel. Some may say that the way I do it below is not the greatest way to put together an HTML email, and I won't argue...

     

    The main thing that allowed me to do this is the realisation that there's nothing stopping you from having many <![CDATA[...whatever literals you put in here...]]> to construct your final email content, so:

     

    My email has some descriptive text,

    a table in html format with a project per row (and a URL in the 'Project ID' column),

    and some footer info.

     

    The table's rows are gathered from a sql query (each row is in the variable v_row).

     

    I've omitted lots of the gel below, but I hope that most of it makes sense...

     

    1. Parse an xml structure, which has the 'headings' of the table in the html

            <gel:parse var="v_mail_content">

                <table>

                    <tr>

                        <th align="left">Project Manager</th>

                        <th align="left">Project ID</th>

                        <th align="left">Project Name</th>

                        <th align="right">Num Of ***</th>

                        <th align="right">*** Amount</th>

                    </tr>

                </table>

            </gel:parse>

     

    2. for each row in the sql query results, construct it's URL (broken out here to avoid a very wide single line

                <core:set value="https://cppm-XXXXX.ondemand.ca.com/niku/nu#action:projmgr.projectProperties" var="v_inv_url"/>

                <core:set value="${v_inv_url + '&amp;odf_view=projectCreate.subObjList.XXXX_XXXX&amp;id=' + v_row.id}" var="v_inv_url"/>

                <core:set value="${v_inv_url + '&amp;odf_pk=' + v_row.id + '&amp;parentObjectCode=project&amp;odf_concrete_parent_object_code=project&amp;odf_parent_id=' + v_row.id}" var="v_inv_url"/>

                <core:set value="${v_inv_url + '&amp;odf_cncrt_parent_id=' + v_row.id}" var="v_inv_url"/>

      

    3. Create a html row from the data in the sql query and the constructed URL using parse

            <gel:parse var="v_detail_item">

                <tr>

                    <td align="left">${row.full_name}</td>

                    <td align="left"><a href="${v_inv_url}">${v_row.code}</a></td>

                    <td align="left">${v_row.name}</td>

                    <td align="right">${v_row.a_count}</td>

                    <td align="right">${v_row.an_amount}</td>

                </tr>

            </gel:parse>

     

    4. Insert this detail item in the main table (and iterate around steps 2,3,4)      

            <core:catch var="gel_error">

                <gel:set select="$v_mail_content/table" value="${v_detail_item}" insert="true" trim="false"/>

            </core:catch>

                  

    5. Make a string version of the created table.

            <core:catch var="v_exception">

                <gel:set asString="true" select="$v_mail_content/table" var="v_variable"/>

            </core:catch>

     

    6. Here's the fun bit. Everything that is HTML specific jargon is encased in CDATA (many of them!), including the styling defined in the head section. Further in, I dump in the string-version of the html table/row/div constructed further up.

            <gel:email

                from="clarity_admin@yourcompany.com.au"

                fromName="Clarity Admin"

                subject="Please action XXXX in Clarity"

                to="samos2@yourcompany.com.au,clarity_admin@yourcompany.com.au">

                <![CDATA[<html>

                <head>

                <style>

                table, th, td {

                    border: 1px solid black;

                    border-collapse: collapse;

                }

                th, td {

                    padding: 5px;

                }

                th {

                    text-align: left;

                }

                </style>

                </head>

                <body>

                <font face="verdana" size="2">

                </br></br>

                <p>This email is a reminder to action the following in your Clarity projects:</p>

                </br></br>

                ]]>

                ${v_variable}

                <![CDATA[</br></br>]]>

                Please contact <![CDATA[<a href="mailto:Clarity_Admin@yourcompany.com.au">Clarity Admin</a>]]> for any enquiries.

                <![CDATA[</br></br>]]>

                (Automated email sent ${v_row.mail_time})

                <![CDATA[</br></font></body></html>]]>

            </gel:email>

     

    I'll try to attach a picture of the result.

          

    Hope that helps the next person...

     

    Sam

     

     

    2015-02-13.png



  • 5.  Re: How to code an HTML formatted email using GEL script

    Posted Aug 25, 2015 04:48 PM

    Thanks for posting this, I found it very useful!



  • 6.  Re: How to code an HTML formatted email using GEL script

    Posted Apr 03, 2019 01:16 PM

    Hi Sam, 

     

    Can I have a complete GEL scripts so that it will be helpful for a better understanding ?