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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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!!!");

}

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!!!");

}

 

 

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

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
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you