本文转自:https://library.adaptavist.com/entity/send-custom-email
This snippet shows you how to send a custom email. You can use the method inside in any script designed to send an email.
I want to send an email when an issue transitions from In progress to Done to update the customer on their issue status. I can use this snippet inside a post-function script.
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.mail.Email import com.atlassian.jira.mail.settings.MailSettings import com.atlassian.mail.MailException import com.atlassian.mail.server.SMTPMailServer import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil import org.apache.log4j.Level import org.apache.log4j.Logger String sendEmail(String emailAddr, String subject, String body) { def logger = Logger.getLogger(getClass()) logger.setLevel(Level.DEBUG) // Stop emails being sent if the outgoing mail server gets disabled (useful if you start a script sending emails and need to stop it) def mailSettings = ComponentAccessor.getComponent(MailSettings) if (mailSettings?.send()?.disabled) { return 'Your outgoing mail server has been disabled' } def mailServer = ComponentAccessor.mailServerManager.defaultSMTPMailServer if (!mailServer) { logger.debug('Your mail server Object is Null, make sure to set the SMTP Mail Server Settings Correctly on your Server') return 'Failed to Send Mail. No SMTP Mail Server Defined' } def email = new Email(emailAddr) email.setMimeType('text/html') email.setSubject(subject) email.setBody(body) try { // This is needed to avoid the exception about IMAPProvider ContextClassLoaderSwitchingUtil.runInContext(SMTPMailServer.classLoader) { mailServer.send(email) } logger.debug('Mail sent') 'Success' } catch (MailException e) { logger.debug("Send mail failed with error: ${e.message}") 'Failed to Send Mail, Check Logs for error' } } sendEmail('user@email.com', '<Your subject here>', '<Your body here>')
https://library.adaptavist.com/entity/notify-approvers-of-pending-approvals-via-email
This escalation service is used to trigger an email that notifies approvers of pending issues, once their email has been retrieved using the REST Endpoint.
In this example, a reminder email is triggered, after the email addresses of an issue’s outstanding approvers were obtained via a REST Endpoint.
def email = new Email(emailAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType('text/html')
mailServer.send(email)
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.mail.Email import groovy.json.JsonSlurper import groovy.xml.MarkupBuilder def key = issue.key def applicationProperties = ComponentAccessor.applicationProperties def mailServerManager = ComponentAccessor.mailServerManager def mailServer = mailServerManager.defaultSMTPMailServer final def host = applicationProperties.getString('jira.baseurl') final def restEndpointName = 'getApprovers' def baseUrl = "${host}/rest/scriptrunner/latest/custom/${restEndpointName}?issueKey=${key}" def response = baseUrl.toURL().text def json = new JsonSlurper().parseText(response) def artifacts = json.collect() def emailBody = new StringWriter() def html = new MarkupBuilder(emailBody) def mkp = html.mkp html.html { body { p { mkp.yield 'Awaiting your Approval for this Issue' a(href: "${host}/browse/${key}", key.toString()) } } } artifacts.findAll { def emailAddress = it.toString() def subject = 'Approval Reminder' def body = emailBody.toString() def email = new Email(emailAddress) email.setSubject(subject) email.setBody(body) email.setMimeType('text/html') def threadClassLoader = Thread.currentThread().contextClassLoader Thread.currentThread().contextClassLoader = mailServer.class.classLoader mailServer.send(email) Thread.currentThread().contextClassLoader = threadClassLoader }
https://library.adaptavist.com/entity/send-an-issues-change-history-to-watchers
Send an email to all watchers of an issue with the change history items in the last X days. Edit the number of days to go back by changing the lastDays
variable. Modify the script to set the jiraIssue
variable depending on where you add the script (for example, in a post function use issue.key
instead of TEST-1
).
As a project manager, I want to keep track of all changes to my watched issues. I need to be up to date with who is working on an issue and what has been changed. I do not want to check each watched issue manually. I can use this script to send me an email with change history items weekly to keep informed.
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.mail.Email import groovy.xml.MarkupBuilder import java.time.LocalDate import java.time.format.DateTimeFormatter final jiraIssue = ComponentAccessor.issueManager.getIssueByCurrentKey("TEST-1") final lastDays = 2 static sendEmail(String emailAddr, String subject, String body) { def mailServer = ComponentAccessor.mailServerManager .defaultSMTPMailServer if (mailServer) { def email = new Email(emailAddr) email.setMimeType("text/html") email.setSubject(subject) email.setBody(body) mailServer.send(email) } } def writer = new StringWriter() def builder = new MarkupBuilder(writer) builder.html { head { h1("Hi,") } body { p("I have some changes you might have missed.") def changeHistory = ComponentAccessor.changeHistoryManager.getChangeHistories(jiraIssue) p("Here is what changed:") changeHistory.each { def now = LocalDate.now() def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.[SSS][SS][S]") def dateToParse = it.timePerformed as String def parsedDate = LocalDate.parse(dateToParse, formatter) if (parsedDate < now.minusDays(lastDays)) { return } def change it.changeItems.each { change = "${it.field} changed from ${it.oldstring ?: 'nothing'} to ${it.newstring ?: 'nothing'}\n\t" } p("Date: ${parsedDate} | Author: ${it.authorDisplayName} | ${change}") } } } def watchers = ComponentAccessor.watcherManager.getWatchersUnsorted(jiraIssue) watchers.each { sendEmail(it.emailAddress, "Changes", writer.toString()) }