PHPackages                             2amigos/mailer - 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. 2amigos/mailer

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

2amigos/mailer
==============

Mailer adapter for the Symphony Mailer library with queuing capabilities.

2.0.0(2y ago)502.5k3[4 issues](https://github.com/2amigos/mailer-library/issues)[1 PRs](https://github.com/2amigos/mailer-library/pulls)PHPPHP &gt;=7.4

Since Jan 18Pushed 2y ago5 watchersCompare

[ Source](https://github.com/2amigos/mailer-library)[ Packagist](https://packagist.org/packages/2amigos/mailer)[ RSS](/packages/2amigos-mailer/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (12)Versions (12)Used By (0)

Mailer
======

[](#mailer)

[![tests](https://github.com/2amigos/mailer-library/actions/workflows/ci.yml/badge.svg)](https://github.com/2amigos/mailer-library/actions/workflows/ci.yml)[![Codacy Badge](https://camo.githubusercontent.com/bfb510cd50bc4d6d84597ca482d4dbe8b6537976d1e0ea328f3c9ac8613a7f40/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6430653864366539363839343435393261393561303931316438663137386666)](https://app.codacy.com/gh/2amigos/mailer-library/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)[![Codacy Badge](https://camo.githubusercontent.com/6f614d367686509a8905d0ec55158a3946b1f42cc96eac0b31323fa977e99e1c/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f6430653864366539363839343435393261393561303931316438663137386666)](https://app.codacy.com/gh/2amigos/mailer-library/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)[![Latest Stable Version](https://camo.githubusercontent.com/c09e1703940b4b7addc2a12d1356e63b23260e166f9bed5f6be98195a635fd8c/687474703a2f2f706f7365722e707567782e6f72672f32616d69676f732f6d61696c65722f76)](https://packagist.org/packages/2amigos/mailer)[![Total Downloads](https://camo.githubusercontent.com/8a202228a2759e29d6f0ae9722ffd6c5f7e71d7a5e98dcb107c4094260a8a2bd/687474703a2f2f706f7365722e707567782e6f72672f32616d69676f732f6d61696c65722f646f776e6c6f616473)](https://packagist.org/packages/2amigos/mailer)[![PHP Version Require](https://camo.githubusercontent.com/5c438b3f94664a4601e4210779b7367d137577ad8a860e8fc190d775be38fd08/687474703a2f2f706f7365722e707567782e6f72672f32616d69676f732f6d61696c65722f726571756972652f706870)](https://packagist.org/packages/2amigos/mailer)

Many times we face a requirement to implement queue mail functionality in our projects. There are queue and
mailing libraries, but there seemed to be none that could actually suit our needs and moreover, we always had to sync their functionality together.

The `Mailer` library was built to fill the gaps that we have faced when implementing queue and/or mailing systems. It features:

- message encryption/decryption just in case a mail message contains data that should not be publicly exposed. Perfect for SAS systems.
- queueing on different backends (currently supporting beanstalkd, pdo, redis, sqs and rabbitmq) so we are not forced to use a queue storage due to the narrowed capabilities of the framework and/or libraries
- unified system. Basic to Middle size projects do have mailing but they do not require another type of queue system. That's the reason the queue system is not standalone and is coupled with this system.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require 2amigos/mailer
```

or add

```
"2amigos/mailer": "^2.0"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

Configuration
-------------

[](#configuration)

All the configuration needed to set up the message broker connections as the mailer transport should be performed on the .env file. A .env.example file is provided, you can just copy it and start over!

```
$ cp .evn.example .env
```

The keys `MESSAGE_BROKER` and `MAIL_TRANSPORT` defines the default message broker and mail transport, and they are mandatory to be filled. By default, its set to use Redis broker with SMTP transport.

You can access the related configuration values by calling:

```
$values = \Da\Mailer\Helper\ConfigReader::get(); // array
```

Mail Messages
-------------

[](#mail-messages)

The `MailMessage` class is an abstraction for an email content. Beside the attachments, you can specify the email content directly by the constructor or directly accessor.

```
$message = new \Da\Mailer\Model\MailMessage([
    'from' => 'sarah.connor@gmail.com',
    'to' => 'john.connor@gmail.com',
    'subject' => 'What is up?',
    'textBody' => 'I hope to find you well...'
]);

// or
$message->bodyHtml = "I hope I'm finding doing well."
// body html takes priority over body text with both were set.
```

You can also use our `EmailAddress` class to define emails with related name:

```
$message->cc = [
    \Da\Mailer\Mail\Dto\EmailAddress::make('Samn@email.com', 'Samantha');
    \Da\Mailer\Mail\Dto\EmailAddress::make('oliver@email.com', 'Oliver');
];
```

And to add attachments, you can make use of the method `addAttachment(path, name)`:

```
$message->addAttachment(__DIR__ . DIRECTORY_SEPARATOR . 'file-test.pdf', 'Important File.png');
```

Also, you can set text or html body as a resource path.

```
$message->bodyHtml = __DIR__ . DIRECTORY_SEPARATOR . 'html-template.html';
```

### Available public properties:

[](#available-public-properties)

PropertyTypefromstring, arraytostring, arrayccstring, arraybccstring, arraysubjectstringbodyTextstringbodyHtmlstring### enqueue MailMessage

[](#enqueue-mailmessage)

You can easily assess the message enqueue by calling the method `enqueue`.

```
$message->enqueue();
```

The message will enqueued to the default message broker, and use the default transport.

MailJob
-------

[](#mailjob)

The MailJob class will abstract the message behavior for our queue application. You can create a new MailJob with the `MailJobBuilder` class:

```
$mailJob = \Da\Mailer\Builder\MailJobBuilder::make([
    'message' => json_encode($message)
]);
```

Behind the hoods, the builder will build the MailJob specialized to the default broker you've defined on your .env file. If you ever want a mail job to be created to a different broker than your default, you can set it as the second argument, using one value from the `\Da\Mailer\Enum\MessageBrokerEnum` enum:

```
$mailJob = \Da\Mailer\Builder\MailJobBuilder::make([
        'message' => json_encode($message)
    ],
    \Da\Mailer\Enum\MessageBrokerEnum::BROKER_SQS
);
```

The MailJob class has a set of methods to manipulate it's content and also to check its status. The next piece of code cover them all:

```
$mailJob->getMessage(); // returns the MailJob message
$mailJob->markAsCompleted(); // void, mark the job as completed
$mailJob->isCompleted(); // returns true if the job has been complete
$mailJob->setMessage(new \Da\Mailer\Model\MailMessage()); // change the job's message
```

Mailer
------

[](#mailer-1)

The Mailer class is the one we use for sending the emails.

```
$message = new \Da\Mailer\Model\MailMessage([
    'from' => 'sarah.connor@gmail.com',
    'to' => 'john.connor@gmail.com',
    'subject' => 'What is up?',
    'textBody' => 'I hope to find you well...'
]);

$mailer = \Da\Mailer\Builder\MailerBuilder::make();
// or if you want to set a transport different from the default
$mailer = \Da\Mailer\Builder\MailerBuilder::make(\Da\Mailer\Enum\TransportType::SEND_MAIL);
$mailer->send($message); // returns \Symfony\Component\Mailer\SentMessage::class|null
```

Queues
------

[](#queues)

To create a queue, you can make use of our `QueueBuilder` class. It will return a queue object with a few methods to handle the queue. They are:

- [enqueue(MailJob $job)](docs/queue/methods.md#enqueue): bool
- [dequeue()](docs/queue/methods.md#dequeue): mailjob
- [ack(MailJob $job)](docs/queue/methods.md#ack): void
- [isEmpty()](docs/queue/methods.md#isempty): bool

```
$queue = \Da\Mailer\Builder\QueueBuilder::make();

// if you want to use a different broker than the default
$queue = \Da\Mailer\Builder\QueueBuilder::make(\Da\Mailer\Enum\MessageBrokerEnum::BROKER_RABBITMQ);
```

Advanced usage
--------------

[](#advanced-usage)

If you want to handle your message broker and smtp manually, you can follow through the following topics:

- [Beanstalkd Backend](docs/advanced-usage/bt.md)
- [Pdo Backend](docs/advanced-usage/pdo.md)
- [RabbitMq Backend](docs/advanced-usage/rabbitmq.md)
- [Redis Backend](docs/advanced-usage/redis.md)
- [SQS Backend](docs/advanced-usage/sqs.md)

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Clean code
----------

[](#clean-code)

We have added some development tools for you to contribute to the library with clean code:

- PHP mess detector: Takes a given PHP source code base and look for several potential problems within that source.
- PHP code sniffer: Tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
- PHP code fixer: Analyzes some PHP source code and tries to fix coding standards issues.

And you should use them in that order.

### Using php mess detector

[](#using-php-mess-detector)

Sample with all options available:

```
 ./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode
```

### Using code sniffer

[](#using-code-sniffer)

```
 ./vendor/bin/phpcs -s --report=source --standard=PSR2 ./src
```

### Using code fixer

[](#using-code-fixer)

We have added a PHP code fixer to standardize our code. It includes Symfony, [PSR-12](https://www.php-fig.org/psr/psr-12/) and some contributors rules.

```
./vendor/bin/php-cs-fixer --config-file=.php_cs fix ./src
```

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

Credits
-------

[](#credits)

- [Antonio Ramirez](https://github.com/tonydspaniard)
- [All Contributors](https://github.com/2amigos/mailer-library/graphs/contributors)

License
-------

[](#license)

The BSD License (BSD). Please see [License File](LICENSE.md) for more information.

> [![](https://camo.githubusercontent.com/9fd8f1de41dc23003bb2a54034cb6658dde5be97092e195a62d629d0d7fa7f6c/687474703a2f2f7777772e67726176617461722e636f6d2f6176617461722f35353336333339346437323934356666376564333132353536656330343165302e706e67)](http://www.2amigos.us)
>  *web development has never been so fun*
>  [www.2amigos.us](http://www.2amigos.us)

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 70.7% 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 ~487 days

Recently: every ~676 days

Total

7

Last Release

892d ago

Major Versions

1.0.5 → 2.0.02024-01-20

PHP version history (2 changes)1.0.0PHP &gt;=5.6.4

2.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/566016?v=4)[Antonio Ramirez](/maintainers/tonydspaniard)[@tonydspaniard](https://github.com/tonydspaniard)

---

Top Contributors

[![2amjsouza](https://avatars.githubusercontent.com/u/142612126?v=4)](https://github.com/2amjsouza "2amjsouza (41 commits)")[![resurtm](https://avatars.githubusercontent.com/u/100198?v=4)](https://github.com/resurtm "resurtm (16 commits)")[![amigo-tabin](https://avatars.githubusercontent.com/u/3017365?v=4)](https://github.com/amigo-tabin "amigo-tabin (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/2amigos-mailer/health.svg)

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

###  Alternatives

[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[magento/community-edition

Magento 2 (Open Source)

12.1k53.0k12](/packages/magento-community-edition)[shopware/platform

The Shopware e-commerce core

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

Shopware platform is the core for all Shopware ecommerce products.

585.4M524](/packages/shopware-core)[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[oro/platform

Business Application Platform (BAP)

642140.7k104](/packages/oro-platform)

PHPackages © 2026

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