Email notification with Last file attachment for issue

Trying to send email notification with latest attached file for the issue.   I am unable to find the options to send email with attachment. I am able to find the attachment path and filename but finding the way to add latest file added for issue and send email with that file as an attachment. Any help much appreciate.

 

package com.custom
import com.atlassian.jira.event.issue.AbstractIssueEventListener 
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.ComponentManager 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager 
import com.atlassian.jira.issue.fields.CustomField 
import com.atlassian.jira.issue.Issue 
import com.atlassian.jira.issue.MutableIssue 
import com.atlassian.jira.issue.AttachmentManager; 
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.mail.Email 
import com.atlassian.mail.server.MailServerManager 
import com.atlassian.mail.server.SMTPMailServer import org.apache.log4j.Category import java.sql.Timestamp
import com.atlassian.jira.util.AttachmentUtils; 
import com.atlassian.jira.util.PathUtils
//////////////////////////////////////////////////////////////////////////////////////// //Used for testing on specific issue 
def IssueKey = "TEST-58"
ComponentManager componentManager = ComponentManager.getInstance() 
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager() 
def issue = componentManager.getIssueManager().getIssueObject(IssueKey) 
////////////////////////////////////////////////////////////////////////////////////////
// Logger 
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
// Set the logging level to DEBUG 
log.setLevel(org.apache.log4j.Level.DEBUG); 
log.debug ("Event 400 : Sending email on ${issue.getStatusObject().name}")
MailServerManager mailServerManager = componentManager.getMailServerManager() 
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer() 
AttachmentManager attachmentManager = componentManager.getAttachmentManager() 
pathManager = componentManager.getAttachmentPathManager()
 
if (mailServer) {    
 //TO: Email Address//       
 toname = "emailID@companymail.com"       
Email email = new Email(toname)         
///////Email Subject////////    
 email.setSubject("($issue.key) - $issue.summary") 
///////Email Body////////   
 String content          
 List attachments = attachmentManager.getAttachments(issue); 

if (!attachments.isEmpty()) 
{ attachmentManager.getAttachments(issue).each { attachment ->  
    
 filepath = AttachmentUtils.getAttachmentFile(attachment);   
  attachmentFilename = attachment.getFilename() //   
atFile = new File(filename)    
      
  }     
}
   content = "Testing attachment ${issue.key} \n" +       
         "${filepath} - attached file - ${attachmentFilename}"   
  email.setBody(content)    
 ///////SEND Email ////////     
mailServer.send(email)
}else {    log.error "No SMTP mail server defined" }
  filepath.toString() +  " Filename =  " + attachmentFilename.toString()

2 answers

1 accepted

This widget could not be displayed.

Does any one have idea on  attaching file to an email using groovy script?

This widget could not be displayed.

Below code will get the latest file and send an attachment. Its worked for me.

////Checking for attachment.
       Multipart multipart = new MimeMultipart("mixed");
        BodyPart attachBody = new MimeBodyPart();
    
    List<Attachment> attachments = attachmentManager.getAttachments(issue);
    NumOfAttachedfiles = attachments.size();
     
    if(!attachments.isEmpty()) {
        def fileCreatedDate, fileCreatedDateFinal;
        Attachment attachmentFinal;
        
        for (int i=0; i< NumOfAttachedfiles; i++) {
            Attachment attachment = attachments[i];
            fileCreatedDate = attachment.getCreated();
            if(i==0){
                fileCreatedDateFinal = fileCreatedDate;
         
                attachedFile = AttachmentUtils.getAttachmentFile(attachment);
                attachedFileName = attachment.getFilename();
            }else{
         if(fileCreatedDate.after(fileCreatedDateFinal)){
                    fileCreatedDateFinal = fileCreatedDate;
                   // attachmentFinal = attachment;
                    attachedFile = AttachmentUtils.getAttachmentFile(attachment);
                    attachedFileName = attachment.getFilename();
                }
         }//else
            
         } //for
      
        FileDataSource source = new FileDataSource(attachedFile);
        attachBody.setDataHandler(new DataHandler(source));
        attachBody.setFileName(attachedFileName);
        multipart.addBodyPart(attachBody);
        email.setMultipart(multipart);

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

215 views 3 0
Join discussion

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