Send email with ScriptRunner

Hello

We would like to send a reminder to only the reporters of issues with status "Under Review" which haven't been updated for a week.

I then used the built-in script escalation service and tried to insert the following example I've found here on Answers, also applied the edits from the comments of said topic (topic) and other stuff.

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

subject = "test"
body = "test"
emailAddr = issue.getReporter().getEmailAddress()
 
def sendEmail(emailAddr, subject, body) {
  SMTPMailServer mailServer = ComponentManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
  if (mailServer) {
    Email email = new Email(emailAddr);
    email.setSubject(subject);
    email.setBody(body);
    mailServer.send(email);
  } else {
    // Problem getting the mail server from JIRA configuration, log this error
  }
}

sendEmail (emailAddr, subject, body)

However, ScriptRunner indicates that getMailServerManager can't be found and multiple other errors. When I checked the API of our current JIRA version, the desired class is actually not available anymore it seems or I just can't find it.

screenshot1.png

I then created my own version like so.

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.functest.framework.admin;

def subject = "test"
def body = "test"
def emailAddr = issue.getReporter().getEmailAddress()
 
def sendEmail(emailAddr, subject, body)
{
    SMTPMailServer mailServer = admin.MailServerAdministration.MailServerConfiguration.get()
    
    if (mailServer) 
    {
        Email email = new Email(emailAddr);
        email.setSubject(subject);
        email.setBody(body);
        mailServer.send(email);
	}
    
    else
    {
        // Problem getting the mail server from JIRA configuration, log this error
    }
}
sendEmail (emailAddr, subject, body)

But this indicates that the class com.atlassian.jira.functest.framework.admin can't be resolved which I've found in the API documentation for JIRA 7.1.4

screenshot2.png

Can somebody help me here?

Thanks

1 answer

1 accepted

5 votes
Accepted answer

Hi there,

You said you are in a JIRA v7.* , so the ComponentManager is deprecated and you should use the ComponentAccessor instead. therefore in the first script you posted you will be able to get the mailServerManager (and then the SMTPServer) like 

import com.atlassian.jira.component.ComponentAccessor


def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

regards

thanos

Hi Thanos

Thanks. This works so far.

But now it says it can't find a matching method for

new Email(emailAddr);

I just checked the API again and this class and method are available in 7.1.4 and the syntax seems correct.

Any ideas?

Thanks for your help.

The 'errors' you see in the script runner are because of the Static Type Checking, in a dynamically-typed language. Therefore some times is safe to ignore them. So if you declare the type of your params 

def sendEmail(String emailAddr, String subject, String body) { ... }

you will see the difference.

regards

Thanos

Ah, I see. It works now! Thanks a lot for your help and the helpful link.

Hi @Thanos Batagiannis [Adaptavist] ,

I try using this code, without success:

Got errors:

2018-10-09 08:22:53,337 WARN [runner.ScriptRunnerImpl]: Please make sure that a valid mailServer is configured
2018-10-09 08:22:53,337 WARN [runner.ScriptRunnerImpl]: java.lang.NullPointerException: Cannot invoke method send() on null object

My code:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import com.atlassian.jira.issue.issuetype.IssueType
import javax.ws.rs.core.Response
import javax.ws.rs.core.MultivaluedMap
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequest;

try {

sendEmail('daniel_mor@bmc.com','Mail','Hi');


}
catch (e) {
}

 

// Create an email
def sendEmail(String emailAddr, String subject, String body) {
//MailServerManager mailServerManager = ComponentAccessor.getMailServerManager();
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();

//if (mailServer) {
try{
Email email = new Email(emailAddr)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.debug("Mail sent")
} catch(Exception e) {
log.warn("Please make sure that a valid mailServer is configured")
log.warn e;
}
}

Hi Daniel,

Well the logs give you a hint

Please make sure that a valid mailServer is configured

Configuring an SMTP mail server

Regards, Thanos

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

412 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you