diff --git a/README.md b/README.md index 82f8da7..3504e69 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,18 @@ 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 suppressionControlAddress: @App\Model\SuppressionControl::decide diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index 9d6193a..49a7745 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -51,8 +51,8 @@ public function validateConfig(array $expected, array $config = NULL, $name = NU 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_callable($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)) { diff --git a/src/Services/Api.php b/src/Services/Api.php index 174cb14..215fc4b 100644 --- a/src/Services/Api.php +++ b/src/Services/Api.php @@ -18,6 +18,44 @@ class Api { public function __construct(array $config, \Tracy\ILogger $logger) { $this->logger = $logger; $this->config = $config; + } + + protected function getSuppressionControlAddress(\Nette\Mail\Message $mail) { + return $this->processCallableOption($this->config['suppressionControlAddress'], $mail); + } + + protected function getRemoteKey(\Nette\Mail\Message $mail) { + return $this->processCallableOption($this->config['ip_pool'], $mail); + } + + protected function processCallableOption($value, \Nette\Mail\Message $mail) { + if (is_callable($value, FALSE)) { + return $value($mail); + } else { + return $value; + } + } + + /** + * @param \Nette\Mail\Message $mail + * @return array + */ + protected function serializeMessage(\Nette\Mail\Message $mail) { + $result = [ + 'from' => $mail->getFrom(), + 'subject' => $mail->getSubject(), + 'message' => $mail->generateMessage(), + 'suppressionControlAddress' => $this->getSuppressionControlAddress($mail), + ]; + + foreach ([ 'to', 'cc', 'bcc' ] as $header) { + $result[$header] = $mail->getHeader(ucfirst($header)); + } + + return $result; + } + + public function send(\Nette\Mail\Message $mail) { $this->curl = curl_init(); // set remote URL @@ -25,7 +63,7 @@ public function __construct(array $config, \Tracy\ILogger $logger) { curl_setopt( $this->curl, CURLOPT_URL, - $endPoint . '/mail/send?key=' . $this->config['remote']['key'] + $endPoint . '/mail/send?key=' . $this->getRemoteKey() ); // do not wait more than 3s @@ -51,38 +89,7 @@ public function __construct(array $config, \Tracy\ILogger $logger) { // do not display result curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); - } - - protected function getSuppressionControlAddress(\Nette\Mail\Message $mail) { - $address = $this->config['suppressionControlAddress']; - if (is_callable($address, FALSE)) { - return $address($mail); - } else { - return $address; - } - } - - /** - * @param \Nette\Mail\Message $mail - * @return array - */ - protected function serializeMessage(\Nette\Mail\Message $mail) { - $result = [ - 'from' => $mail->getFrom(), - 'subject' => $mail->getSubject(), - 'message' => $mail->generateMessage(), - 'suppressionControlAddress' => $this->getSuppressionControlAddress($mail), - ]; - - foreach ([ 'to', 'cc', 'bcc' ] as $header) { - $result[$header] = $mail->getHeader(ucfirst($header)); - } - - return $result; - } - - public function send(\Nette\Mail\Message $mail) { $postData = \Nette\Utils\Json::encode($this->serializeMessage($mail)); // set message