PHPackages                             milosp/mailer-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. milosp/mailer-sdk

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

milosp/mailer-sdk
=================

SDK for the mailer service

v1.0.3(9mo ago)01PHP

Since Aug 25Pushed 9mo agoCompare

[ Source](https://github.com/miilos/mailer-sdk)[ Packagist](https://packagist.org/packages/milosp/mailer-sdk)[ RSS](/packages/milosp-mailer-sdk/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (7)Versions (5)Used By (0)

Mailer SDK
==========

[](#mailer-sdk)

`mailer-sdk` is a PHP SDK for the mailer API

Getting started
---------------

[](#getting-started)

### Installing

[](#installing)

You can install via Composer:

`composer require milosp/mailer-sdk`

or include the SDK under `repositories` in your `composer.json` file:

```
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/miilos/mailer-sdk"
    }
]
```

Usage
-----

[](#usage)

The `Mailer` class allows you to send emails by calling the API, or by dispatching messages into a RabbitMQ exchange, which the API will then also pick up. First, configure the `Mailer`:

### Configuration

[](#configuration)

The mailer takes in an `$options` array as an argument with the following keys:

- `base_uri` - the base URL where the API is located (like `http://localhost:8000`)
- `api_client` - the HTTP client to use when communicating with the API
- `amqp_client` - the AMQP client to use when dispatching messages

#### HTTP client configuration

[](#http-client-configuration)

By default, the SDK uses the Symfony HTTP client to make requests. If you have `symfony/http-client` installed, you don't need to do any configuration. If you want to use a different HTTP client (Guzzle, cURL...), just create an instance of the `ApiClient` class provided by the SDK. The `ApiClient` class uses PSR compliant interfaces to make it possible to integrate whatever http client you want to use. To create an instance of `ApiClient`, pass in the following arguments:

```
use Milos\MailerSdk\Core\ApiClient;

$apiClient = new ApiClient(
    $httpClient,
    $requestFactory,
    $streamFactory
);
```

- `$httpClient` - an instance of the PSR-18 `ClientInterface`, handles PSR-7 requests and responses
- `$requestFactory` - an instance of the PSR-17 `RequestFactoryInterface`, handles PSR-7 request creation
- `$streamFactory` - an instance of the PSR-17 `StreamFactoryInterface`, handles creating streams when sending requests with data in their bodies

pass the object to the `Mailer` constructor:

```
$mailer = new Mailer([
    'base_uri' => 'http://example.com:8000'
    'api_client' => $apiClient
]);
```

#### AMQP client configuration

[](#amqp-client-configuration)

If you don't plan on dispatching messages into the API's queue, you don't need to pass in anything under the `amqp_client` key in the config. If you do want to dispatch messages, pass in an instance of the `AmqpClient` class:

```
use Milos\MailerSdk\Core\AmqpClient;

$amqpClient = new AmqpClient(
    $host, // 'localhost'
    $port, // 5674
    $user, // 'guest'
    $password, // 'guest'
    $exchange, // 'your-exchange'
    $routingKey // 'your-routing-key'
);
```

and pass the object in the `$options`:

```
$mailer = new Mailer([
    'base_uri' => 'http://example.com:8000'
    'amqp_client' => $amqpClient
]);
```

The `$exchange` and `$routingKey` parameters are optional and should be passed to the constructor if you only plan to dispatch messages into the same exchange and queue every time. If those might change, pass the `$exchange` and `$routingKey` into the `sendAsMessage()` method to configure these parameters on a per-message basis. If you use both the constructor and the method parameters, the parameters passed to `sendAsMessage()` take precedence.

### Sending emails through the API

[](#sending-emails-through-the-api)

In order to send emails through the API, first assemble an `EmailDto` object. You can use the `EmailBuilder` class for this:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setSubject('test')
    ->setFrom('testSender@gmail.com')
    ->setTo([
        'testRecipient@gmail.com'
    ])
    ->setCc([
        'testCCAddress@gmail.com
    ])
    ->setBcc([
        'testBCCAddress@gmail.com
    ])
    ->setBody('This a test email body')
    ->getEmail();
```

and then send the email:

```
$mailer->emails->send($emailDto);
```

The `send()` method returns the API's response as an instance of `ResponseInterface`.

You can also validate the email before getting the object to make sure you have the essential properties (`subject`, `from`, `to` and `body`) set:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setSubject('test')
    ->setFrom('testSender@gmail.com')
    ->setTo([
        'testRecipient@gmail.com'
    ])
    ->validate() // throws a MailerException - body isn't set
    ->getEmail();
```

You can also set API-specific things like an email template, body template or variables:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setSubject('test')
    ->setFrom('testSender@gmail.com')
    ->setTo([
        'testRecipient@gmail.com'
    ])
    ->setBodyTemplate('test-body-template')
    ->validate() // this passes validation - a template is used for the body
    ->getEmail();
```

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setSubject('test')
    ->setFrom('testSender@gmail.com')
    ->setTo([
        'testRecipient@gmail.com'
    ])
    ->setBody('This template uses variables: {{ var }}')
    ->setVariables([
        'var' => 'some variable value'
    ])
    ->getEmail();
```

If you have an email template set up that fills in all the fields of an email, you can also do this:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setEmailTemplate('some-email-template')
    ->getEmail();
```

You can also override specific fields of a template:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->setSubject('New subject') // this will override the template's subject
    ->setEmailTemplate('some-email-template')
    ->getEmail();
```

### Sending emails by dispatching RabbitMQ messages

[](#sending-emails-by-dispatching-rabbitmq-messages)

To do this, you must set up an `AmqpClient`, otherwise, a `MailerException` will be thrown when you try to dispatch a message.

When sending an email this way, you also create an `EmailDto` object, you just use a different method to send the email - `sendAsMessage()`:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->...

$mailer->emails->sendAsMessage($emailDto)
```

You can specify an exchange and routing key for the message here:

```
use Milos\MailerSdk\Dtos\EmailBuilder;

$emailDto = (new EmailBuilder())
    ->...

$mailer->emails->sendAsMessage($emailDto, 'your-exchange', 'your-routing-key')
```

Authors
-------

[](#authors)

- Miloš Popović

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance55

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

4

Last Release

297d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/132298451?v=4)[Miloš Popović](/maintainers/miilos)[@miilos](https://github.com/miilos)

---

Top Contributors

[![miilos](https://avatars.githubusercontent.com/u/132298451?v=4)](https://github.com/miilos "miilos (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/milosp-mailer-sdk/health.svg)

```
[![Health](https://phpackages.com/badges/milosp-mailer-sdk/health.svg)](https://phpackages.com/packages/milosp-mailer-sdk)
```

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.4k1.5M3](/packages/shopware-platform)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)[mollie/mollie-api-php

Mollie API client library for PHP. Mollie is a European Payment Service provider and offers international payment methods such as Mastercard, VISA, American Express and PayPal, and local payment methods such as iDEAL, Bancontact, SOFORT Banking, SEPA direct debit, Belfius Direct Net, KBC Payment Button and various gift cards such as Podiumcadeaukaart and fashioncheque.

60216.0M85](/packages/mollie-mollie-api-php)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
