How to do it...

The first step is to create our custom email templates. All mail templates are stored in the JIRA_INSTALL/atlassian-jira/WEB-INF/classes/templates/email directory, and generally, for each event in Jira, there are three template files:

  • The subject template: This is the template file for the email's subject line, which is stored in the subject subdirectory.
  • The text template: This is the template file for emails sent in text format, which is stored in the text subdirectory.
  • The HTML template: This is the template file for emails sent in HTML format, which is stored in the html subdirectory.

To start creating our own email templates, we first need to create the three files mentioned in the previous list of template files and place them in their respective directories. Take special note that all three files need to have the same filename with a .vm extension.

We will start with the subject template, as follows:

  1. Create a new file called issueapproved.vm in the subject subdirectory with the following code snippet:
#disable_html_escaping()

$eventTypeName - ($issue.key) $issue.summary
  1. We now need to create the body of the email, keeping in mind that we have to create two versions—one for text and one for HTML. The following snippet shows the HTML version; for the text version, simply remove the HTML markups. Note that both HTML and text template files need to be the same—that is, issueapproved.vm:
#disable_html_escaping()

Hello $issue.reporterUser.displayName,

<p>
 Your request <a href="">$issue.key</a> has been approved, with the comment below:
</p>

<blockquote>
  <p>
#if($comment.body)
$comment.body
#else
<i>No comment</i>
#end
</p> </blockquote> <br/> Internal IT team
  1. After we have created all three template files, we need to register them in Jira so that they can be selected while creating custom events. To register new email templates, open the email-template-id-mappings.xml file in a text editor; you can find the file inside the JIRA_INSTALL/atlassian-jira/WEB-INF/classes directory.
  2. The email-template-id-mappings.xml file lists all the email templates in Jira, so we need to add a new entry at the end, as follows:
<templatemapping id="10002">

 <name>Issue Approved</name>

 <template>issueapproved.vm</template>

 <templatetype>issueevent</templatetype>

</templatemapping>

There are a few points to note here:

  • The id value of <templatemapping> needs to be unique.
  • You can give any value to the <name> element and it will be displayed in Jira. It is good practice to keep it consistent with standard Jira event convention.
  • The <template> element should be the name of the custom template files we have created. All three files need to have the same filename since we can only have one <template> element.
  • The <templatetype> element needs to have the value set to issueevent.

Once you have added the entry and saved the file, you will need to restart Jira for the changes to be applied.

Now that we have our custom email templates in place, we can create the custom event that will use our new templates. Go through the following steps to create a custom event in Jira:

  1. Navigate to AdministrationSystemEvents.
  2. Enter Issue Approved for the new event's name.
  1. Select the Issue Approved template we just created.
  2. Click on the Add button to create the new event:

Once you have created the event, it will be available in the notification schemes, and we will be able to select who will receive email notifications by configuring our notification schemes—as shown in the following screenshot. Whenever an issue is approved, both the reporter and the user Christine will be notified:

The last step is to make sure that our custom events are fired when users trigger the action:

  1. Navigate to AdministrationIssues > Workflows.
  1. Click on the Edit link for the workflow, which contains the transitions that will fire the custom event. In this case, we will be using a simple approval workflow that contains a transition called Approve:
  1. Click on the workflow transition, and select the Post Functions tab. Normally, you will see the last post function in the list firing Generic Event.
  2. Hover your mouse over the post function and click on the edit icon (it looks like a pencil).
  3. Select the new Issue Approved event and click on Update, as shown in the following screenshot. This will make the transition fire our event instead of the default Generic Event:

Let's see how the steps work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset