Send custom email when Reporter @mention someone in Comment on Issue Edit Screen
Asked by Digvijay Singh Gehlot:

Hi Community,

I want to send a custom email when the Reporter mentions @ someone in Comment section on the Issue Edit Screen.

I don't want to send the JIRA Email notification to mentioned user here, I want to send a custom email with content like this:

"Hi \<mentioned-user\>,

Reporter \<issue.reporter\> has mentioned you on this issue \<issue.summary\>, \<issue.key\> for further assistance. Please find the below comment:

\<issue.comment.last.body\>"

How can I configure the above by using scriptrunner, JMWE app, or JIRA automation?

And please guide if there is any possibility to make the required changes in Notification scheme accordingly.

Thanks


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

What you are trying to do is not achievable via Behaviour. You must use the Listener instead.

For your requirement, it would be best to use the Custom Script Listener and set the event to Issue Commented so it will trigger only if a comment is added

Below is a sample working code for your reference, which has been extracted from this [Adaptavist code snippet](https://bitbucket.org/Adaptavist/workspace/snippets/aq64jj/automatically-send-email-to-users):-

```
/*
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party."
 */

import com.adaptavist.hapi.jira.mail.Mail
import com.adaptavist.hapi.jira.users.Users

def issue = event.issue

def latestComment = issue.comments.last().body

if (latestComment.contains('~')) {

      def usernames = latestComment.split(' ').collect {
            if (it.contains('[') && it.contains(']') && it.contains('~')) {
                  it.replace('[','').replace(']','').replace('~','').trim().replace(',','').trim()
            }
      } - [null]

      def displayNames = usernames.collect { filtered ->
            Users.getByName(filtered).displayName
      }

      def emails = usernames.collect { filtered ->
            Users.getByName(filtered).emailAddress
      }

      def body = """
      Hi ${displayNames.join(',')},
     
      Reporter ${issue.reporter} has mentioned you on this issue ${issue.summary}, ${issue.key} for further assistance. Please find the below comment:
      
      ${latestComment}
      """

      Mail.send {
            setFrom('mail@mailbox.com')
            setSubject('Auto Notification')
            setTo(emails.join(','))
            setBody(body.toString())
            setHtml()
      }
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

For the code above to work, you must use [ScriptRunner's HAPI](https://docs.adaptavist.com/sr4js/latest/hapi) feature.

The code extracts all the Usernames mentioned in the comment, identifies the respective user's name and email address, and adds them to the email body and the email recipient's address accordingly.

Note: Although you can invoke the users mentioned in the comment by using the @ symbol when the comment is actually extracted, it uses the \[\~\<username\>\] format to identify the user. Hence, when the email is sent, the comment body added to the email template will contain, for example, Hi \[\~jake\] / \[\~will\].

Below is a screenshot of the Listener Configuration:-

![listener_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/338316iF7261497049F57BC/image-size/large?v=v2&px=999 "listener_config.png")

I hope this helps to solve your question. :-)

Thank you and Kind regards,  
Ram

comments: author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

Has your question been answered?  

If yes, please accept the answer.

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

Thank you so much for your detailed explanation, and my apologies, I didn't get back to you.

After going through your message, I am able to understand on how I can achieve my requirements using listeners, and will let you know if I still face any issues after configuring the same at my end.

Best,

Digvijay


author: C_ Derek Fields
body: You may be better off updating the Velocity template than trying to implement this using an external script. I don't recall an "@mention" event that you can intercept, though there may be one deep in the bowels of Jira. There is a MentionEvent that can be raised, but you probably need an app to catch it.

comments: author: Digvijay Singh Gehlot
body: Hi [@C_ Derek Fields](/t5/user/viewprofilepage/user-id/41090)

Thank you for your message.

May you guide me on how to use Velocity template in JIRA Data center? Any help documents will be helpful.

Regarding MentionEvent, I am able to see that in Scriptrunner Listeners and on selecting "Send a custom email (non-issue events)", I can select MentionIssueCommentEvent.

So, could you help me, what will be the correct Condition and Configuration (groovy code) to call for the Mentioned User present in Comment section when added on Issue Edit screen?

I am also able to find this below document for reference: <https://docs.atlassian.com/software/jira/docs/api/7.2.1/com/atlassian/jira/event/issue/MentionIssueCommentEvent.html>

May you guide me further with the code or the correct method to check if a mentioned user is present in comment and then send the custom email to that user on Issue Update? It would be a great help.

Looking forward to hearing from you soon.


author: C_ Derek Fields
body: I hadn't looked closely enough and you are correct - ScriptRunner will let you catch the MentionIssueCommentEvent. This will trigger when someone is @mentioned in the comment (as opposed to being mentioned in the Description or in a custom field.

You can get the comment from the event by calling getComment() on the event. You can get the list of mentioned users by calling getRecipients().

I suggest that you look at [Send a Custom Email (adaptavist.com)](https://docs.adaptavist.com/sr4js/latest/features/workflows/post-functions/send-a-custom-email) for detailed information on how to set up the custom email, including how to set the "To" so that you can use the list of recipients in place of the static "To" field.


Tags: automation

data-center

jira-data-center

jmwe-app

mention

scriptrunner

user-group