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.
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.
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
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' => '[email protected]',
'to' => '[email protected]',
'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('[email protected]', 'Samantha');
\Da\Mailer\Mail\Dto\EmailAddress::make('[email protected]', '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';
Property | Type |
---|---|
from | string, array |
to | string, array |
cc | string, array |
bcc | string, array |
subject | string |
bodyText | string |
bodyHtml | string |
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.
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
The Mailer class is the one we use for sending the emails.
$message = new \Da\Mailer\Model\MailMessage([
'from' => '[email protected]',
'to' => '[email protected]',
'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
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): bool
- dequeue(): mailjob
- ack(MailJob $job): void
- 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);
If you want to handle your message broker and smtp manually, you can follow through the following topics:
Please see CONTRIBUTING for details.
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.
Sample with all options available:
./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode
./vendor/bin/phpcs -s --report=source --standard=PSR2 ./src
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
$ ./vendor/bin/phpunit
The BSD License (BSD). Please see License File for more information.
web development has never been so fun
www.2amigos.us