Skip to content

mailersend/mailersend-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MailerSend PHP SDK

MIT licensed build badge analysis badge

Table of Contents

Installation

Requirements

  • PHP 7.4
  • PSR-7 and PSR-18 based HTTP adapter
  • An API Key from mailersend.com

Setup

This library, after version v0.22.0 is not compatible with Laravel 8.0 or lower. Please use older versions of SDK, or update your Laravel version.

This library is built atop of PSR-7 and PSR-18. You will need to install some implementations for those interfaces.

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

After that you can install the SDK.

composer require mailersend/mailersend

Finally, add an environment variable called MAILERSEND_API_KEY with the appropriate API key.

Optionally, although not recommended, you can manually add the API key when instantiating the MailerSend class, like so:

use MailerSend\MailerSend;

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

Usage

Email

Send an email

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->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);

HTML content is not required. You still can send an email with Text only.

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setText('This is the text content');

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

Add CC, BCC recipients

Send an email with CC and BCC.

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$cc = [
    new Recipient('[email protected]', 'CC'),
];

$bcc = [
    new Recipient('[email protected]', 'BCC'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setCc($cc)
    ->setBcc($bcc)
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content');

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

Send a template-based email

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$tags = ['tag'];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setTemplateId('ss243wdasd')
    ->setTags($tags);

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

Advanced personalization

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$personalization = [
    new Personalization('[email protected]', [
        'var' => 'variable',
        'number' => 123,
        'object' => [
            'key' => 'object-value'
        ],
        'objectCollection' => [
            [
                'name' => 'John'
            ],
            [
                'name' => 'Patrick'
            ]
        ],
    ])
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject {$var}')
    ->setHtml('This is the html version with a {$var}.')
    ->setText('This is the text versions with a {$var}.')
    ->setPersonalization($personalization);

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

Send email with attachment

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$attachments = [
    new Attachment(file_get_contents('attachment.jpg'), 'attachment.jpg')
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the html version.')
    ->setText('This is the text version.')
    ->setAttachments($attachments);

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

Send a scheduled message

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the html version.')
    ->setText('This is the text version.')
    ->setSendAt(1665626400);
    ->setPrecedenceBulkHeader(true);

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

Send email with precedence bulk header

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the html version.')
    ->setText('This is the text version.')
    ->setPrecedenceBulkHeader(true);

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

Send an email with tracking

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

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content')
    ->setTrackClicks(true)
    ->setTrackOpens(true)
    ->setTrackContent(true);

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

Send an email with custom headers

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

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

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$headers = [
    new Header('Custom-Header-1', 'Value 1')
    new Header('Custom-Header-2', 'Value 2')
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content')
    ->setHeaders($headers);

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

Bulk email API

Send bulk email

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

$mailersend = new MailerSend();

$bulkEmailParams = [];

$bulkEmailParams[] = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients([
        new Recipient('[email protected]', 'Your Client'),
    ])
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content');

$bulkEmailParams[] = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients([
        new Recipient('[email protected]', 'Your Client'),
    ])
    ->setSubject('Subject')
    ->setHtml('This is the HTML content')
    ->setText('This is the text content');

$mailersend->bulkEmail->send($bulkEmailParams);

Get bulk email status

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->bulkEmail->getStatus('bulk_email_id');

Inbound routing

Get a list of inbound routes

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->inbound->getAll($domainId = 'domainId', $page = 1, $limit = 10);

Get a single inbound route

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->inbound->find('inboundId');

Add an inbound route

Example using only classes:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Inbound;
use \MailerSend\Helpers\Builder\CatchFilter;
use \MailerSend\Helpers\Builder\MatchFilter;
use \MailerSend\Helpers\Builder\Forward;
use \MailerSend\Helpers\Builder\Filter;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->inbound->create(
    (new Inbound('domainId', 'name', true))
        ->setInboundDomain('inboundDomain')
        ->setCatchFilter(
            (new CatchFilter(Constants::TYPE_CATCH_RECIPIENT)
                ->addFilter(new Filter(Constants::COMPARER_EQUAL, '[email protected]'))))
        ->setMatchFilter(
            (new MatchFilter(Constants::TYPE_MATCH_SENDER))
                ->addFilter(new Filter(Constants::COMPARER_EQUAL, '[email protected]', 'sender')))
        ->addForward(new Forward(Constants::COMPARER_EQUAL, 'value'))
);

Example using both classes and arrays:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Inbound;
use \MailerSend\Helpers\Builder\CatchFilter;
use \MailerSend\Helpers\Builder\MatchFilter;
use \MailerSend\Helpers\Builder\Forward;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->inbound->create(
    (new Inbound('domainId', 'name', true))
        ->setInboundDomain('inboundDomain')
        ->setCatchFilter(
            (new CatchFilter(Constants::TYPE_CATCH_RECIPIENT))
                ->setFilters([
                    [
                        'comparer' => Constants::COMPARER_EQUAL,
                        'value' => '[email protected]',
                    ]
                ])
        )
        ->setMatchFilter(
            (new MatchFilter(Constants::TYPE_MATCH_SENDER))
                ->setFilters([
                    [
                        'comparer' => Constants::COMPARER_EQUAL,
                        'value' => '[email protected]',
                        'key' => 'sender',
                    ]
                ])
        )
        ->addForward(new Forward(Constants::COMPARER_EQUAL, 'value'))
);

Example using only arrays:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Inbound;
use \MailerSend\Helpers\Builder\CatchFilter;
use \MailerSend\Helpers\Builder\MatchFilter;
use \MailerSend\Helpers\Builder\Forward;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->inbound->create(
    (new Inbound('domainId', 'name', true))
        ->setInboundDomain('inboundDomain')
        ->setCatchFilter([
            'type' => Constants::TYPE_CATCH_RECIPIENT,
            'filters' => [
                [
                    'comparer' => Constants::COMPARER_EQUAL,
                    'value' => '[email protected]',
                ],
            ],
        ])
        ->setMatchFilter([
            'type' => Constants::TYPE_MATCH_SENDER,
            'filters' => [
                [
                    'comparer' => Constants::COMPARER_EQUAL,
                    'value' => '[email protected]',
                    'key' => 'sender',
                ],
            ],
        ])
        ->setForwards([
            [
                'type' => Constants::COMPARER_EQUAL,
                'value' => 'value',
            ]
        ])
);

Update an inbound route

The examples on building the Inbound object portrayed in the 'Add an inbound route' also apply in here.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Inbound;
use \MailerSend\Helpers\Builder\CatchFilter;
use \MailerSend\Helpers\Builder\MatchFilter;
use \MailerSend\Helpers\Builder\Forward;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->inbound->update(
    'inboundId',
    (new Inbound('domainId', 'name', true))
        ->setInboundDomain('inboundDomain')
        ->setCatchFilter(
            (new CatchFilter(Constants::TYPE_CATCH_ALL))
        )
        ->setMatchFilter(new MatchFilter(Constants::TYPE_MATCH_ALL))
        ->addForward(new Forward(Constants::COMPARER_EQUAL, 'value'))
);

Delete an inbound route

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->inbound->delete('inboundId');

Activity

Get a list of activities

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\ActivityParams;

$mailersend = new MailerSend();

$activityParams = (new ActivityParams())
                    ->setPage(3)
                    ->setLimit(15)
                    ->setDateFrom(1623073576)
                    ->setDateTo(1623074976)
                    ->setEvent(['queued', 'sent']);

$mailersend->activity->getAll('domainId', $activityParams);

Get a single activity

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\ActivityParams;

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

$mailersend->activity->find('activity_id');

Analytics

Get activity data by date

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\ActivityAnalyticsParams;
use MailerSend\Common\Constants;

$mailersend = new MailerSend();

$activityAnalyticsParams = (new ActivityAnalyticsParams(100, 101))
                    ->setDomainId('domain_id')
                    ->setGroupBy(Constants::GROUP_BY_DAYS)
                    ->setTags(['tag'])
                    ->setEvent(['queued', 'sent']);

$mailersend->analytics->activityDataByDate($activityAnalyticsParams);

Opens by country

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\OpensAnalyticsParams;

$mailersend = new MailerSend();

$opensAnalyticsParams = (new OpensAnalyticsParams(100, 101))
                    ->setDomainId('domain_id')
                    ->setTags(['tag']);

$mailersend->analytics->opensByCountry($opensAnalyticsParams);

Opens by user-agent

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\OpensAnalyticsParams;

$mailersend = new MailerSend();

$opensAnalyticsParams = (new OpensAnalyticsParams(100, 101))
                    ->setDomainId('domain_id')
                    ->setTags(['tag']);

$mailersend->analytics->opensByUserAgentName($opensAnalyticsParams);

Opens by reading environment

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\OpensAnalyticsParams;

$mailersend = new MailerSend();

$opensAnalyticsParams = (new OpensAnalyticsParams(100, 101))
                    ->setDomainId('domain_id')
                    ->setTags(['tag']);

$mailersend->analytics->opensByReadingEnvironment($opensAnalyticsParams);

Domains

Get a list of domains

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->getAll($page = 1, $limit = 10, $verified = true);

Get domain

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->find('domain_id');

Add a domain

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\DomainParams;

$mailersend = new MailerSend();

$domainParams = (new DomainParams('domainName'))
                    ->setReturnPathSubdomain('returnPath')
                    ->setCustomTrackingSubdomain('customTracking')
                    ->getInboundRoutingSubdomain('inboundRouting');

$mailersend->domain->create($domainParams);

Delete domain

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->delete('domain_id');

Get a list of recipients per domain

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->recipients($domainId = 'domain_id', $page = 1, $limit = 10);

Update domain settings

Here you can set as many properties as you need, one or multiple.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\DomainSettingsParams;

$mailersend = new MailerSend();

$domainSettingsParam = (new DomainSettingsParams())
                            ->setSendPaused(true)
                            ->setTrackClicks(true)
                            ->setTrackOpens(false)
                            ->setTrackUnsubscribe(false)
                            ->setTrackContent(true)
                            ->setTrackUnsubscribeHtml('html')
                            ->setTrackUnsubscribePlain('plain')
                            ->setCustomTrackingEnabled(true)
                            ->setCustomTrackingSubdomain(false);

$mailersend->domain->domainSettings($domainId = 'domain_id', $domainSettingsParam);

Verify a domain

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->verify('domain_id');

Get DNS records

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->domain->getDnsRecords('domain_id');

Messages

Get a list of messages

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->messages->get($limit = 100, $page = 3);

Get info on a message

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->messages->find('message_id');

Scheduled Messages

Get a list of scheduled messages

use MailerSend\MailerSend;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->scheduleMessages->getAll(
    'domain_id',
    Constants::STATUS_SCHEDULED,
    $limit = 100,
    $page = 3
)

Get a single scheduled message

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->scheduleMessages->find('message_id');

Delete a scheduled message

use MailerSend\MailerSend;
use \MailerSend\Common\Constants;

$mailersend = new MailerSend();

$mailersend->scheduleMessages->delete('message_id');

Tokens

Get all tokens

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->token->getAll($page = 1, $limit = 10);

Find a token

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->token->find('token_id'); 

Create a token

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\TokenParams;

$mailersend = new MailerSend();

$mailersend->token->create(
    new TokenParams('token name', 'domainId', TokenParams::ALL_SCOPES)
);

Because of security reasons, we only allow access token appearance once during creation. In order to see the access token created you can do:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\TokenParams;

$mailersend = new MailerSend();

$response = $mailersend->token->create(
    new TokenParams('token name', 'domainId', TokenParams::ALL_SCOPES)
);

echo $response['body']['data']['accessToken'];

Change a token name

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\TokenParams;

$mailersend = new MailerSend();

$mailersend->token->changeName('token_id', 'new name'); 

Update token

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\TokenParams;

$mailersend = new MailerSend();

$mailersend->token->update('token_id', TokenParams::STATUS_PAUSE); // PAUSE
$mailersend->token->update('token_id', TokenParams::STATUS_UNPAUSE); // UNPAUSE

Delete Token

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\TokenParams;

$mailersend = new MailerSend();

$mailersend->token->delete('token_id');

Recipients

Get a list of recipients

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->recipients->get(null, $limit = 100, $page = 3);
// Or for a specific domain
$mailersend->recipients->get('domain_id', $limit = 100, $page = 3);

Get single recipient

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->recipients->find('recipient_id');

Delete recipient

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->recipients->delete('recipient_id');

Add recipients to a suppression list

Blocklist

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\BlocklistParams;

$mailersend = new MailerSend();

$params = (new BlocklistParams())
    ->setDomainId('domain_id')
    ->setRecipients(['recipient_one', 'recipient_two'])
    ->setPatterns(['pattern_one', 'pattern_two']);

$mailersend->blocklist->create($params);

Hard Bounces

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SuppressionParams;

$mailersend = new MailerSend();

$params = (new SuppressionParams())
    ->setDomainId('domain_id')
    ->setRecipients(['recipient_one', 'recipient_two']);

$mailersend->hardBounce->create($params);

Spam Complaints

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SuppressionParams;

$mailersend = new MailerSend();

$params = (new SuppressionParams())
    ->setDomainId('domain_id')
    ->setRecipients(['recipient_one', 'recipient_two']);

$mailersend->spamComplaint->create($params);

Unsubscribes

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SuppressionParams;

$mailersend = new MailerSend();

$params = (new SuppressionParams())
    ->setDomainId('domain_id')
    ->setRecipients(['recipient_one', 'recipient_two']);

$mailersend->unsubscribe->create($params);

Delete recipients from a suppression list

Blocklist

use MailerSend\MailerSend;

$mailersend = new MailerSend();

// Delete specific instances
$mailersend->blocklist->delete(['id_one', 'id_two']);

// or delete all
$mailersend->blocklist->delete(null, true);

// You can also specify the domain
$mailersend->blocklist->delete(['id'], false, 'domain_id');

Hard Bounces

use MailerSend\MailerSend;

$mailersend = new MailerSend();

// Delete specific instances
$mailersend->hardBounce->delete(['id_one', 'id_two']);

// or delete all
$mailersend->hardBounce->delete(null, true);

// You can also specify the domain
$mailersend->hardBounce->delete(['id'], false, 'domain_id');

Spam Complaints

use MailerSend\MailerSend;

$mailersend = new MailerSend();

// Delete specific instances
$mailersend->spamComplaint->delete(['id_one', 'id_two']);

// or delete all
$mailersend->spamComplaint->delete(null, true);

// You can also specify the domain
$mailersend->spamComplaint->delete(['id'], false, 'domain_id');

Unsubscribes

use MailerSend\MailerSend;

$mailersend = new MailerSend();

// Delete specific instances
$mailersend->unsubscribe->delete(['id_one', 'id_two']);

// or delete all
$mailersend->unsubscribe->delete(null, true);

// You can also specify the domain
$mailersend->unsubscribe->delete(['id'], false, 'domain_id');

On Hold List

use MailerSend\MailerSend;

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

// Delete specific instances
$mailersend->onHoldList->delete(['id_one', 'id_two']);

// or delete all
$mailersend->onHoldList->delete(null, true);

Get recipients from a suppression list

Blocklist

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->blocklist->getAll('domain_id', 15);

Hard Bounces

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->hardBounce->getAll('domain_id', 15);

Spam Complaints

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->spamComplaint->getAll('domain_id', 15);

Unsubscribes

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->unsubscribe->getAll('domain_id', 15);

On Hold List

use MailerSend\MailerSend;

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

$mailersend->onHoldList->getAll('domain_id', 15);

Webhooks

Get a list of webhooks

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->webhooks->get('domain_id');

Get webhook

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->webhooks->find('webhook_id');

Create webhook

use MailerSend\Helpers\Builder\WebhookParams;
use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->webhooks->create(
    new WebhookParams('https://webhook_url', 'Webhook name', WebhookParams::ALL_ACTIVITIES, 'domain_id')
);

// Or a disabled webhook

$mailersend->webhooks->create(
    new WebhookParams('https://webhook_url', 'Webhook name', WebhookParams::ALL_ACTIVITIES, 'domain_id', false)
);

Update webhook

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\WebhookParams;

$mailersend = new MailerSend();

$mailersend->webhooks->update('webhook_id', 'https://webhook_url', 'Webhook name', WebhookParams::ALL_ACTIVITIES);

// Enable webhook
$mailersend->webhooks->update('webhook_id', 'https://webhook_url', 'Webhook name', WebhookParams::ALL_ACTIVITIES, true);

// Disable webhook
$mailersend->webhooks->update('webhook_id', 'https://webhook_url', 'Webhook name', WebhookParams::ALL_ACTIVITIES, false);

Delete webhook

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->webhooks->delete('webhook_id');

If, at the moment, some endpoint is not available, please use cURL and other available tools to access it. Refer to official API docs for more info.

Templates

Get a list of templates

use MailerSend\MailerSend;

$mailersend = new MailerSend();

// Get all templates of an account
$mailersend->template->getAll();

// Get all templates of a domain
$mailersend->template->getAll('domain_id');

// Get page 2 of templates with 20 records per page
$mailersend->template->getAll('domain_id', 2, 20);

Get a single template

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->template->find('template_id');

Delete a template

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->template->delete('template_id');

Email Verification

Get all email verification lists

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->emailVerification->getAll($page = 1, $limit = 10);

Get an email verification list

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->emailVerification->find('email_verification_id');

Create an email verification list

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\EmailVerificationParams;

$mailersend = new MailerSend();

$emailVerificationParams = (new EmailVerificationParams('file.csv'))
    ->setEmailAddresses(['[email protected]']);

$mailersend->emailVerification->create($emailVerificationParams);

Verify an email list

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->emailVerification->verify('email_verification_id');

Get email verification list results

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\EmailVerificationParams;

$mailersend = new MailerSend();

$mailersend->emailVerification->getResults(
        $emailVerificationId = 'email_verification_id',
        $page = 1,
        $limit = 10,
        $results = [
            EmailVerificationParams::TYPO,
            EmailVerificationParams::CATCH_ALL,
        ],
    );

SMS

Send SMS

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsParams;

$mailersend = new MailerSend();

$smsParams = (new SmsParams())
    ->setFrom('+12065550101')
    ->setTo(['+12065550102'])
    ->addRecipient('+12065550103')
    ->setText('Text');
    
$sms = $mailersend->sms->send($smsParams);

Personalization

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsParams;

$mailersend = new MailerSend();

$smsParams = (new SmsParams())
    ->setFrom('+12065550101')
    ->setTo(['+12065550102'])
    ->setText('Text {{ var }}')
    ->setPersonalization([
        new SmsPersonalization('+12065550102', [
            'var' => 'variable',
            'number' => 123,
            'object' => [
                'key' => 'object-value'
            ],
            'objectCollection' => [
                [
                    'name' => 'John'
                ],
                [
                    'name' => 'Patrick'
                ]
            ],
        ])
    ]);
    
$sms = $mailersend->sms->send($smsParams);

SMS phone numbers

Get a list of SMS phone numbers

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$sms = $mailersend->smsNumber->getAll($page = 1, $limit = 10, $paused = true);

Get an SMS phone number

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$sms = $mailersend->smsNumber->find('sms_number_id');

Update a single SMS phone number

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$sms = $mailersend->smsNumber->update('sms_number_id', $paused = true);

Delete an SMS phone number

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$sms = $mailersend->smsNumber->delete('sms_number_id');

SMS messages API

Get a list of SMS messages

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsMessages = $mailersend->smsMessage->getAll($page = 1, $limit = 10);

Get an SMS message

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsMessage = $mailersend->smsMessage->find('sms_message_id');

SMS activity API

Get a list of SMS activities

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsActivityParams;

$mailersend = new MailerSend();

$smsActivityParams = (new SmsActivityParams())
    ->setSmsNumberId('sms_number_id')
    ->setDateFrom(1623073576)
    ->setDateTo(1623074976)
    ->setStatus(['queued'])
    ->setPage(3)
    ->setLimit(15);

$smsActivity = $mailersend->smsActivity->getAll($smsActivityParams);

SMS recipients API

Get a list of SMS recipients

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsRecipientParams;

$mailersend = new MailerSend();

$smsRecipientParams = (new SmsRecipientParams())
    ->setSmsNumberId('sms_number_id')
    ->setStatus('opt_out')
    ->setPage(3)
    ->setLimit(15);

$smsRecipients = $mailersend->smsRecipient->getAll($smsRecipientParams);

Get an SMS recipient

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsRecipient->find('sms_recipient_id');

Update a single SMS recipient

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsRecipient->update('sms_recipient_id', $status = 'opt_out');

SMS webhooks API

Get a list of SMS webhooks

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsWebhook->get('sms_number_id');

Get a single SMS webhook

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsWebhook->find('sms_webhook_id');

Create a single SMS webhook

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsWebhookParams;

$mailersend = new MailerSend();

$smsWebhookParams = (new SmsWebhookParams())
    ->setSmsNumberId('sms_number_id')
    ->setName('Name')
    ->setUrl('https://mailersend.com/sms_webhook')
    ->setEvents(['sms.sent', 'sms.delivered', 'sms.failed'])
    ->setEnabled(false);

$smsRecipients = $mailersend->smsWebhook->create($smsWebhookParams);

Update a single SMS webhook

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsWebhookParams;

$mailersend = new MailerSend();

$smsWebhookParams = (new SmsWebhookParams())
    ->setSmsNumberId('sms_number_id')
    ->setName('Name')
    ->setUrl('https://mailersend.com/sms_webhook')
    ->setEvents(['sms.sent', 'sms.delivered', 'sms.failed'])
    ->setEnabled(false);

$smsRecipients = $mailersend->smsWebhook->update($smsWebhookParams);

SMS inbound routing API

Get a list of SMS inbound routes

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsInbound->getAll($smsNumberId = 'sms_number_id', $enabled = true, $page = 3, $limit = 15);

Get a single SMS inbound route

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsInbound->find('sms_inbound_id');

Add an SMS inbound route

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsInbound;
use MailerSend\Helpers\Builder\SmsInboundFilter;

$mailersend = new MailerSend();

$smsInboundParams = (new SmsInbound())
    ->setSmsNumberId('sms_number_id')
    ->setName('Name')
    ->setForwardUrl('https://mailersend.com/inbound_webhook')
    ->setFilter(new SmsInboundFilter($comparer = 'starts-with', $value = 'Stop'))
    ->setEnabled(true);

$smsRecipients = $mailersend->smsInbound->create($smsInboundParams);

Update an inbound route

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SmsInbound;
use MailerSend\Helpers\Builder\SmsInboundFilter;

$mailersend = new MailerSend();

$smsInboundParams = (new SmsInbound())
    ->setSmsNumberId('sms_number_id')
    ->setName('Name')
    ->setForwardUrl('https://mailersend.com/inbound_webhook')
    ->setFilter(new SmsInboundFilter($comparer = 'starts-with', $value = 'Stop'))
    ->setEnabled(true);

$smsRecipients = $mailersend->smsInbound->update('sms_inbound_id', $smsInboundParams);

Delete an inbound route

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$smsRecipients = $mailersend->smsInbound->delete('sms_inbound_id');

Sender identities

Get a list of Sender Identities

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->senderIdentity->getAll($domainId = 'domainId', $page = 1, $limit = 10);

Get a single Sender Identity

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->senderIdentity->find('identityId');

Get a single Sender Identity by email

use MailerSend\MailerSend;

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

$mailersend->senderIdentity->findByEmail('email');

Add a Sender Identity

Example using only classes:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SenderIdentity;

$mailersend = new MailerSend();

$mailersend->senderIdentity->create(
    (new SenderIdentity('domainId', 'name', 'email'))
);

Example using all options:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SenderIdentity;

$mailersend = new MailerSend();

$mailersend->senderIdentity->create(
    (new SenderIdentity('domainId', 'name', 'email'))
        ->setReplyToName("John Doe")
        ->setReplyToEmail("[email protected]"))
        ->setAddNote(true)
        ->setPersonalNote("Hi John, please use this token")
);

Update a Sender Identity

The examples on building the Sender Identity object portrayed in the 'Add a Sender Identity' also apply in here.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SenderIdentity;

$mailersend = new MailerSend();

$mailersend->senderIdentity->update(
    'identityId',
    (new SenderIdentity('domainId', 'name', 'email'))
        ->setReplyToName("John Doe")
        ->setReplyToEmail("[email protected]"))
        ->setAddNote(true)
        ->setPersonalNote("Hi John, please use this token")
);

Update a Sender Identity by email

The examples on building the Sender Identity object portrayed in the 'Add a Sender Identity' also apply in here.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\SenderIdentity;

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

$mailersend->senderIdentity->updateByEmail(
    'identityId',
    (new SenderIdentity('domainId', 'name', 'email'))
        ->setReplyToName("John Doe")
        ->setReplyToEmail("[email protected]"))
        ->setAddNote(true)
        ->setPersonalNote("Hi John, please use this token")
);

Delete a Sender Identity

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->senderIdentity->delete('identityId');

Delete a Sender Identity by email

use MailerSend\MailerSend;

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

$mailersend->senderIdentity->deleteByEmail('email');

SMTP Users

Get a list of SMTP Users

use MailerSend\MailerSend;

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

$mailersend->smtpUser->getAll('domainId', 25);

Get a single SMTP User

use MailerSend\MailerSend;

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

$mailersend->smtpUser->find('domainId', 'smtpUserId');

Add SMTP User

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;
use MailerSend\Helpers\Builder\SmtpUserParams;

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

$mailersend->smtpUser->create(
    'domainId',
    (new SmtpUserParams('name'))
        ->setEnabled(false)
);

Update SMTP User

The examples on building the SMTP User object portrayed in the 'Add SMTP User' also apply in here.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;

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

$mailersend->smtpUser->update(
    'domainId',
    'smtpUserId',
    (new SmtpUserParams('New name'))
        ->setEnabled(false)
);

Delete SMTP User

use MailerSend\MailerSend;

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

$mailersend->smtpUser->delete('domainId', 'smtpUserId');

Users

Get a list of Users

use MailerSend\MailerSend;

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

$mailersend->user->getAll();

Get a single User

use MailerSend\MailerSend;

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

$mailersend->user->find('userId');

Add a User

Example using only classes:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;
use MailerSend\Common\Roles;

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

$mailersend->user->create(
    (new UserParams('email', Roles::ADMIN))
);

Example using all options:

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;
use MailerSend\Common\Roles;
use MailerSend\Common\Permissions;

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

$mailersend->user->create(
    (new UserParams('email', Roles::CUSTOM_USER))
        ->setDomains(['domainId', 'anotherDomainId'])
        ->setTemplates(['templateId', 'anotherTemplateId'])
        ->setPermissions([Permissions::READ_OWN_TEMPLATES])
        ->setRequiresPeriodicPasswordChange(true)
);

Update a User

The examples on building the User object portrayed in the 'Add a User' also apply in here.

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;
use MailerSend\Common\Roles;
use MailerSend\Common\Permissions;

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

$mailersend->user->update(
    'userId',
    (new UserParams())
        ->setRole(Roles::CUSTOM_USER)
        ->setDomains(['domainId', 'anotherDomainId'])
        ->setTemplates(['templateId', 'anotherTemplateId'])
        ->setPermissions([Permissions::READ_OWN_TEMPLATES])
        ->setRequiresPeriodicPasswordChange(true)
);

Delete a User

use MailerSend\MailerSend;

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

$mailersend->user->delete('userId');

<a name="get-a-list-of-invites>

Get a list of Invites

use MailerSend\MailerSend;

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

$mailersend->invite->getAll();

Get a single Invite

use MailerSend\MailerSend;

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

$mailersend->invite->find('inviteId');

Resend an Invite

use MailerSend\MailerSend;

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

$mailersend->invite->resend('inviteId');

<a name="cancel-an-invite>

Cancel an Invite

use MailerSend\MailerSend;

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

$mailersend->invite->cancel('inviteId');

Other endpoints

Get API quota

use MailerSend\MailerSend;

$mailersend = new MailerSend();

$mailersend->apiQuota->get();

Debugging validation errors

use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
use MailerSend\Exceptions\MailerSendValidationException;
use MailerSend\Exceptions\MailerSendRateLimitException;

$mailersend = new MailerSend();

$recipients = [
    new Recipient('[email protected]', 'Your Client'),
];

$emailParams = (new EmailParams())
    ->setFrom('[email protected]')
    ->setFromName('Your Name')
    ->setRecipients($recipients)
    ->setSubject('Subject {$var}')
    ->setHtml('This is the html version with a {$var}.')
    ->setText('This is the text versions with a {$var}.');

try{
    $mailersend->email->send($emailParams);
} catch(MailerSendValidationException $e){
    // See src/Exceptions/MailerSendValidationException.php for more more info
    print_r($e->getResponse()->getBody()->getContents());
    print_r($e->getBody());
    print_r($e->getHeaders());
    print_r($e->getErrors());
    print_r($e->getStatusCode());
} catch (MailerSendRateLimitException $e) {
    print_r($e->getHeaders());
    print_r($e->getResponse()->getBody()->getContents());
}

Testing

composer test

Support and Feedback

In case you find any bugs, submit an issue directly here in GitHub.

You are welcome to create SDK for any other programming language.

If you have any troubles using our API or SDK free to contact our support by email [email protected]

The official documentation is at https://developers.mailersend.com

License

The MIT License (MIT)