Skip to content

Commit

Permalink
remote.key can be method
Browse files Browse the repository at this point in the history
  • Loading branch information
michallohnisky committed Nov 13, 2020
1 parent 43f404d commit 7352206
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/DI/AdtMailerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
71 changes: 39 additions & 32 deletions src/Services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,52 @@ 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
$endPoint = rtrim($this->config['remote']['api'], '/');
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
Expand All @@ -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
Expand Down

0 comments on commit 7352206

Please sign in to comment.