Email

This section applies to Rebar only.

Introduction

The unit for sending e-mail is based on the service provided by Send Grid. It takes care of sending email and error handling.

Configuration

Settings

In /configuration/units/rb-email/server/settings.js populate the EmailRecipientSubstitution setting with rules for replacement of an email recipient. This setting is useful in a development environment, where during testing one would like all emails to be sent to only one E-Mail address instead of E-Mail addresses of real users.

In the example below, for development environment (as specified by NODE_ENV), we would send all emails to tester@pacificgadget.com:

export const EmailRecipientSubstitution = {
  'development': [
    { match: /[\s\S]*/, replacement: 'tester@pacificgadget.com' }
  ]
}

Account setup

In your account in Send Grid, create a new API key at https://app.sendgrid.com/settings/api_keys. Configure the API key to send email:

SendGrid API creation

Your key will be displayed once the API key is generated:

SendGrid API key

Notice that once you close the page you would not be able to retrieve the key again. Make sure to record it properly.

In .env specify:

EMAIL_SENDGRID_API_KEY=<value of the App key above>

Usage

In order to send HTML mail you would need a template file:

<!-- SendEmail.ejs -->

<html>
<body>
  Hello <%- userDisplayName %>.
  <br/>
  The World is ready to see you!
</body>
</html>

In order to send an email based on the template, use:

// SendEmail.js

// ...
import EmailSender from '../../../units/rb-email/server/emailSender'
// ...

EmailSender.sendTemplatedHTMLEmail(
  'RecipientOfEmail@Recipient.com',    // To
  'SenderOfEmail@Sender.com',          // From
  'hello world',                       // Subject
  '/path/to/SendEmail.ejs',            // Path to email template
  { userDisplayName: 'John Doe' }      // Template parameters
)

In the example above the template has one parameter, userDisplayName, which is passed to it. If the template can not be parsed and processed, an error will be written into the log and no email will be sent.