Skip to main content

Email Client

The EmailClient class enables interaction with the Sendlix Email Service API and provides methods for sending various types of emails.

import { EmailClient } from "sendlix";

const client = new EmailClient("your-api-key");

Types

SendEmailResponse

The SendEmailResponse type represents the response after sending an email with the Sendlix API. It contains the following properties:

PropertyTypeDescription
messageListstring[]List of IDs of the sent email messages
emailsLeftnumberNumber of remaining email credits in the account

Example:

{
"messageList": ["msg_1234567890"],
"emailsLeft": 10000
}

EmailAddress

The EmailAddress type can be either a string or an object with the following properties:

PropertyTypeDescription
emailstringThe email address
namestringOptional display name

Examples:

// As a string
const address1 = "user@example.com";

// As an object
const address2 = {
email: "user@example.com",
name: "John Doe",
};

mailOption

The mailOption object configures an email to be sent:

PropertyTypeDescription
fromEmailAddressSender email address and optional name
toEmailAddress[]List of recipient email addresses
ccEmailAddress[]Optional list of CC recipients
bccEmailAddress[]Optional list of BCC recipients
subjectstringSubject line of the email
replyToEmailAddressOptional reply-to address
htmlstringOptional HTML content of the email
textstringOptional plain text content of the email
trackingbooleanOptional flag for tracking links in email

Note: At least one of html or text must be provided. If both are provided, the email will be sent as a multipart message.

Example:

const emailConfig = {
from: { email: "sender@example.com", name: "Sender Name" },
to: [{ email: "recipient@example.com", name: "Recipient Name" }],
subject: "Test Email",
html: "<h1>Hello World!</h1><p>This is a test email.</p>",
text: "Hello World! This is a test email.",
tracking: true,
};

Attachment

The Attachment object configures an email attachment:

PropertyTypeDescription
contentURLstringURL or path to the content of the attachment
filenamestringName of the file displayed to recipients
contentTypestringOptional MIME type

Example:

const attachment = {
contentURL: "https://example.com/document.pdf",
filename: "document.pdf",
contentType: "application/pdf",
};

AdditionalEmailOptions

The AdditionalEmailOptions object provides additional options for sending emails:

PropertyTypeDescription
attachmentsAttachment[]Optional list of email attachments
categorystringOptional category for the email
send_atDateOptional scheduled send time. The sending time needs to be at least 15 minutes in the future.

Example:

const additionalOptions = {
attachments: [
{
contentURL: "https://example.com/document.pdf",
filename: "document.pdf",
contentType: "application/pdf",
},
],
category: "marketing",
send_at: new Date("2025-04-01T10:00:00Z"),
};

GroupMailData

The GroupMailData object configures an email to be sent to a group:

PropertyTypeDescription
fromEmailAddressSender email address and optional name
groupIdstringID of the group to send the email to
subjectstringSubject line of the email
categorystringOptional category for the email
htmlstringOptional HTML content of the email
textstringOptional plain text content of the email
trackingbooleanOptional flag for tracking links in email

Note: At least one of html or text must be provided. If both are provided, the email will be sent as a multipart message.

Example:

const groupMailData = {
from: { email: "sender@example.com", name: "Sender Name" },
groupId: "group-123",
subject: "Group Announcement",
html: "<h1>Hello Group!</h1><p>This is a group email.</p>",
text: "Hello Group! This is a group email.",
tracking: true,
};

Methods

constructor(auth)

Creates a new EmailClient instance.

Parameters:

NameTypeDescription
authstring | IAuthAPI key or authentication handler

Example:

const client = new EmailClient("your-api-key");

sendEmail(mailOption, additionalOptions)

Sends an email with the specified configuration.

Parameters:

NameTypeDescription
mailOptionmailOptionEmail configuration
additionalOptionsAdditionalEmailOptionsOptional additional settings

Returns:

  • Promise<Response>: Promise that resolves with the email sending response

Example:

const client = new EmailClient("your-api-key");
const response = await client.sendEmail({
from: { email: "sender@example.com", name: "Sender Name" },
to: [{ email: "recipient@example.com", name: "Recipient Name" }],
subject: "Test Email",
html: "<h1>Hello World!</h1><p>This is a test email.</p>",
text: "Hello World! This is a test email.",
});

sendEmlEmail(eml, additionalOptions)

Sends a pre-formatted email in EML format.

Parameters:

NameTypeDescription
emlstring | Buffer | Uint8ArrayEML content or file path
additionalOptionsAdditionalEmailOptionsOptional additional settings

Returns:

  • Promise<Response>: Promise that resolves with the email sending response

Example:

const client = new EmailClient("your-api-key");
// Sending an EML file
const response = await client.sendEmlEmail("path/to/email.eml");

// Or with a Buffer
const emlBuffer = fs.readFileSync("path/to/email.eml");
const response = await client.sendEmlEmail(emlBuffer);

For more information on processing Eml emails, see the documentation: https://docs.sendlix.com/emlemail

sendGroupEmail(groupData)

Sends an email to a group of recipients identified by a group ID.

Parameters:

NameTypeDescription
groupDataGroupMailDataGroup email configuration

Returns:

  • Promise<number>: Number of remaining email credits in the account

Example:

const client = new EmailClient("your-api-key");
const emailsLeft = await client.sendGroupEmail({
from: { email: "sender@example.com", name: "Sender Name" },
groupId: "group-123",
subject: "Group Announcement",
html: "<h1>Hello Group!</h1><p>This is a group email.</p>",
});