Hi,
I'm trying to use the ScriptRunner to send email notification from the last comment of an issue including inline images by specifying their content IDs. I'm not sure how to include the following tag to each inline image automatically.
<img src="cid:${addAttachment("my-image.png")}/>
My main goal is to keep the last comment text as the body of the email and inject the above code for each inline image to show them in the body of the email.
I'm using the following script to call the last comment and only include new attachments from the comment.
Could you please clarify if you have an attachment that is a text file, how are you planning to embed it into the email?
Do you want to add it as an attachment? I am asking this so I can try to provide a solution.
I am looking forward to your feedback and clarification.
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_
It possible that end user will have other attachment types as in Docx or PDFs. Email body should only contain the comment and its inline images using the content ID of the email's attachments.
Thanks
Roshan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am currently working on an example code. But it requires a bit of time as your requirement is a bit complex.
I will add it here once I have finished testing it.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_
Thanks for the update. I'm looking forward to test your solution.
Regards
Roshan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have managed to come up with a working solution.
First, please ensure that you upgrade your ScriptRunner plugin to the latest release, i.e. 8.11.0, so you can use ScriptRunner's HAPI feature.
Below is the sample working code for your reference:-
import com.adaptavist.hapi.jira.mail.Mail
import com.atlassian.jira.issue.attachment.Attachment
def issue = event.issue
def latestComment = issue.comments.last()
def content = latestComment.body.split("\\r?\\n")
def result = content - content.collect { if (it.trim().contains('[^')) { it } }
def mailBodyContent = result.collect {
if (it.trim().startsWith('!')) {
"${it.replace('!', '').replace('|thumbnail', '')}".trim()
} else {
"${it}<br/>".trim()
}
}.toUnique()
def commentAttachmentNames = content.collect {
if (it.trim().startsWith('!')) {
it.trim().replace('!', '').replace('|thumbnail', '').trim()
} else if (it.trim().startsWith('[')) {
it.trim().replace('[^', '').replace(']', '').trim()
}
}.unique() as List<String>
commentAttachmentNames.removeAll([null])
def filteredAttachments = issue.attachments.collect {
if (commentAttachmentNames.contains(it.filename)) { it }
}.unique() as List<Attachment>
filteredAttachments.removeAll([null])
Mail.send {
setHtml()
setTo('user1@mail.com')
setFrom('user2@mail.com')
filteredAttachments.each {
if (mailBodyContent.toString().contains(it.filename) && (it.filename.endsWith('.png') || it.filename.endsWith('.jpg')) ) {
Collections.replaceAll(mailBodyContent, it.filename, "<img src=\"cid:${addAttachment(it)}\"/>")
} else {
addAttachment(it)
}
}
setBody(mailBodyContent.join(''))
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
For this code to work, you must use ScriptRunner's Custom Script Listener and the Issue Commented Event.
Below is a screenshot of the Listener configuration for your reference:-
Below are a couple of test screenshots for your reference:-
1. For testing, I created a comment with some text and included a PNG image file along with a text file in the comment body
2. Once the Listener is triggered and the email is sent as expected, the email body only contains the text and image content. However, if you look at the attachment section, both the image and the text file are included.
I hope this solves your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_
Thanks for the detailed instructions. Your solution is exactly what I was looking for.
Regards
Roshan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.