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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Script Runner Mail Handler




i use a custom mailhandler with groovy script to add comments generated from email bodies. i use the sender address which is a customer of the service desk as author. But all comments are marked as internal. After some testing i found out that only users with agent rights can create public comments. If you are no agent, comments are always internal.

Here is some code


import com.atlassian.jira.project.UpdateProjectParameters
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.Issue
//import com.onresolve.jira.groovy.user.FormField
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.crowd.embedded.impl.ImmutableGroup
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.project.ProjectCategory
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.mail.MailUtils
import com.atlassian.jira.user.UserUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.util.json.JSONObject

// get log in user to proof that you can create user
def user = ComponentAccessor.getUserManager().getUserByName('admin')

// get Manager classes
def userService = ComponentAccessor.getComponent(UserService)
def userManager = ComponentAccessor.getComponent(UserManager)
def projectManager = ComponentAccessor.getProjectManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def commentManager = ComponentAccessor.getCommentManager()
def groupManager = ComponentAccessor.getGroupManager()

final SD_PUBLIC_COMMENT = "sd.public.comment"
//def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
//def properties = [(SD_PUBLIC_COMMENT): new JSONObject().put("internal", false).toString()]
def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false] as Map)]
// get email sender
//def reporter = MailUtils.getSenders(message)
def reporter = ""
final List<String> senders = MailUtils.getSenders(message);
for (final String emailAddress : senders)
reporter = emailAddress
//check if user is in Whitelist

// check if sender is already a known user
ApplicationUser ReporterFrom = messageUserProcessor.getAuthorFromSender(message)

Project project = projectManager.getProjectByCurrentKey("ACSD")

if(ReporterFrom == null){
//user unknown - create user

// create issue or comment
log.debug("create the issue")
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
def subject = message.getSubject() as String
def issue = ServiceUtils.findIssueObjectInString(subject)
if (issue) {
log.debug("Add comment to existing issue")
def comment = commentManager.create(issue, user, MailUtils.getBody(message), null, null, new Date(), properties, true)
else {

//ApplicationUser user = userManager.getUserByName("admin")
//ApplicationUser reporter = messageUserProcessor.getAuthorFromSender(message) ?: user

def issueObject = issueFactory.getIssue()
issueObject.setIssueTypeId(project.issueTypes.find { == "Incident" }.id)
issue = messageHandlerContext.createIssue(user, issueObject)

// Get Attachments out of the email
def attachments = MailUtils.getAttachments(message)

attachments.each { MailUtils.Attachment attachment ->
def destination = new File(jiraHome.home, FileService.MAIL_DIR).getCanonicalFile()
def file = FileUtils.getFile(destination, attachment.filename) as File
FileUtils.writeByteArrayToFile(file, attachment.contents)
messageHandlerContext.createAttachment(file, attachment.filename, attachment.contentType, user, issue)

return 'finished'


Did anybody runs into the same issue?


best regards 

2 answers

When you create the issue,  issue id need to be included in the issue subject. Because findIssueObjectsInString(String) use string tokenizing to search existing issue key. 

That will avoid new issue creation when commenting via email.

Hi @Andre Schmidt ,


def issue = ServiceUtils.findIssueObjectInString(subject)

 worked to find the existing issue as my mail handler is not able to get it and creating a new issue every time with the same summary?



Hi @Eshwar Palem ,

yes, the function scans the subject to find an existing issue ID. If so the issue object will be returned. So he checks the issue id, not the summary


best regards

Hi @Andre Schmidt


The method is returning null when I printed in the logs and it is creating duplicate issues when I am replying to the existing email thread.

Can you provide some suggestions on this?



Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events