Send Emails

A common requirement when writing automations is the ability to mail people. HAPI provides you with a very simple method of sending mail.

Prerequisite

You must configure an SMTP mail server to send mail.

Sending mail

When sending mail, you need to specify at least one recipient (a To, Cc and/or Bcc), the subject and body text. The following is an example of the minimum requirements for sending mail:

        Mail.send {
            setTo('foo@example.com', 'bar@example.com')
            setSubject('this is the subject')
            setBody('this is the body')
        }

When mail is sent, it's added to the Jira mail queue first, and then sent within one minute.

Setting who the mail is from

To set the "from" address, you can use setFrom(). Additionally, you can use setFromName() to set the name the mail client will display. For example:

        Mail.send {
            setTo('foo@example.com', 'bar@example.com')
            setFrom('from@example.com')
            setFromName('Your Boss')
            setSubject('this is the subject')
            setBody('this is the body')
        }

Sending HTML mail

To send HTML mail, you can use setHtml(). For example:  

        Mail.send {
            setTo('foo@example.com')
            setSubject('my subject')
            setHtml()
            setBody('This is <b>html</b>.')
        }

Sending attachments

You can add attachments to mail using HAPI. You can call addAttachment multiple times as required. For example:

        Mail.send {
            setTo('foo@example.com')
            setSubject('this is the subject')
            setBody('this is the body')
            addAttachment('my-file.xls')
        }

Including inline images

To include inline images in an HTML mail, you must specify the content ID of the attachment as the image source. The addAttachment() method will return the content ID, for example:

        Mail.send {
            setTo('foo@example.com')
            setSubject('this is the subject')
            setHtml()
            setBody('embedded image <img src="cid:${addAttachment("my-image.png")}/>"')
        }



Related content

On this page