I'm trying to send a Scriptrunner Listener custom email based on a certain comment.
Asked by Karl Samson:

I want to setup a ScriptRunner Listener that sends a custom email to certain people when '@ Data Export Control' is added in a Comment.

I have the email subject and template setup ok, but the 'Condition and Configuration' I can't get to work.

I currently have:-

"import com.atlassian.jira.event.comment.CommentEvent  

CommentEvent == ("@Data Export Control")"  
I know there must be more to the code than this, and I can't seem to find anything online, can anybody help me with this please?  
NB - I know how to do it using Jira Automation, but we have too many Automations already, degrading our system performance! Thanks.


Answers:
author: Eugenio Onofre
body: Hello [@Karl Samson](/t5/user/viewprofilepage/user-id/3611064)

Here's a simplified version of what the script might look like:

*** ** * ** ***

import com.atlassian.jira.component.ComponentAccessor  
import com.atlassian.jira.event.issue.IssueEvent  
import com.atlassian.jira.event.comment.CommentEvent  
import com.atlassian.jira.issue.comments.CommentManager  
import com.atlassian.jira.mail.Email  
import com.atlassian.mail.server.SMTPMailServer

// Your ScriptRunner listener should listen for CommentEvent  
if (event instanceof CommentEvent) {  
def commentEvent = event as CommentEvent  
def comment = commentEvent.getComment()  
def commentBody = comment.getBody()

// Check if the comment contains the specific keyword  
if (commentBody.contains("@Data Export Control")) {  
def issue = comment.getIssue()  
def recipient = "email@example.com" // Define the recipient's email address  
def subject = "Your Email Subject"  
def body = "Your Email Template"

// Sending email  
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()  
if (mailServer) {  
Email email = new Email(recipient)  
email.setSubject(subject)  
email.setBody(body)  
mailServer.send(email)  
} else {  
log.warn("No SMTP Mail Server configured in JIRA.")  
}  
}  
}

*** ** * ** ***

A few things to note:

* Make sure to replace `"email@example.com"`, `"Your Email Subject"`, and `"Your Email Template"` with the actual email address, subject, and body template you want to use.
* This script assumes you have an SMTP server set up in Jira for sending emails.
* The `contains` method is case-sensitive. You may want to use `toLowerCase()` or a similar method for a case-insensitive match.
* Remember to test this script in a controlled environment before deploying it in your production environment to ensure it works as expected and does not have unintended side effects.

If this answer has resolved your issue or helped you in any way, kindly consider accepting it by clicking on the "Accept" button. This helps other community members find similar solutions more efficiently in the future.

comments: author: Karl Samson
body: Hey Eugenio, that's great! More than I was expecting in terms of an answer, thank you very much!

I'll need to modify it slightly in order for it to work in context. But will publish the final working solution.


author: Karl Samson
body: Hi Eugenio, so I'm wanting to adapt the script for a Custom Email Listener. But the script won't compile? Can you suggest why please?

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.issue.IssueEvent

import com.atlassian.jira.event.comment.CommentEvent

import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.mail.Email

import com.atlassian.mail.server.SMTPMailServer

// Your ScriptRunner listener should listen for CommentEvent

if (event instanceof CommentEvent) {

def commentEvent = event as CommentEvent

def comment = commentEvent.getComment()

def commentBody = comment.getBody()

// Check if the comment contains the specific keyword

if (commentBody.contains("@Data Export Control"))

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script6.groovy: 15: Unexpected input: '{\\r\\ndef commentEvent = event as CommentEvent\\r\\ndef comment = commentEvent.getComment()\\r\\ndef commentBody = comment.getBody()\\r\\n\\r\\n// Check if the comment contains the specific keyword\\r\\nif (commentBody.contains("@F-35 Data Export Control"))' @ line 15, column 55.

("@Data Export Control"))

**Email Template:-** \^

Notification of Task ${issue.key}

${issue.key} $issue.summary

Description: $issue.description

<https://jira.usp.greenlnk.net/browse/${issue.key>}

Regards,

${issue.reporter?.displayName}

**Email Subject:-**

Notification of Task ${issue.key}


Tags: jira-service-desk-server

jira-service-management

jira-service-management-server