Mail Service
Mail template and queueing service.
Installation
You can install this package via packagist.org with composer.
composer require systream/mail-service
composer.json:
"require": {
"systream/mail-service": "*"
}
This library requires php 5.6
or higher.
Usage examples
MailQueue
Create a simple mailQueue item with factory.
$item = Mail\MailQueueItem\MailQueueItemFactory::make('subject', 'Message for {$name}', 'recipient@email.hu', 'Recipent Name', array('name' => 'John'));
Formatters
You can add message formatters to a mailqueue item. For example a token formatter, which can replace the given tokens in the message and the subject.
You can add multiple message formatter.
$tokenFormatter = new TokenFormatter($tokens);
// ...
$mailQueueItem = new MailQueueItem($message);
$mailQueueItem->addMessageFormatter($tokenFormatter);
Mail templates
Mail templates gives the basic content of the message.
This library provides a simple StringMailTemplate object, but you can easily add your custom. The only thing you needed to do is implement MailTemplateInterface
.
$mailTemplate = new StringMailTemplate($body, $subject);
Recipients
One or more recipient can be added to the message.
$message = new Message($mailTemplate);
$message->addRecipient(new Recipient($recipientEmail, $recipientName));
Custom mailqueue item
$tokenFormatter = new TokenFormatter($tokens);
$mailTemplate = new StringMailTemplate($body, $subject);
$message = new Message($mailTemplate);
$message->addRecipient(new Recipient($recipientEmail, $recipientName));
$mailQueueItem = new MailQueueItem($message);
$mailQueueItem->addMessageFormatter($tokenFormatter);
Send message
You need a mailer and a Queue hadler.
Mail sender
This repository provides a phpmailler adapter for MailSenderInterface
$PHPMailer = new \PHPMailer(true);
$PHPMailer->isSMTP();
$PHPMailer->Host = "127.0.0.1";
$PHPMailer->Port = 1025;
$PHPMailer->SetFrom('test@unit.test');
$mailer = new Mail\MailSender\PHPMailerAdapter($PHPMailer);
Queue handler
Sqlite queue handler
$mail = new Mail($mailer, new Mail\QueueHandler\SqliteQueHandlerAdapter());
Sending immediate
$mail->send($item);
Add item to queue
$mail->queue($item);
Process Queue
$mail->consume();
Set logger
You can use any PSR-3 compatible logger, for example monolog.
$mail->setLogger($logger);