0.3.2 • Published 6 years ago

gcp-function-mailer v0.3.2

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

GCP Function Mailer

Sends an email using the Sendgrid API using a Google Cloud Platform (GCP) Function. The data and trigger for the email is a GCP Pubsub message. It gets formatted to HTML and/or Text using a given mustache template.

See the example under the example directory for usage.

Quickstart

mkdir your-mailer-directory
cd your-mailer-directory
npm init .
npm install gcp-function-mailer --save
cp config.json config.production.json
touch hello.text.mustache
touch hello.html.mustache

Edit the config.production.json file to include the Sendgrid API key you want to use in production. Then edit the two mustache templates. The first line of the template should be the subject as a string.

To deploy to Google Cloud, set up your gcloud environment and run:

gcloud beta functions deploy mailer --trigger-topic=mailer

You can trigger the function in either the Google Cloud Console or via the command line:

gcloud beta functions call mailer --data '{"template":"hello", "to":"jon@example.org"}'

Configuration

Configuration data is taken from a number of sources. The code will attempt to load the local config.json and config.NODE_ENV.json files. It will also load package.json and look for the "mailer key". These values are combined with any configuration supplied programattically. Generally all you will need is a config.json for local development and a production version.

Don't check in your production version. Right now there isn't an easy mechanism for secrets configuration for Google Cloud Functions. However, this is in progress with Google.

The configuraton must contain a sendgridApiKey, unless it can be sourced from the SENDGRID_API_KEY environment variable.

The configuration can contain: defaultCc, defaultBcc, defaultFrom, and defaultReplyTo values.

Invoking

To send an email, publish a message to the mailer pubsub topic.

Each invocation must have a to and template value. It can optionally contain cc, bcc, from and replyTo values.

The whole message is supplied to the Mustache template for rendering.

Templates

The code assumes the first line of the template is the subject line for the email. If you have both HTML and Text templates, the Text subject line takes precidence. So you can leave the HTML one blank if that suits - but the line must still be present.

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago