How to Send Email with JavaScript: Complete Guide with SendLayer API
Source: Dev.to
Prerequisites
- Node.js installed on your machine. Download the latest version here.
- Code editor (e.g., Visual Studio Code).
- Basic knowledge of HTML and JavaScript.
- A SendLayer account. You can get started with the trial account that lets you send up to 200 emails for free.
Start your free trial at SendLayer
After creating your SendLayer account, make sure to authorize your domain. This step is essential to improve your site’s email deliverability.
Send an Email via SendLayer API (Recommended)
Sending emails through an API is faster and more secure than using SMTP. Transactional email services like SendLayer provide APIs for sending emails programmatically with advanced features built‑in.
I’ll use SendLayer’s SDK for this guide. The SDK includes features like email validation, error handling, and file attachment encoding out of the box.
Getting Your API Key
To use the SendLayer SDK, you’ll need an active API key. Log in to your SendLayer account and navigate to Settings » API Keys.

Copy your API key – we’ll use it in the next steps.
Installing the SendLayer SDK
npm install sendlayer
Sending Your First Email
Create a new JavaScript file (e.g., emailService.js) and add the following code:
import { SendLayer } from 'sendlayer';
const apiKey = 'your-sendlayer-api-key';
// Initialize the SendLayer package with your API key
const sendlayer = new SendLayer(apiKey);
const sendEmail = async () => {
// Email data
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test Email via SendLayer API',
text: 'This is a test email sent via the SendLayer API.'
};
// email sending function with error handling
try {
const response = await sendlayer.Emails.send(params);
console.log('Email sent successfully:', response);
} catch (error) {
console.error('Error sending email:', error.message);
}
};
sendEmail();
Code Breakdown
from– Sender email address (must belong to an authorized domain).to– Recipient email address or list of recipients.subject– Email subject line.textorhtml– Email content (plain text or HTML).
The sendlayer.Emails.send() method sends the email, and the try...catch block handles any errors.
Running Your Code
node emailService.js
If successful, you’ll see a confirmation message similar to:
{
"MessageID": "62e09048-039f-4fce-a744-b096981e4990"
}

Sending HTML Emails
To send HTML‑formatted emails, use the html parameter instead of text:
const params = {
from: 'paulie@example.com',
to: 'recipient@example.com',
subject: 'Test HTML Email',
html: '
## Hello!
This is an **HTML email** sent via the SendLayer API.
'
};
Sending to Multiple Recipients
Provide an array of recipient objects for multiple addresses:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [
{ name: "Recipient 1", email: "recipient1@example.com" },
{ name: "Recipient 2", email: "recipient2@example.com" }
],
subject: "Test Email to Multiple Recipients",
html: "
This email is sent to multiple recipients.
"
};
You can also include CC and BCC recipients:
const params = {
from: { name: "Sender Name", email: "sender@example.com" },
to: [{ name: "Recipient 1", email: "recipient1@example.com" }],
subject: "Test Email",
html: "
This email includes CC and BCC recipients.
",
cc: ["cc@example.com"],
bcc: [{ name: "BCC", email: "bcc@example.com" }]
};
Note: There’s a limit to the number of recipients per email request. See the developer documentation for details.
Sending Emails with Attachments
Add an attachments field to your params. The SDK handles encoding automatically.
const params = {
from: { name: 'Sender Name', email: 'sender@example.com' },
to: [{ name: 'Recipient Name', email: 'recipient@example.com' }],
subject: 'Test Email with Attachment',
html: '
This email includes an attachment.
',
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
}
]
};
const response = await sendlayer.Emails.send(params);
You can attach multiple files, including remote URLs:
attachments: [
{
path: 'path/to/file.pdf',
type: 'application/pdf'
},
{
path: 'https://example.com/image.png',
type: 'image/png'
}
]
Pro Tip: The type field should match the MIME type of the file for proper handling by email clients.