The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Send email for each issue of a JQL

G Sunil Kumar
August 4, 2021

I have an email template that I want to send for each issue of a JQL with in template using script runner JOBS functionality.

 

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

Query query = jqlQueryParser.parseQuery('project = "Risk Exceptions" AND status = Closed')
def results = searchService.search(scriptUser[0],query, PagerFilter.getUnlimitedFilter())

log.warn("Total issues: ${results.total}")

List<Issue> matchingIssues = []
results.getResults().each { documentIssue ->
def issue = issueManager.getIssueObject(documentIssue.id)
matchingIssues.add(issue)
}
log.warn("${matchingIssues}")




subject = "test"
emailAddr = "something@something.com"
body = "This issue " + issue.getkey().toString() + " will expire in 15 days".

def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
if (mailServer) {
Email email = new Email(emailAddr);
email.setMimeType("text/html");
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)


 

The above format sends an email but I want to send my template to each issue of the JQL.

 

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Vikrant Yadav
Community Champion
August 4, 2021

Hi @G Sunil Kumar  I suggest you to use Script Runner Jobs >> Escalation Services for this. 

Escaltion sends an email for each issues, like in below screenshot, it's showing 25 hits, system will send 25 emails means mail for each issue.

Here jobs is sending email to reporter, you can change it something else. 

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

def subject = "${issue.key} = ${issue.summary}"
def body = "test"
def emailAddr = issue.getReporter().getEmailAddress()

def sendEmail(String emailAddr, String subject, String body) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr);
email.setMimeType("text/html")
email.setSubject(subject);
email.setBody(body);
mailServer.send(email);
log.error("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}
sendEmail (emailAddr,subject, body)

Escaltion Services.PNG

G Sunil Kumar
August 4, 2021

@Vikrant Yadav ,

 

I will try and update the ticket.

 

Regards,

Sunil