# JavaScript in Mail Task

**URL:** <https://forum.flowable.org/t/javascript-in-mail-task/1043>\
**Category:** Flowable Engine\
**Created:** [October 9, 2017, 7:06pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043 "2017-10-09T19:06:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinayksharma](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vinayksharma](https://forum.flowable.org/u/vinayksharma)\
**Post date:** [October 9, 2017, 7:06pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/1 "2017-10-09T19:06:26Z")

</div>

Hi

Have created a process which needs to send Emails. In this email, I need to format the text based on some variable i.e. I have tried to use if/else conditions using Javascript. But this is not working for me. My question is , can I java-script inside mail task body ? If not, then is there any other way of formatting the text .Here is what am trying to do inside the “html” attribute of Mail-task : -

Hello Solution Delivery Team   
  
  
A new request has been submitted with the following details :-   
   
  
**Project Name:** ${projectNm}

Please initiate the following tasks:-   
  
**Application Ids required:-**

- Gold   
- Platinum

---

<div class="post-metadata">

**Author:** ![PHH](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/phh/32/94_2.png) [@PHH](https://forum.flowable.org/u/PHH)\
**Post date:** [October 9, 2017, 8:50pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/2 "2017-10-09T20:50:50Z")

</div>

Hi,

I’m not entirely clear what you’re trying to do, but you can use variables to insert whatever simple html content you want. I think you could have the process set the content through conditional flows or script tasks and just have an html attribute with a value something like “`${emailTop}<br><p>Constant content</p>${emailBody1}<p>More constant content</p>${emailBody2}<br>${emailFooter}`”, with the variables also containing simple html, e.g. emailTop = “`<h1>Hello!</h1>`”.

Cheers  
Paul.

---

<div class="post-metadata">

**Author:** ![vinayksharma](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vinayksharma](https://forum.flowable.org/u/vinayksharma)\
**Post date:** [October 9, 2017, 11:26pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/3 "2017-10-09T23:26:24Z")

</div>

Thanks @PHH for replying. What am trying to achieve is Format the text e.g. (as per your example ) I want ${emailBody2} to appear only if its NOT NULL i.e. something like

if(null != ${emailBody2} ) {  
 **Email Body 2 is : ${emailBody2}  
}**

Hope , I clarified what exactly am looking for.

Thanks again.

---

<div class="post-metadata">

**Author:** ![PHH](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/phh/32/94_2.png) [@PHH](https://forum.flowable.org/u/PHH)\
**Post date:** [October 10, 2017, 8:54am UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/4 "2017-10-10T08:54:44Z")

</div>

You can use the JUEL expressions more fully, for example:  
`<h1>Hello</h1>`  
`${ ! empty myVar1 ? myVar1 : "<p><i>Something else<i></p>" }`

So if myVar1 is not null or the empty string, it gets output (containing html, if desired), otherwise something else is output.

Cheers  
Paul.

---

<div class="post-metadata">

**Author:** ![vinayksharma](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vinayksharma](https://forum.flowable.org/u/vinayksharma)\
**Post date:** [October 10, 2017, 9:43pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/5 "2017-10-10T21:43:37Z")

</div>

Thanks Paul this works. Is there any way , I can use Java directly to do this if/else stuff in mail-task ?

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [October 18, 2017, 5:31pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/6 "2017-10-18T17:31:04Z")

</div>

No. An alternative could be to have a java service tasks that prepares this text in Java and stores it as a transient variable that’s referenced in the mail task.

---

<div class="post-metadata">

**Author:** ![vinayksharma](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vinayksharma](https://forum.flowable.org/u/vinayksharma)\
**Post date:** [October 18, 2017, 5:51pm UTC](https://forum.flowable.org/t/javascript-in-mail-task/1043/7 "2017-10-18T17:51:40Z")

</div>

Thanks @joram, Got it . 🙂
