Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# BackgroundQueueMailer

Delegates sending emails to [adt/background-queue](https://github.com/AppsDevTeam/BackgroundQueue).

## Installation
composer:
```
composer require adt/background-queue-mailer
```

If you're not familiar with [adt/background-queue](https://github.com/AppsDevTeam/BackgroundQueue),
you should head there in the first place.

## Usage

To use BackgroundQueueMailer as buffer between your application and SmtpMailer, register
it in your `config.neon`:

```neon
services:
smtpMailer:
class: \Nette\Mail\SmtpMailer
autowired: no # this is important

nette.mailer: \ADT\Mail\BackgroundQueueMailer(@smtpMailer, 'backgroundMail')

backgroundQueue:
callbacks:
backgroundMail: @nette.mailer::process
```

where `@smtpMailer` is outgoing mailer, and `backgroundMail` is unique callback name.

Callback name has to be same in both mailer definition and BackgroundQueue callback list. If they
are not, warning is logged using Tracy. This should get resolved [here](https://github.com/AppsDevTeam/BackgroundQueue/issues/8).

The `autowired: no` option is important because Nette DI container would not know
which `\Nette\Mail\IMailer` to inject in your application. By setting `autowired: no` on
SMTP mailer only one instance of `IMailer` interface remains.

You cannot set `autowired: no` on `nette.mailer` because your application
would not be able to inject it.

It is also important that you autowire `\Nette\Mail\IMailer` throughout your application.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
},
"minimum-stability": "stable",
"require": {
"adt/background-queue": "^3.1"
"adt/background-queue": "^3.1",
"tracy/tracy": "^2.3"
},
"autoload": {
"psr-4": {
"ADT\\Mail\\BackgroundQueueMailer\\": "src/"
}
}
}
64 changes: 64 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace ADT\Mail\BackgroundQueueMailer;

use ADT\BackgroundQueue;
use Nette\Mail;
use Nette\Utils\JSON;
use Tracy\Debugger;


class Mailer extends \Nette\Object implements Mail\IMailer {

/** @var Mail\IMailer */
protected $next;

/** @var string */
protected $callbackName;

/** @var BackgroundQueue\Service */
protected $backgroundQueueService;

public function __construct(
Mail\IMailer $next,
$callbackName,
BackgroundQueue\Service $backgroundQueueService
) {
$this->next = $next;
$this->callbackName = $callbackName;
$this->backgroundQueueService = $backgroundQueueService;
}

public function send(Mail\Message $mail) {
$entity = new BackgroundQueue\Entity\QueueEntity;
$entity->setCallbackName($this->callbackName);
$entity->setParameters([
// Parameters are stored as LONGTEXT UTF-8, so they cannot contain binary data.
// This should be fine if we encode mail as JSON.
'mail' => JSON::encode(serialize($mail)),
]);

$this->backgroundQueueService
->publish($entity);
}

public function process(BackgroundQueue\Entity\QueueEntity $entity) {
if ($entity->getCallbackName() !== $this->callbackName) {
Debugger::log("Callback names do not match, expected: '{$this->callbackName}' but got: '{$entity->getCallbackName()}'; skipping'", Debugger::WARNING);
return FALSE; // repeatable error
}

$parameters = $entity->getParameters();
$mail = unserialize(JSON::decode($parameters['mail']));

try {
$this->next->send($mail);
return TRUE; // done
} catch (Mail\SendException $e) {
return FALSE; // repeatable error
}

// everything else is unrepeatable error (logged in BackgroundQueue)
}

}