Knowledge base

Getting started with MailerSend

Your go-to guide for settings up MailerSend and sending your first email with a trial domain.

Step 1: Sign up for a free MailerSend account

1. Create your free MailerSend account.

2. Confirm your email address from the verification email sent to the address you signed up with.

Your account will now be activated with a trial plan and you can send an email with your trial domain.

Step 2: Create an API token or SMTP user

To send email with the API, from the dashboard:

1. Click the API option.

2. Enter a name and expiration date for your API token and select the permissions.

API token creation in MailerSend.

3. Click Generate token.

4. Save or download your API token. You won't be able to access it again for security reasons.

To send an email with the SMTP relay, from the dashboard:

1. Click the SMTP relay option.

2. Enter a name for your SMTP user.

SMTP user generation in MailerSend.

3. Click Save user.

4. Your SMTP credentials will be generated. Copy or download your password, you won't be able to access it again.

SMTP user credentials in MailerSend

Step 3: Send an email

1. Install the SDK for your preferred language:

PHP
composer require mailersend/mailersend
Laravel
composer require mailersend/laravel-driver
Java
<dependency>
  <groupId>com.mailersend</groupId>
  <artifactId>java-sdk</artifactId>
  <version>1.5.0</version>
</dependency>
Node.js
npm install mailersend
Go
go get github.com/mailersend/mailersend-go
Python
pip install mailersend
Ruby
gem install mailersend-ruby

2. Then copy and paste the code snippet into your project, adding your API token.

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('your@client.com', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('your@domain.com')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content')
    ->setReplyTo('reply to')
    ->setReplyToName('reply to name');

$mailersend->email->send($emailParams);
Laravel
<?php

namespace App\Mail;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use MailerSend\Helpers\Builder\Personalization;
use MailerSend\Helpers\Builder\Variable;
use MailerSend\LaravelDriver\MailerSendTrait;

class TestEmail extends Mailable
{
    use Queueable, SerializesModels, MailerSendTrait;

    public function __construct() {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Email',
        );
    }

    public function content(): Content
    {
        $to = Arr::get($this->to, '0.address');

        $this->mailersend(
            template_id: null,
            tags: ['tag'],
            personalization: [
                new Personalization($to, [
                    'var' => 'variable',
                    'number' => 123,
                    'object' => [
                        'key' => 'object-value'
                    ],
                    'objectCollection' => [
                        ['name' => 'John'],
                        ['name' => 'Patrick']
                    ],
                ])
            ],
            precedenceBulkHeader: true,
            sendAt: new Carbon('2022-01-28 11:53:20'),
        );

        return new Content(
            view: 'emails.test_html',
            text: 'emails.test_text'
        );
    }

    public function attachments(): array
    {
        return [
            Attachment::fromStorageDisk('public', 'example.png')
        ];
    }
}
Java
import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "your@recipient.com");
    email.addRecipient("name 2", "your@recipient2.com");

    Recipient recipient = new Recipient("name", "your@recipient3.com");
    email.AddRecipient(recipient);

    email.setSubject("Email subject");
    email.setPlain("This is the text content");
    email.setHtml("<p>This is the HTML content</p>");

    MailerSend ms = new MailerSend();
    ms.setToken("Your API token");

    try {
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {
        e.printStackTrace();
    }
}
Node.js
import 'dotenv/config';
import { MailerSend, EmailParams, Sender, Recipient } from "mailersend";

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

const sentFrom = new Sender("you@yourdomain.com", "Your name");

const recipients = [
  new Recipient("your@client.com", "Your Client")
];

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

await mailerSend.email.send(emailParams);
Go
package main

