A simple REST API for reliable email sending and logging.
- Validates and cleans input data from POST request
- Initially uses SendGrid to send emails. If SendGrid fails, then falls back to PostMark
- Automatic retries (customisable) with exponential backoff
- Can handle Sendgrid and Postmark open/click webhooks
- Can be extended to keep a record of all emails passing through, all status logs, and all open/click events from emails. Requires the implementation of a datastore (See datastore.js)
-
Set up your
.env
variables (renameenv
file to.env
)- Get API keys for SendGrid and PostMark by creating free accounts.
- If you have your own domain, then set up the relevant anti-spam settings (e.g. DKIM) to ensure your emails are received in the user's inbox
- Alternatively, the receiver may need to check their spam folders for your emails
- Create a
KEY_AUTH
key so only you can send emails - Customise
SETTING_DEFAULT_RETRIES
to the number of max retries for endpoint failures
- Get API keys for SendGrid and PostMark by creating free accounts.
-
Install dependencies and start the web service:
npm install npm start
-
Make a POST request to the webservice, making sure to include the
authKey
header and value (needs to be the same asKEY_AUTH
in.env
file).Example cURL:
curl --location --request POST 'http://localhost:8080/email' \ --header 'authKey: XXXXXXX' \ --header 'Content-Type: application/json' \ --data-raw '{ "to": "[email protected]", "to_name": "Ms.Fake", "from": "[email protected]", "from_name": "David", "subject": "A Message from sendgrid", "body": "<h1>Your Bill</h1> <p>$20000</p>" }'
-
When making a POST request to the
/email
endpoint, include in the JSON body:"isHTML": true, "htmlTags": ["a"]
-
Follow the instructions below to enable Webhooks and tracking.
- In your Sendgrid mail settings, enable
Event Notifications
and select the relevant 'Open' and 'Click' events. - Deploy your web service and enter the
/sgWebhook
endpoint in theHTTP POST URL
section. This should be a HTTPS endpoint.- Be sure to include the basic authentication details in your
.env
file underWEBHOOK_AUTH
- e.g.
https://webhook:password123@YOUR_WEB_SERVICE.COM/sgWebhook
- e.g.
- Be sure to include the basic authentication details in your
- Save the settings in Sendgrid
- Ensure the appropriate
click_tracking
andopen_tracking
values are set in the sendGrid.js file.
- In your Postmark servers settings, enable
Webhooks
by selecting 'Add Webhook'. - Deploy your web service and enter the
/pmWebhook
endpoint in theWebhook URL
section. This should be a HTTPS endpoint.- Be sure to include the basic authentication details in your
.env
file underWEBHOOK_AUTH
- e.g.
https://webhook:password123@YOUR_WEB_SERVICE.COM/pmWebhook
- e.g.
- Be sure to include the basic authentication details in your
- Select the relevant 'Open' and 'Link Click' events.
- Save the webhook.
- Ensure the appropriate
TrackOpens
andTrackLinks
values are set in the postMark.js file.
When making a POST request to the /email
endpoint, include in the JSON body:
"isHTML": true,
"htmlTags": ["a", "h1"] // Or any HTML tags to enable
Ensure you've installed the dependencies, then in the terminal: npm test
- Implement a datastore (GCP or AWS) to make emails, status logs, and open/click events queryable
- When datastore is implemented, add metadata (Sendgrid =
custom_args
, Postmark =Metadata
) to each email sent with a UID. This will enable each click/open event to be associated with a specific logged email. - Enable scheduling of emails based on timezones.