(Quick Reference)

3 Sending Email - Reference Documentation

Authors: Antony Jones

Version: 1.0

3 Sending Email

In a pinch, you can send email using the SendGridService in one of three ways:
  1. Using any controller's built-in sendMail method, and passing your email details to it.

> Note: This is the grails-mail way of doing things, and should always be your preferred method to allow for quick and easy interchange of any grails-supported mail plugin.

sendMail {
     from 'antony@example.com'
     to 'aiten@example.net'
     to 'wirah@example.org'
     bcc 'yourbcc@example.com'
     subject 'This is the subject line'
     body 'This is our message body'
}

2. Using the sendMail closure from any class which has a reference to the SendGridService.

As above, but referencing the sendMail method of the SendGridService directly:

sendGridService.sendMail {
    …
}

3. Using the email builder

This is useful when you might want a more programmatic approach to sending email.

SendGridEmail email = new SendGridEmailBuilder()
                        .from('antony@example.com')
                        .to('aiten@example.net')
                        .subject('This is the subject line')
                        .withText('This is our message body')
                        .build()

When you've built your email, pass it to the SendGridService's send method:

sendGridService.send(email)

The email builder is written as a natural-language type DSL, so you might find that there is more than one way to build your email, but under the covers, they are exactly the same.

4. Optional email parameters and advanced sending instructions

OptionBuilder SyntaxsendMail { } SyntaxParameter typesCan be called more than onceRequired
Recipient
.to('email@example.com')
to 'email@example.com'
StringYesYes
Recipient display name with email address
.to('Antony Jones', 'email@example.com')
or
.addRecipient('Antony Jones', 'email@example.com')
not implementedString, StringYesNo
Sender
.from('email@example.com')
from 'email@example.com'
StringNoYes
Sender display name with email address
.from('Antony Jones', 'emailexample.com')
not implemented@String, StringNoNo
Reply-to address
.replyTo('email@example.com')
replyTo 'email@example.com'
StringNoNo
Sent date
.sentDate(new Date())
sentDate new Date()
DateNoNo
Subject
.subject('Your subject line')
subject 'Your subject line'
StringNoYes
Blind Carbon-copy
.addBcc('email@example.com')
bcc 'email@example.com'
StringYesYes
Body text
.text('Your message text')
body 'Your message text'
StringNoYes (one of text or html)
Body html
.html('<h2>Your message html</h2>')
html '<h2>Your message html</h2>'
StringNoYes (one of text or html)
Custom handling instruction
.addCustomHandlingInstruction('headerName', 'headerValue')
addCustomHandlingInstruction 'headerName', 'headerValue'
String, StringYesNo
Header
.addHeader('headerName', 'headerValue')
addHeader 'headerName', 'headerValue'
String, StringYesNo
Attachments
.addAttachment(new File('/tmp/your.file'))
attach new File('/tmp/your.file')
FileYesNo

For further details, see the sendgrid api