Skip to end of banner
Go to start of banner

Mail Handler

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

On this page:


Starting with JJUPIN 4.0, we've added the ability to use SIL Scripts to process incomig emails. JJUPIN provides a set of specific routines for use withing mail handling scripts that allow you to read the contents of the incoming email and handle any attachments, as described below.

Specific SIL integrations

JJUPIN provides a set of specific routines for use withing mail handling scripts that allow you to read the contents of the incoming email and handle any attachments, as described below.

Predefined structure: IncomingEmail

NameFieldTypeObs

from

string []

array of email addresses

to

string []

array of email addresses

cc

string

array of email addresses

subject

string


body

string

this is the entire body of the message, including any quoted text in case of replied/forwarded messages

attachments

date

array containing the names of the attachments; there can be duplicates

Routines

  • Page:
    getIncomingEmail (Power Scripts™ for Jira 4.0) — Retrieves a structure containing information about the incoming email such as sender, recipients, subject, or body.
  • Page:
    attachFileFromEmail (Power Scripts™ for Jira 4.0) — Attaches a single file from the incoming email to a specified issue.
  • Page:
    attachAllFilesFromEmail (Power Scripts™ for Jira 4.0) — Attaches all attachments from the incoming email to a specific issue.
  • Page:
    saveAttachmentFromEmail (Power Scripts™ for Jira 4.0) — Saves the attachment file from the incoming email to a specific file path.

Writing the handler script

There are 2 important things to note about the mail handler script:

  1. If the first email address in the "from" field (the sender of the email) belongs to a registered JIRA user, the script will be run as that user (currentUser() will return that user). Otherwise, the script will be run as an anonymous user.
  2. The return value of the script dictates if other mail handlers will be called for the email or not. 
    1. If the script returns true or has no return value, the email is considered as "handled" and subsequent mail handlers will not be called.
    2. If the script returns false or throws an error, the email is considered "not handled" and subsequent mail handlers will be called.

Here is an example of mail handler equivalent to JIRA's built in "Create issue or add comment".

IncomingEmail mail = getIncomingEmail();

string issueKey = matchText(mail.subject, "[A-Z][A-Z]+-[0-9]+"); // find an issue key in the subject
if(isNull(issueKey)) {
	// if no issue key found, create a new issue
    string [] fields = {};
    fields += {"reporter", currentUserKey()};
    fields += {"assignee", getUserByEmail(mail.cc[0]).key}; 

    string newIssue = createIssue("TEST", "", "Task", mail.subject, "Minor", mail.body, {}, "", "", 0, fields);
    attachAllFilesFromEmail(newIssue);
    %newIssue%.watchers = getUserKeysFromEmails(mail.cc); 
} else {
	// if issue key found in subject, add a comment
    addComment(issueKey, currentUserKey(), mail.body);
}

You may find useful the getUserByEmail and getUserKeysFromEmails routines.


Adding the SIL Mail Handler

To add a new SIL Mail Handler, go to Administration → System → Incoming Mail and click Add incoming mail handler

Fill in the name, select the incoming server to use, select the SIL Mail Handler and click Next.

In the next screen, select the script to use for the handler and click Add.


To delete the mail handler, use the JIRA standard feature.


That's it!

  • No labels