Skip to content

desirable-objects/grails-sendgrid

Repository files navigation

grails-sendgrid

Slack Signup Travis CI

Introduction

The Grails SendGrid plugin allows you to use the services offered by SendGrid to send email from your application.

The plugin works for Grails 3.x. For Grails 2.x please refer to the 1.x branch

Installation

Add the following dependencies in build.gradle

dependencies {
...
    compile 'desirableobjects.grails.plugins:grails-sendgrid:2.0.1'
...
}

Configuration

Configuration takes place in your application's yaml file.

The basic configuration you will need to use the plugin is:

    sendgrid:
        username: 'your-username'
        password: 'your-password'

Where your-username and your-password should be replaced with your sendgrid login details.

If you need to override the sengrid API endpoint (such as for development/integration environments, to replace it with a fake 'fixture'), you can do that in the same place:

    sendgrid:
        api:
            url: 'http://localhost:8080/your-application/fixture/'
        username: 'your-username'
        password: 'your-password'

Note that your @fixture@ controller must have the 'mail.send.json' action configured, and sending and receiving @application/json@ content, as this is what the plugin expects to call.

Sending Email

In a pinch, you can send email using the SendGridService in one of two ways:

  • Using the sendMail method of the SendGridService directly:
sendGridServicesendMail {
     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'
}
  • 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.

For further details, see the sendgrid api [http://docs.sendgrid.com/documentation/api/web-api/mail/]