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 dotenvSet your API key
Create a .env file in your project root and add your API key:
MAILERSEND_API_KEY=<your_api_key>Send an email
Create app.js with the following:
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.jsWhat's next?
- SDK source on GitHub:
- Email API Reference
General API resources
Discover the ultimate API resources and authentication guide for MailerSend Developers. Improve your transactional messaging with our powerful API. Get started now.
Sending an Email
Learn how to start sending emails quickly with MailerSend's Email API and comprehensive developers' documentation. Integrate easily and streamline your email communication.