Skip to Content
MailerSend

Sending emails with MailerSend SDKs

Install a MailerSend SDK, configure your API token, and send your first email in PHP, Node.js, Python, Go, Ruby, or Java.

MailerSend ships official SDKs for PHP, Node.js, Python, Go, Ruby, and Java. Pick your language below for the install, configure, and send-an-email steps.

Prerequisites

  • A MailerSend account with at least one verified sending domain
  • An API token generated in Settings → API Tokens
  • The runtime for your chosen language installed locally

Install the SDK and send an email

Install the SDK

Run the following command in your project directory to install the MailerSend Node.js SDK and the dotenv module:

npm install mailersend dotenv

Set your API key

Create a .env file in your project root and add your API key:

.env
MAILERSEND_API_KEY=<your_api_key>

Send an email

Create app.js with the following:

app.js
import 'dotenv/config';
import { MailerSend, EmailParams, Sender, Recipient } from "mailersend";

const mailerSend = new MailerSend({
  apiKey: process.env.MAILERSEND_API_KEY,
});

const sentFrom = new Sender("you@yourdomain.com", "Your name");
const recipients = [new Recipient("recipient@example.com", "Recipient name")];

const emailParams = new EmailParams()
  .setFrom(sentFrom)
  .setTo(recipients)
  .setSubject("Hello from MailerSend")
  .setHtml("<strong>This is the HTML content</strong>")
  .setText("This is the text content");

mailerSend.email
  .send(emailParams)
  .then((response) => console.log(response))
  .catch((error) => console.log(error));

Run it:

node app.js

What's next?

On this page