Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Send a custom email (Scriptrunner) Conditional template / BCC / Image / Daylight Savings

Vivo Bandito May 20, 2023

Hey all, 

I've been working on some conditional formatting for the "Send a custom email (Scriptrunner)" post function on Server. Our particular use case for this was to send a custom email for maintenance notifications, unplanned events, and hotfixes. With each of these we've previously had to use an email template an fill it in with the relevant information. Recently we made the push to get this onto Jira and out of our tedious tasks. Here are a few snippets that have worked and might be helpful to you. 

BCC

We have quite a few clients and need for our notifications to be sent out using BCC instead of the standard TO/CC. We use a custom field "BCC" which is a User Picker (multiple users) using the User Picker & Group Searcher template. so that we can send out our notifications to several distribution groups. 

 

import com.atlassian.jira.component.ComponentAccessor

//set recipients from request participants

def multiSelectAddressesCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Bcc..")

def recipients = issue.getCustomFieldValue(multiSelectAddressesCF)?.collect {

    it.emailAddress

}?.join(",")

//BCC Email List

mail.setBcc(recipients)

 

Summary

Our only deviation in summaries were for hotfixes so only two conditions were needed for our 4 different notification types. This was set using two custom fields. One a single select list and the other a scripted multiselect list. I added the replace all as to remove the square brackets around the product custom field. 

 

<% def notificationField = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Notification Email")

   def notificationValue = issue.getCustomFieldValue(notificationField)?.toString()

   def productfield = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Product")

   def productValue = issue.getCustomFieldValue(productfield).toString().replaceAll("\\[|\\]", "")

if (notificationValue != "Hotfix") {

       out << "Notification" + "-" + notificationValue + "-" + productValue

}else if (notificationValue == "Hotfix") {

       out << "Notification" + " - " + "Informational" + " - Hotfix"
}
%>

 

Conditional Formatting

This snippet allows for the same formatting across the entire condition. 

 

<span style="font-family:Calibri;font-size: 15px;;">

    <%

        if (notificationValue != "Hotfix"){

            out << 'Summary: ' + notificationValue + ' for' + productValue + '.'

        }else if (notificationValue == "Hotfix"){

            out << 'Summary: ' + notificationValue + ' deployment for' + productValue + '.'

        }

    %>
</span>

This snippet allows for select formatting depending on the information selected. We use one of two custom fields to add the formatting. 

    <%

        if (updateValue != null){

            out << "<i><span style='color: red; font-family:Calibri;font-size: 15px;'>Update: " + updateValue + "</span></i><br>"

        }

         if (resolvedValue != null){

            out << "<i><span style='color: red; font-family:Calibri;font-size: 15px;'>Resolved: " + resolvedValue + "</span></i><br>"

        }
%>

 With this you could have multiple templates within the same script as we have done with conditional formatting all throughout to differentiate the choices given in Jira. 

 

Images

This one took a sec to figure out, but if you use a transparent png for your email signature this is for you! Most resources have pointed me to putting the image onto our server and referencing the directory or inputting a url to the image. Both of these let to the [x] icon. They were able to be downloaded and I could see them but base64 was the way to go. https://www.base64-image.de/ is a very simple tool that worked on the first try. All you have to do is upload your png and copy the <img> code and place it inside.

One thing I have noticed is that attempting to change the image size like below won't change the image size at all. So this must be changed in an image editor prior to placing it in the tool. 

 

<img src=" YOUR CODE HERE ')}" alt="Image Description" style="max-width: 100%;">

 

Daylight Saving Time

I hate it, you hate it, but it's still here. So here is a snipped that we use in the email template to determine if we are currently in Standard Time or Daylight Saving Time.

<% 
def timeZone

def currentTime = Calendar.getInstance()

def currentYear = currentTime.get(Calendar.YEAR)

def startMarch = Calendar.getInstance()

startMarch.set(currentYear, Calendar.MARCH, 1)

def secondSunday = startMarch

while (secondSunday.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {

    secondSunday.add(Calendar.DAY_OF_MONTH, 1)

}

while (secondSunday.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {

    secondSunday.add(Calendar.DAY_OF_MONTH, 1)

}

def firstNovember = Calendar.getInstance()

firstNovember.set(currentYear, Calendar.NOVEMBER, 1)

while (firstNovember.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {

    firstNovember.add(Calendar.DAY_OF_MONTH, 1)

}

if (currentTime.after(secondSunday) && currentTime.before(firstNovember)) {

    timeZone = "CST"

} else {

    timeZone = "CDT"

}

%>

 

I hope this helps someone out somewhere as it has helped me. 

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events