Need help creating a script to send emails using ScriptRunner for Confluence.

Charles Huggins December 15, 2020

Good day,

I'm trying to create a working script to send a notification when a label is added to a page.

Here is script:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager

import com.atlassian.confluence.event.events.content.page.PageEvent

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
if (mailServer) {
Email email = new Email("mail@domain.com");
email.setSubject("test");
email.setBody("test");

mailServer.send(email);

} else {
throw new RuntimeException("no mail server!!!");
}


Tell me how to add a page and link, as well as the name of the label and the user who added the label to the email template.

Hope you help,

 

1 answer

1 accepted

0 votes
Answer accepted
Robert Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 15, 2020

Hi Charles, 

You can achieve this with the script below, can you try this out and let me know if it works for you?

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.event.events.label.LabelAddEvent


def labelEvent = event​
def label = labelEvent.getLabel()
def page = labelEvent.getLabelled() as Page

​def emailBody = """

Page: ${page.getTitle()}

Label: ${label}

"""

​log.warn emailBody

​def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)

SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

if (mailServer) {

Email email = new Email("mail@domain.com");

email.setSubject("test");

email.setBody(emailBody);



mailServer.send(email);



} else {

throw new RuntimeException("no mail server!!!");

}
Charles Huggins December 17, 2020

Hi Robert, 1st thank you for provided solution. 2nd i didn't found row where i can get user who added label according this event fired.

I have tried these methods to solve question mentioned from above, but it is not helped, perhaps you makes it clear.

def author = label.getOwner() as User
Author: ${author}

 

Current version of script:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.event.events.label.LabelAddEvent
import com.atlassian.user.User

def labelEvent = event
def label = labelEvent.getLabel()
def author = label.getOwner() as User
def page = labelEvent.getLabelled() as Page

def emailBody = """

Page: ${page.getTitle()}
Page link: https://baselink.domain.com/${page.getUrlPath()}

Label: ${label}
Author: ${author}
"""

log.warn emailBody

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)

SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

if (mailServer) {

Email email = new Email("mail@domain.com");

email.setSubject("test labels");

email.setBody(emailBody);

mailServer.send(email);




} else {

throw new RuntimeException("no mail server!!!");

}

 

 

Robert Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2020

For some reason the getOwner function does not seem to return anything. The below script gets the label that has been added for the event, and then loops through all the labels of the page and then finds the matching label, and its owner. 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.confluence.pages.Page

def log = Logger.getLogger(getClass())

log.setLevel(Level.DEBUG)
def labelEvent = event
def label = labelEvent.getLabel()
def page = labelEvent.getLabelled() as Page
def labellings = page.getLabellings()

labellings.each { labelling ->
if (label.getId() == labelling.getLabel().getId()){
log.debug("Owner: " + labelling.getOwningUser().getLowerName())
}
}


This should cover what you need 

Like Charles Huggins likes this
Charles Huggins December 22, 2020

Hi Robert, thank you so much for assistance! I'm also added this class "import com.atlassian.confluence.user.Confluence User Impl" and now it works.

The last version of my script:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.event.events.label.LabelAddEvent
import com.atlassian.user.User
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.user.ConfluenceUserImpl


def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def labelEvent = event
def label = labelEvent.getLabel()
def page = labelEvent.getLabelled() as Page
def labellings = page.getLabellings()

labellings.each { labelling ->
if (label.getId() == labelling.getLabel().getId()){
log.debug("Owner: " + labelling.getOwningUser().getFullName())
}
def author = labelling.getOwningUser().getFullName()




def emailBody = """

Label: ${label}
Label Author: ${author}

Page link: https://baselink.domain.com/${page.getUrlPath()}

Kind Regards,
SDV Confluence Development

"""

log.warn emailBody

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)

SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

if (mailServer) {

Email email = new Email("mail@domain.com");
email.setBcc("mail@domain.com")
email.setSubject("New label were added into ${page.getTitle()} page");

email.setBody(emailBody);

mailServer.send(email);




} else {

throw new RuntimeException("no mail server!!!");

}
}

Suggest an answer

Log in or Sign up to answer