diff --git a/README.md b/README.md index 82f8da7..c9e63b5 100644 --- a/README.md +++ b/README.md @@ -20,15 +20,20 @@ extensions: adtMailer: remote: api: yourAdtMailApiInstance.com:1234 + + # can be either static string or method, required key: yourPrivateKey + error: # mode: silent => log and continue # mode: exception => throw mode: silent + # all undelivered messages are stored here (applies to mode: silent) logDir: %logDir%/adt_mailer + # if recipient is suppressed, this address receives notification and delist link - # can be either static string or method, required + # can be either an email address, url or a callback returning an email address or url suppressionControlAddress: @App\Model\SuppressionControl::decide ``` diff --git a/composer.json b/composer.json index bd69fdb..8219025 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,11 @@ ], "minimum-stability": "stable", "require": { - "php": ">=7.1", - "nette/mail": "~3.0", + "php": ">=7.4", + "nette/mail": "^2.3 | ^3.0 | ^4.0", "tracy/tracy": "^2.3", - "nette/di": "~3.0", - "guzzlehttp/guzzle": "^6.3" + "nette/di": "^2.3 || ^3.0", + "guzzlehttp/guzzle": "^6.3|^7.0" }, "require-dev": { }, diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index b23205f..d9d3af7 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -44,15 +44,15 @@ public function loadConfiguration() { ->setAutowired($config['autowireMailer']); } - public function validateConfig(array $expected, array $config = NULL, $name = NULL): array { + public function validateConfig(array $expected, ?array $config = NULL, $name = NULL): array { $config = parent::validateConfig($expected, $config, $name); if (empty($config['remote']['api'])) { throw new \Nette\UnexpectedValueException('Specify remote API endpoint.'); } - if (empty($config['remote']['key'])) { - throw new \Nette\UnexpectedValueException('Specify authentication key.'); + if (empty($config['remote']['key']) || !(is_string($config['remote']['key']) || is_array($config['remote']['key']))) { + throw new \Nette\UnexpectedValueException('Specify authentication key as string or method (e.g. [@ServiceClass, method]).'); } if (!in_array($config['error']['mode'], static::errorModes(), TRUE)) { @@ -65,12 +65,7 @@ public function validateConfig(array $expected, array $config = NULL, $name = NU throw new \Nette\UnexpectedValueException('Specify mail log directory.'); } - if (empty($config['suppressionControlAddress']) || !(is_string($config['suppressionControlAddress']) || is_callable(array_map(function($value) { return ltrim($value, '@'); }, $config['suppressionControlAddress'])))) { - throw new \Nette\UnexpectedValueException('Specify suppression control address as string or method (e.g. @ServiceClass::method).'); - } - return $config; } - } diff --git a/src/Services/Api.php b/src/Services/Api.php index e7d4bea..ce748de 100644 --- a/src/Services/Api.php +++ b/src/Services/Api.php @@ -19,12 +19,18 @@ public function __construct(array $config, \Tracy\ILogger $logger) { } protected function getSuppressionControlAddress(\Nette\Mail\Message $mail) { - $address = $this->config['suppressionControlAddress']; + return $this->processCallableOption($this->config['suppressionControlAddress'], $mail); + } + + protected function getRemoteKey(\Nette\Mail\Message $mail) { + return $this->processCallableOption($this->config['remote']['key'], $mail); + } - if (is_callable($address, FALSE)) { - return $address($mail); + protected function processCallableOption($value, \Nette\Mail\Message $mail) { + if (is_callable($value, FALSE)) { + return $value($mail); } else { - return $address; + return $value; } } @@ -44,6 +50,10 @@ protected function serializeMessage(\Nette\Mail\Message $mail) { $result[$header] = $mail->getHeader(ucfirst($header)); } + if ($mail->getHeader('Reply-To') !== null) { + $result['reply-to'] = $mail->getHeader('Reply-To'); + } + return $result; } @@ -60,7 +70,7 @@ public function send(\Nette\Mail\Message $mail) { $client = new Client; try { - $client->request("POST", $endPoint . '/mail/send?key=' . $this->config['remote']['key'], [ + $client->request("POST", $endPoint . '/mail/send?key=' . $this->getRemoteKey($mail), [ 'headers' => [ 'Cache-Control'=> 'no-cache', 'Content-Type' => 'application/octet-stream', @@ -100,4 +110,4 @@ public function send(\Nette\Mail\Message $mail) { } class ApiException extends \Nette\IOException { -} \ No newline at end of file +} diff --git a/src/Services/Mailer.php b/src/Services/Mailer.php index ffcb4e7..c2d0459 100644 --- a/src/Services/Mailer.php +++ b/src/Services/Mailer.php @@ -17,4 +17,4 @@ function send(Message $mail): void { $this->apiService->send($mail); } -} \ No newline at end of file +}