The SocketLabs Email Delivery Node.js library allows you to easily send email messages via the SocketLabs Injection API. The library makes it easy to build and send any type of message supported by the API, from a simple message to a single recipient all the way to a complex bulk message sent to a group of recipients with unique merge data per recipient.
Table of Contents
Prerequisites and Installation
- A supported Node.js version (
- A SocketLabs account. If you don't have one yet, you can sign up for a free account to get started.
For most uses we recommend installing the SocketLabs Email Delivery package via npm. If you already have a supported version of Node.js, then it should have included npm automatically. To install the package to your current project, simply type the following in your terminal or command prompt:
> npm install --save @socketlabs/email
Alternately, you can simply clone this repository directly to include the source code in your project.
Obtaining your API Key and SocketLabs ServerId number
In order to get started, you'll need to enable the Injection API feature in the SocketLabs Control Panel. Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in). Make note of your 4 or 5 digit ServerId number, as you'll need this along with your API key in order to use the Injection API.
To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option. Once here, you can enable the feature by choosing the "Enabled" option in the dropdown. Enabling the feature will also generate your API key, which you'll need (along with your ServerId) to start using the API. Be sure to click the "Update" button to save your changes once you are finished.
A basic message is an email message like you'd send from a personal email client such as Outlook. A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses. You can also send a file attachment in a basic message.
const SocketLabsClient = ;const client = processenvSOCKETLABS_INJECTION_API_KEY;const message =to: "firstname.lastname@example.org"from: "email@example.com"subject: "Hello from Node.js"textBody: "This message was sent using the SocketLabs Node.js library!"htmlBody: "<html>This message was sent using the SocketLabs Node.js library!</html>"messageType: 'basic'client;
A basic message supports up to 50 recipients and supports several different ways to add recipients. We've also provided several helper classes to help you construct your message more easily:
const SocketLabsClient BasicMessage EmailAddress = ;const client = processenvSOCKETLABS_INJECTION_API_KEY;//Instantiate the BasicMessage class to use built-in properties and helper methods. No need to set the messageType when using//the helper classes.let basicMessage = ;basicMessagefrom = "firstname.lastname@example.org";basicMessagesubject = "Hello from Node.js";basicMessagetextBody = "This message was sent using the SocketLabs Node.js library!";//Add a recipient by pushing a string to the To arraybasicMessageto;//Add a recipient by pushing an object literal to the To arraybasicMessageto;//Add a recipient by pushing an EmailAddress object to the To arraybasicMessageto;//Use the helper method on the instantiated BasicMessagebasicMessage;//CC and BCC addresses have all of the same optionsbasicMessagecc;basicMessage;client;
A bulk message usually contains a single recipient per message and is generally used to send the same content to many recipients, optionally customizing the message via the use of MergeData. For more information about using Merge data, please see the Injection API documentation.
const SocketLabsClient BulkMessage BulkRecipient = ;const client = processenvSOCKETLABS_INJECTION_API_KEY;let bulkMessage = ;bulkMessagetextBody = "Is your favorite color still %%FavoriteColor%%?";bulkMessagehtmlBody = "<html>Is your favorite color still %%FavoriteColor%%?";bulkMessagesubject = "Sending a Bulk Message With Merge Fields";bulkMessage;//Use built-in classes and helper methodslet recipient1 = "email@example.com";recipient1;bulkMessageto;//Use object literalsconst recipient2 =emailAddress: "firstname.lastname@example.org"friendlyName: "Recipient #2"mergeData:key: "FavoriteColor" value: "Orange");bulkMessageto;client;
Managing API Keys
For ease of demonstration, some of our examples may include the ServerId and API key directly in our code sample. Generally it is not considered a good practice to store sensitive information like this directly in your code. In most cases we recommend the use of Environment Variables.
Examples and Use Cases
In order to demonstrate the many possible use cases for the Node.js library, we've provided an assortment of code examples. These examples demonstrate many different features available to the Injection API and Node.js library, including using templates created in the SocketLabs Email Designer, custom email headers, sending attachments, sending content that is stored in an HTML file, advanced bulk merging, and even pulling recipients from a datasource.
This example demonstrates the sending of a piece of content that was created in the SocketLabs Email Designer. This is also known as the API Templates feature.
This example demonstrates how to read in your HTML content from an HTML file rather than passing in a string directly.
This example demonstrates how to add a file attachment to your message.
This example demonstrates how to embed an image in your message.
This example demonstrates sending with a specific character set.
This example demonstrates how to add custom headers to your email message.
This example demonstrates how to use a proxy with your HTTP client.
This example demonstrates many features of the Basic Send, including adding multiple recipients, adding message and mailing id's, and adding an embedded image.
This example demonstrates how to send a bulk message to multiple recipients.
This example demonstrates many features of the
adding multiple recipients, merge data, and adding an attachment.
This example uses a mock repository class to demonstrate how you would pull your recipients from a database and create a bulk mailing with merge data.
This example demonstrates how to send a bulk message with a specified character set and special characters.
The SocketLabs Node.js library and all associated code, including any code samples, are MIT Licensed.