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

Install the SDK

The PHP SDK uses PSR-7 and PSR-18, so install an HTTP adapter alongside the SDK:

composer require php-http/guzzle7-adapter nyholm/psr7
composer require mailersend/mailersend

Set your API key

Initialise the client with your API key:

use MailerSend\MailerSend;

$mailersend = new MailerSend(['api_key' => 'YOUR_API_KEY']);

Send an email

Build the email and send it:

use MailerSend\Helpers\Builder\EmailParams;
use MailerSend\Helpers\Builder\Recipient;

$recipients = [
    new Recipient('recipient@example.com', 'Recipient Name'),
];

$email = (new EmailParams())
    ->setFrom('you@yourdomain.com')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Hello from MailerSend')
    ->setHtml('<p>Hello world!</p>')
    ->setText('Hello world!');

$mailersend->email->send($email);

Install the SDK

Install the SDK with pip:

pip install mailersend

Set your API key

Export it as an env var:

export MAILERSEND_API_KEY=<your_api_key>

Send an email

import os
from mailersend import emails

mailer = emails.NewEmail(os.environ['MAILERSEND_API_KEY'])

mail_body = {}

mail_from = {
    "name": "Your Name",
    "email": "you@yourdomain.com",
}

recipients = [
    {"name": "Recipient Name", "email": "recipient@example.com"},
]

mailer.set_mail_from(mail_from, mail_body)
mailer.set_mail_to(recipients, mail_body)
mailer.set_subject("Hello from MailerSend", mail_body)
mailer.set_html_content("<p>This is the HTML content</p>", mail_body)
mailer.set_plaintext_content("This is the text content", mail_body)

mailer.send(mail_body)

Install the SDK

go get github.com/mailersend/mailersend-go

Set your API key

import "github.com/mailersend/mailersend-go"

ms := mailersend.NewMailersend("YOUR_API_KEY")

Send an email

package main

import (
    "context"
    "log"

    "github.com/mailersend/mailersend-go"
)

func main() {
    ms := mailersend.NewMailersend("YOUR_API_KEY")

    from := mailersend.From{
        Name:  "Your Name",
        Email: "you@yourdomain.com",
    }

    recipients := []mailersend.Recipient{
        {Name: "Recipient Name", Email: "recipient@example.com"},
    }

    message := ms.Email.NewMessage()
    message.SetFrom(from)
    message.SetRecipients(recipients)
    message.SetSubject("Hello from MailerSend")
    message.SetHTML("<p>This is the HTML content</p>")
    message.SetText("This is the text content")

    if _, err := ms.Email.Send(context.TODO(), message); err != nil {
        log.Fatal(err)
    }
}

Install the SDK

gem install mailersend-ruby

Set your API key

Add the token to a .env file in your project root, or export it system-wide:

.env
MAILERSEND_API_TOKEN=<your_api_key>

Send an email

require "mailersend-ruby"

ms_email = Mailersend::Email.new

ms_email.add_from("email" => "you@yourdomain.com", "name" => "Your Name")
ms_email.add_recipients("email" => "recipient@example.com", "name" => "Recipient Name")
ms_email.add_subject("Hello from MailerSend")
ms_email.add_html("<p>This is the HTML content</p>")
ms_email.add_text("This is the text content")

ms_email.send

Install the SDK

Add the dependency to your build file:

pom.xml (Maven)
<dependency>
    <groupId>com.mailersend</groupId>
    <artifactId>java-sdk</artifactId>
    <version>1.0.0</version>
</dependency>
build.gradle (Gradle)
implementation 'com.mailersend:java-sdk:1.0.0'

Set your API key

MailerSend ms = new MailerSend();
ms.setToken("YOUR_API_KEY");

Send an email

Email email = new Email();
email.setFrom("Your Name", "you@yourdomain.com");
email.addRecipient("Recipient Name", "recipient@example.com");
email.subject = "Hello from MailerSend";
email.html = "<p>This is the HTML content</p>";
email.text = "This is the text content";

MailerSend ms = new MailerSend();
ms.setToken("YOUR_API_KEY");

MailerSendResponse response = ms.emails().send(email);
System.out.println(response.responseStatusCode);

What's next?

On this page