Issue with Sending Emails via Post Function in Jira Script Runner

I’m trying to implement an email notification system in Jira using Script Runner. I want to automatically send emails to external users when their emails result in new tickets in the system. The email addresses are included in the ticket descriptions.

Successful Script in Console

This version of the script works well when executed in the Script Runner console:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer

ComponentManager manager = ComponentManager.getInstance()
MailServerManager mailManager = manager.getMailServerManager()
SMTPMailServer smtpServer = mailManager.getDefaultSMTPMailServer()

if (smtpServer) {
    IssueManager issueManager = manager.getIssueManager()
    Issue issue = issueManager.getIssueObject("TASK-456")
    
    def getEmail = {
        (((it.split("\\[Email from:")[1]).split("<")[1]).split(">")[0])
    }
    String senderEmail = getEmail("${issue.description}")
    
    Email email = new Email(senderEmail)
    email.setSubject("New Ticket Created: ${issue.summary}")
    String bodyContent = "We have received your request: ${issue.description}"
    email.setBody(bodyContent)
    smtpServer.send(email)
}

Failing Version in Post Function

However, when I try to use it as a post function on the CREATE transition, it doesn’t work:

import com.atlassian.jira.ComponentManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer

ComponentManager manager = ComponentManager.getInstance()
MailServerManager mailManager = manager.getMailServerManager()
SMTPMailServer smtpServer = mailManager.getDefaultSMTPMailServer()

if (smtpServer) {
    def getEmail = {
        (((it.split("\\[Email from:")[1]).split("<")[1]).split(">")[0])
    }
    String senderEmail = getEmail("${issue.description}")
    
    Email email = new Email(senderEmail)
    email.setSubject("New Ticket Created: ${issue.summary}")
    String bodyContent = "We have received your request: ${issue.description}"
    email.setBody(bodyContent)
    smtpServer.send(email)
}

I thought that the issue object would be accessible automatically in post functions, similar to examples I’ve read. Can anyone help me understand what I might be missing? I would also appreciate any debugging suggestions.

you’re missing imports in your post function. add import com.atlassian.jira.component.ComponentAccessor and use ComponentAccessor.getComponent() - ComponentManager’s deprecated. also add error handling since post functions fail silently and debugging becomes a pain.

This happens because post functions run in a different context than console scripts. The issue object might not exist yet or be fully saved when your function runs. I’ve hit this before. Add a null check for the issue object and try using getIssueObject(issue.getId()) to refresh the state. Wrap your email extraction in a try-catch block too - if your regex doesn’t match the description format, the script fails silently. One more thing: CREATE transitions sometimes fire before custom fields get populated. Consider adding a small delay or moving this to a different transition if the email parsing keeps breaking.

You’re using the old ComponentManager approach in your post function. Modern Script Runner versions use ComponentAccessor instead. Also, the issue object isn’t always available - depends on your post function context.

Here’s what works:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer

SMTPMailServer smtpServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (smtpServer && issue) {
    try {
        def getEmail = {
            (((it.split("\\[Email from:")[1]).split("<")[1]).split(">")[0])
        }
        String senderEmail = getEmail("${issue.description}")
        
        Email email = new Email(senderEmail)
        email.setSubject("New Ticket Created: ${issue.summary}")
        email.setBody("We have received your request: ${issue.description}")
        smtpServer.send(email)
    } catch (Exception e) {
        log.error("Email sending failed: ${e.message}")
    }
}

Main changes: switched to ComponentAccessor and added error handling to catch runtime issues.