Show Navigation

grails-sendgrid

Grails SendGrid Plugin

Owner: desirable-objects | 2.0.1 | Aug 27, 2016 | Package | Issues | Source | Documentation | License: Apache-2.0


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

            

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 @[email protected] controller must have the 'mail.send.json' action configured, and sending and receiving @application/[email protected] 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 '[email protected]'
     to '[email protected]'
     to '[email protected]'
     bcc '[email protected]'
     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('[email protected]')
                        .to('[email protected]')
                        .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/]