Can Scriptrunner email handler - add incoming email as an attachment to an existing issue according the issue number in the subject
Hi @Noam Lugasi
I have a doubt to clarify i.e. are you trying to add the incoming email as an attachment to the issue?
If yes, there is an existing example in the Adaptavist Library.
I hope this helps to answer your question. :-)
I am looking forward to your feedback.
Thank you and Kind regards,
Ram
Thank you for the answer, the example shows how to create an issue from incoming mail.
we don't want to create issues from incoming emails.
the issue already exists, there is no need to create them.
we would like to be able to read the subject of the email (the issue number is in the subject) and attach that incoming mail to the issue that is in the subject of the incoming mail.
example, subject has the IMSUP-23453 issue number, we would like to add the incoming mail to that same issue as an attachment.
Thank you for getting back and providing your clarification.
For your requirement, you can try something like this:-
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.config.util.JiraHomeimport com.atlassian.jira.issue.attachment.CreateAttachmentParamsBeanimport com.atlassian.jira.service.services.file.FileServiceimport com.atlassian.mail.MailUtilsimport javax.mail.Messageimport javax.mail.Sessionimport javax.mail.internet.InternetAddressimport javax.mail.internet.MimeBodyPartimport javax.mail.internet.MimeMessageimport javax.mail.internet.MimeMultipart//This requires the name of the user that will trigger the Mail Handlerfinal def username = 'admin'//This requires the Project's Key that will be used for the Mail Handlerfinal def projectKey = 'MOCK'//This requires the temporary file name. The file name should end with the .eml extensionfinal def temporaryAttachmentFileName = 'tempMail.eml'//This requires the actual file name. The file name should end with the .eml extensionfinal def actualAttachmentFileName = 'SampleMail.eml'//This requires the hostname for IMAP / POP server that is being usedfinal def mailHost = 'imap.google.com'//This requires the Email Port that is being usedfinal def port = '993'def projectManager = ComponentAccessor.projectManagerdef issueManager = ComponentAccessor.issueManagerdef attachmentManager = ComponentAccessor.attachmentManagerdef jiraHome = ComponentAccessor.getComponent(JiraHome)def userManager = ComponentAccessor.userManagerdef user = userManager.getUserByName(username)def subject = message.subject as Stringdef from = message.from.join(',') as Stringdef to = message.allRecipients.join(',') as Stringdef messageBody = MailUtils.getBody(message) as Stringdef project = projectManager.getProjectByCurrentKey(projectKey)def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))def destination = new File(jiraHome.home, FileService.MAIL_DIR).absoluteFiledef file = new File("${destination}/${temporaryAttachmentFileName}")file.createNewFile()def out = new FileOutputStream(file)def emailContent = createMessage(from, to, subject, messageBody, mailHost, port)emailContent.writeTo(out)issues.each { if (subject.contains(it.key)) { def attachmentParams = new CreateAttachmentParamsBean.Builder("${destination}/${file.absoluteFile.name}" as File, actualAttachmentFileName, '', user, it).build() attachmentManager.createAttachment(attachmentParams) }}file.delete()static Message createMessage(String from, String to, String subject, String content, String mailHost, String port) { def properties = System.properties properties.setProperty('mail.smtp.host', mailHost) properties.setProperty('mail.smtp.port', port) def session = Session.getDefaultInstance(properties) def msg = new MimeMessage(session) def body = new MimeBodyPart() def multipart = new MimeMultipart() msg.setFrom(new InternetAddress(from)) msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)) msg.setSubject(subject) body.setText(content) multipart.addBodyPart(body) msg.setContent(multipart) msg}
Please note that the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
The function of the code is almost the same as the sample code available in the Adaptavist Library, except that instead of creating a new issue, it just converts the email into a *.eml file and adds it as an attachment to the existing issue.
Below is a screenshot of the output:-
I hope this helps to solve your question. :-)Thank you and Kind regards,Ram
Does the solution I have provided answer your question?If yes, please accept the answer.
Thank you and Kind regards,Ram
It looks like you're new here. Sign in or register to get started.