2amigos/mailer

There is no license information available for the latest version (2.0.0) of this package.

Mailer adapter for the Symphony Mailer library with queuing capabilities.

2.0.0 2024-01-20 08:22 UTC

This package is auto-updated.

Last update: 2024-12-15 15:56:08 UTC


README

tests Codacy Badge Codacy Badge Latest Stable Version Total Downloads PHP Version Require

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

The preferred way to install this extension is through composer.

Either run

$ composer require 2amigos/mailer

or add

"2amigos/mailer": "^2.0"

to the require section of your composer.json file.

Usage

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

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 <b>well.</b>"
// 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:

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

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

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

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:

$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

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

Contributing

Please see CONTRIBUTING for details.

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

Sample with all options available:

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

Using code sniffer

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

Using code fixer

We have added a PHP code fixer to standardize our code. It includes Symfony, PSR-12 and some contributors rules.

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

Testing

$ ./vendor/bin/phpunit

Credits

License

The BSD License (BSD). Please see License File for more information.


web development has never been so fun
www.2amigos.us