Hi Team,
We have a requirement using email handlers.
In the email, we will receive information about the "Vendor Name" and the "Order No." The subject of this email is always "You have 1 new document(s)." When creating a ticket using the email handler, it doesn't make sense to have an unclear ticket summary. Therefore, we would like to copy the "Vendor Name" and the "Order No" from the email description to the ticket summary. Is it possible to achieve this using ScriptRunner?
You can see how the email body looks in the email in the attached screenshot for reference.
Hi @Lakshmi CH
Using ScriptRunner's Mail Handler, this is pretty simple. You will just need to filter the content of the message body.
Below is a sample working code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.mail.MailUtils
final def username = 'admin'
final def projectKey = 'MOCK'
final def issueTypeName = 'Bug'
def userManager = ComponentAccessor.userManager
def projectManager = ComponentAccessor.projectManager
def issueFactory = ComponentAccessor.issueFactory
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)
def user = userManager.getUserByName(username)
def project = projectManager.getProjectByCurrentKey(projectKey)
def issueReporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def messageBody = MailUtils.getBody(message)
if (messageBody.contains('Order No') && messageBody.contains('Vendor Name')) {
def subject = new StringBuilder()
messageBody.eachLine { line ->
if(line.contains('Vendor Name')){
subject.append(line).append(',\t')
} else if(line.contains('Order No')){
subject.append(line)
}
line
}
def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject.toString())
issueObject.setIssueTypeId(project.issueTypes.find { it.name == issueTypeName }.id)
issueObject.setReporter(issueReporter)
messageHandlerContext.createIssue(user, issueObject)
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you must modify the code accordingly.
Below are the screenshots of the Mail Handler configurations:-
Below is the sample email that is tested:-
And below is a sample of the output that is produced:-
I hope this helps to answer your question. :)
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
Thank you so much for your response and sample script to meet my requirements.
Can you please correct me if I am doing something wrong? This is my first script runner email handler.
1. Created a mail server with some email addresses and Impas (screenshot attached)
2. Using the same mail server, I set up an incoming email handler (screenshot attached)
3. The catch email address is where we will get the emails, and the same one I mentioned when I created the mail server.
4. I sent a few emails to that email address, and click on "Run Now" to test mail, it's not creating tickets, and showing the message (screenshot attached)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Lakshmi CH
The Run Now test works if you are doing an offline test, i.e. if the email you are testing is stored in the /media/atl/jira/share/import/mail folder in the *.eml or *.txt file format.
However, if you intend to trigger the test via your Outlook server, you need to try sending an actual email to the address configured for the mail handler.
You can still test it offline. To do so, export the email from your Outlook instance, save it as a *.eml file in the /media/atl/jira/share/import/mail folder, and click on the Run Now button to see if it can read the email.
I am looking forward to your feedback.
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 @Lakshmi CH
Has your question been answered?
If yes, please accept the answer.
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_ ,
Yes Ram, I sent a few emails, and the tickets have been created correctly. The email of the body formatted this way, but the ticket summary shows like this. Do you know how to correct this?
I want to display the summary like this "TXXXX Foods Inc, Order:4501263912"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Lakshmi CH
It's not extracting the Vendor's name because the vendor name is not on the same line as the Details:
Please clarify, are all the Vendor names below the Details? If yes, then there is a slight modification required in the code.
I am looking forward to your feedback and clarification.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.