Send Emails
Send emails using HAPI, with examples for sending emails with attachments, images, and more.
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')
}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")}/>"')
}