import (
    "context"
    "os"
    "fmt"
    "time"

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

func main() {
    ms := mailersend.NewMailersend(os.Getenv("MAILERSEND_API_KEY"))

    ctx := context.Background()
    ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
    defer cancel()

    subject := "Subject"
    text := "This is the text content"
    html := "<p>This is the HTML content</p>"

    from := mailersend.From{
        Name:  "Your Name",
        Email: "your@domain.com",
    }

    recipients := []mailersend.Recipient{
        {
            Name:  "Your Client",
            Email: "your@client.com",
        },
    }

    sendAt := time.Now().Add(time.Minute * 5).Unix()
    tags := []string{"foo", "bar"}

    message := ms.Email.NewMessage()
    message.SetFrom(from)
    message.SetRecipients(recipients)
    message.SetSubject(subject)
    message.SetHTML(html)
    message.SetText(text)
    message.SetTags(tags)
    message.SetSendAt(sendAt)
    message.SetInReplyTo("client-id")

    res, _ := ms.Email.Send(ctx, message)
    fmt.Printf(res.Header.Get("X-Message-Id"))
}
Python
from mailersend import MailerSendClient, EmailBuilder

ms = MailerSendClient()

email = (EmailBuilder()
         .from_email("sender@domain.com", "Your Name")
         .to_many([{"email": "recipient@domain.com", "name": "Recipient"}])
         .subject("Hello from MailerSend!")
         .html("<h1>Hello World!</h1>")
         .text("Hello World!")
         .build())

response = ms.emails.send(email)
Ruby
require "mailersend-ruby"

ms_client = Mailersend::Client.new('your_mailersend_token')

ms_email = Mailersend::Email.new(ms_client)

ms_email.add_recipients("email" => "ron@parksandrec.com", "name" => "Ron")
ms_email.add_recipients("email" => "leslie@parksandrec.com", "name" => "Leslie")
ms_email.add_from("email" => "april@parksandrec.com", "name" => "April")
ms_email.add_subject("Time")
ms_email.add_text("Time is money, money is power, power is pizza, and pizza is knowledge. Let's go.")
ms_email.add_html("<b>Time is money, money is power, power is pizza, and pizza is knowledge. Let's go.</b>")

ms_email.send

Send with SMTP

To send with SMTP, add the following to your code or app configuration:

  • SMTP server: smtp.mailersend.net

  • Port: 587 or 2525

  • SMTP username

  • SMTP password

Below are instructions for the most popular tools, packages and plugins.

Remember:

Most SMTP plugins will work in the same way, so you can use whichever one you prefer. However, we recommend that you select a plugin with caution to ensure that your credentials remain secure.

While WP SMTP Mail is an SMTP plugin for WordPress, it uses an API key to configure the connection to your MailerSend account.

To use the setup wizard and your API key:

1. Go to WP SMTP Mail in the WordPress dashboard and open Settings.
2. In the General tab, click Launch Setup Wizard.
3. Click Let's Get Started and in the next window, select MailerSend then click Save and Continue.
4. Add your API key, From Name, and From Email and click Save and Continue.
5. Follow the remaining steps to complete the plugin configuration.

This is our official SMTP plugin for WordPress, and the most secure way to configure SMTP for your WordPress website.

1. Install the MailerSend SMTP Plugin by going to Plugins and searching for MailerSend.
2. Find the MailerSend - Official SMTP Integration by MailerSend plugin and install and activate it.
3. Open the plugin from the side menu and enter your SMTP username and password. Click Save.

From here, you can also configure your Sender details (name, email address, CC recipients, BCC recipients, Reply-to address, and tags, and send a test email to check that your SMTP credentials are configured correctly.

This is our official plugin for WooCommerce. You can connect your account using an API key. You can also use any other WooCommerce SMTP plugin if you prefer to use SMTP credentials.

1. Install the MailerSend WooCommerce Plugin by going to Plugins and searching for MailerSend.
2. Find the MailerSend - Transactional emails for WooCommerce by MailerSend plugin and install and activate it.
3. Open the plugin from the side menu by going to WooCommerce > MailerSend.
4. Enter your API token and click Validate token.

From here, you can also configure your Email sending options (Sender Name, Sender Address, CC Addresses, BCC Addresses) and Email notifications (templates), which you can test as well.

You can configure your Laravel app to use MailerSend SMTP relay by configuring the mailer with your SMTP credentials. First, use a basic mail command to build an email template, including the from, to, CC, BCC and content, then configure the mailer. For a standard SSL/TLS server, your configuration will look something like this:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailersend.net
MAIL_PORT=587
MAIL_USERNAME=your_smtp_username
MAIL_PASSWORD=your_smtp_password
MAIL_ENCRYPTION=tls

1. Open FluentSMTP by going to Settings > FluentSMTP in WordPress.
2. Select Other SMTP and enter your Sender Settings (From Email and From Name).
3. Next, enter smtp.mailersend.net as the SMTP Host, and 587 as the SMTP Port.
4. Select TLS for encryption and enter your SMTP username and password.
5. Click Save Connection Settings.

You can configure PHPMailer to use MailerSend SMTP relay. Replace the SMTP credentials in the code snippet with your own to test.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; 
$mail = new PHPMailer(true);
try {
    $mail->isSMTP();                                            
    $mail->Host       = 'smtp.mailersend.net';                     
    $mail->SMTPAuth   = true;                                   
    $mail->Username   = 'your_smtp_username';               
    $mail->Password   = 'your_smtp_password';                       
    $mail->SMTPSecure = 'tls'; 
    $mail->Port       = 587;
$mail->setFrom('from@example.com', 'From Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Your Subject Here';
$mail->Body    = '<b>This is the HTML message body</b>';
if(!$mail->send()){
    echo 'Message failed. Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
                                    

If you want to use SMTP to integrate email sending into your Node.js apps, you can use the Nodemailer package. The following snippet configures MailerSend's SMTP relay and sends an email (remember to add your SMTP credentials).

First, add your SMTP credentials to your project's .env file:

SMTP_HOST=smtp.mailersend.net
SMTP_PORT=587
SMTP_USER=your_mailersend_username
SMTP_PASS=your_mailersend_password
                                    

Then use the following code snippet to send the email:

import 'dotenv/config';
import nodemailer from 'nodemailer';
// Load environment variables from .env file
import dotenv from 'dotenv';
dotenv.config();
// Create a transporter object using SMTP transport
const transporter = nodemailer.createTransport({
	host: process.env.SMTP_HOST,
	port: process.env.SMTP_PORT,
	secure: false, // true for 465, false for other ports
	auth: {
		user: process.env.SMTP_USER,
		pass: process.env.SMTP_PASS,
	},
});
// Setup email data
const mailOptions = {
	from: '"Name" <from@example.com>', // sender address
	to: 'recipient@example.com', // list of receivers
	subject: 'This is a test.', // Subject line
	text: 'Hey there! You just sent your first email with MailerSend. Nice one!', // plain text body
	html: `
    <p>Hey there!</p>
    <p>You just sent your first email with MailerSend.</p>
    <p>Nice one!</p>
  `, // html body
};
// Send email
transporter.sendMail(mailOptions, (error, info) => {
	if (error) {
		return console.log(error);
	}
	console.log('Message sent: %s', info.messageId);
});
                                    

Step 4: Check your email activity

Once you've sent your email, you can check your inbox to see if it's arrived as well as the Emails page.

To view the email's activity status, go to Email > Emails.

From here, you'll be able to see every point of a message's journey, from queued and sent to delivered, bounced, clicked and more.

Next steps: Take it live

Add a domain

When you're ready to start sending to recipients, the first step is to add your domain:

1. In the dashboard, scroll to the Start sending transactional emails section and click Add domain.

The Add domain option on the Getting Started page in MailerSend.

2. Click the Add domain button and follow the steps to authenticate your domain. With automatic domain connect, it only takes a few minutes.

Learn more:

Get approved

The account approval process is a crucial step for accessing all MailerSend features. This involves providing details about your company or organization and explaining how you plan to use MailerSend. The more information you provide, the quicker you'll gain full access to all features.

To submit your account for approval, just click Get approved from the notification in the dashboard.

The approval process is usually very quick but can take up to 48 hours. We recommend you to provide clear, detailed information in your approval requests to help speed up this process. Rest assured, we'll notify you as soon as a decision is made.

Subscribe to a Free plan

The Free plan gives you 500 emails a month free. To subscribe:

1. In the dashboard, scroll to the Start sending transactional emails section and click Choose plan.

2. Select a plan, click Upgrade and follow the steps to confirm your subscription.

Learn more:

More resources to enhance your sending

Customizing emails and templates:

Advanced sending and receiving:

Need more info?

Feel free to reach out to support@mailersend.com. A member of our support team will gladly assist you.