From b6e26c52a3c3d88ab8ee3baee55807066f96a3d9 Mon Sep 17 00:00:00 2001 From: "honza@appsdevteam.com" Date: Fri, 6 Apr 2018 11:31:51 +0200 Subject: [PATCH 01/16] =?UTF-8?q?v=C3=BDm=C4=9Bna=20curl=20za=20guzzle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 +- src/Services/Api.php | 88 +++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index 1c0a03e..2651a9a 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ "minimum-stability": "stable", "require": { "php": ">=5.5.0", - "ext-curl": "*", "nette/mail": "^2.3", "tracy/tracy": "^2.3", "nette/di": "^2.3", - "nette/utils": "^2.3" + "nette/utils": "^2.3", + "guzzlehttp/guzzle": "^6.3" }, "require-dev": { }, diff --git a/src/Services/Api.php b/src/Services/Api.php index 174cb14..e7d4bea 100644 --- a/src/Services/Api.php +++ b/src/Services/Api.php @@ -3,54 +3,19 @@ namespace ADT\Mailer\Services; use ADT\Mailer\DI\AdtMailerExtension; +use GuzzleHttp\Client; class Api { /** @var array */ protected $config; - /** @var resource */ - protected $curl; - /** @var \Tracy\ILogger */ protected $logger; public function __construct(array $config, \Tracy\ILogger $logger) { $this->logger = $logger; $this->config = $config; - $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'] - ); - - // do not wait more than 3s - curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, 3000); - - if (PHP_VERSION_ID >= 50600) { - // follow redirects (throws 'CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set' on 5.5) - curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, TRUE); - } - - // disable cache, set content type, keep alive - curl_setopt( - $this->curl, - CURLOPT_HTTPHEADER, - [ - 'Cache-Control: no-cache', - 'Content-Type: application/octet-stream', - 'Connection: Keep-Alive', - 'Keep-Alive: 300', - 'Expect:', // do not send Expect: 100-continue - ] - ); - - // do not display result - curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); } protected function getSuppressionControlAddress(\Nette\Mail\Message $mail) { @@ -82,36 +47,41 @@ protected function serializeMessage(\Nette\Mail\Message $mail) { return $result; } + /** + * @param \Nette\Mail\Message $mail + * @return bool + * @throws \Exception + */ public function send(\Nette\Mail\Message $mail) { - $postData = \Nette\Utils\Json::encode($this->serializeMessage($mail)); - // set message - curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postData); + $postData = \GuzzleHttp\json_encode($this->serializeMessage($mail)); + $endPoint = rtrim($this->config['remote']['api'], '/'); - // send message - $response = curl_exec($this->curl); - $httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); + $client = new Client; try { - $status = \Nette\Utils\Json::decode($response); - - // success check - if (substr($httpCode, 0, 1) === '2' && $status->status === 'ok') { - return; + $client->request("POST", $endPoint . '/mail/send?key=' . $this->config['remote']['key'], [ + 'headers' => [ + 'Cache-Control'=> 'no-cache', + 'Content-Type' => 'application/octet-stream', + 'Connection' => 'Keep-Alive', + 'Keep-Alive' => '300', + 'Expect' => NULL, // do not send Expect: 100-continue + ], + "body" => $postData, + "timeout" => 3, + ]); + + return TRUE; + + } catch (\Exception $e) { + + if ($this->config['error']['mode'] === AdtMailerExtension::ERROR_MODE_EXCEPTION) { + throw $e; } - } catch (\Nette\Utils\JsonException $e) { - // error - } - $error = 'Could not transfer mail to remote server (' . ( - !empty($status) - ? $status->error - : $httpCode . ' ' . curl_error($this->curl) - ) . ').'; + $error = 'Could not transfer mail to remote server (' . $e->getMessage() . ').'; - if ($this->config['error']['mode'] === AdtMailerExtension::ERROR_MODE_EXCEPTION) { - throw new ApiException($error); - } else { // create log directory if (!file_exists($this->config['error']['logDir'])) { mkdir($this->config['error']['logDir'], 0777, TRUE); @@ -123,6 +93,8 @@ public function send(\Nette\Mail\Message $mail) { // send report $this->logger->log($error . PHP_EOL . PHP_EOL . $mailFile, \Tracy\ILogger::EXCEPTION); + + return FALSE; } } } From 5bf12897dcc803a8e619be1ddce8f3bdc6efaa60 Mon Sep 17 00:00:00 2001 From: "honza@appsdevteam.com" Date: Fri, 6 Apr 2018 12:09:19 +0200 Subject: [PATCH 02/16] =?UTF-8?q?odebr=C3=A1n=C3=AD=20z=C3=A1vislosti=20na?= =?UTF-8?q?=20nette=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 2651a9a..8c9e45b 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,6 @@ "nette/mail": "^2.3", "tracy/tracy": "^2.3", "nette/di": "^2.3", - "nette/utils": "^2.3", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { From ac0193d5925bc2269b48152f366580d47b24ed1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Mon, 24 Sep 2018 17:52:50 +0200 Subject: [PATCH 03/16] updated validation to accept callback started with @ --- src/DI/AdtMailerExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index 9d6193a..45f838c 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -65,7 +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($config['suppressionControlAddress']))) { + 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).'); } @@ -73,4 +73,4 @@ public function validateConfig(array $expected, array $config = NULL, $name = NU } -} \ No newline at end of file +} From 75a59969c1ae8492f4a8626bcfbdd956793b320e Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 23 Sep 2019 15:02:21 +0200 Subject: [PATCH 04/16] Nette 3.0 v composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8c9e45b..3c4e213 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ "minimum-stability": "stable", "require": { "php": ">=5.5.0", - "nette/mail": "^2.3", + "nette/mail": "^2.3 || ~3.0", "tracy/tracy": "^2.3", - "nette/di": "^2.3", + "nette/di": "^2.3 || ~3.0", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { From 286d88944702154390a559245ae0a49f0c2edec0 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 25 Sep 2019 15:19:15 +0200 Subject: [PATCH 05/16] Return typy pro overridy z nette/mail (3.0) --- composer.json | 6 +++--- src/DI/AdtMailerExtension.php | 2 +- src/Services/Mailer.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 3c4e213..4530477 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,10 @@ ], "minimum-stability": "stable", "require": { - "php": ">=5.5.0", - "nette/mail": "^2.3 || ~3.0", + "php": ">=7.1", + "nette/mail": "^~3.0", "tracy/tracy": "^2.3", - "nette/di": "^2.3 || ~3.0", + "nette/di": "^~3.0", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index 45f838c..b23205f 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -44,7 +44,7 @@ public function loadConfiguration() { ->setAutowired($config['autowireMailer']); } - public function validateConfig(array $expected, array $config = NULL, $name = NULL) { + public function validateConfig(array $expected, array $config = NULL, $name = NULL): array { $config = parent::validateConfig($expected, $config, $name); if (empty($config['remote']['api'])) { diff --git a/src/Services/Mailer.php b/src/Services/Mailer.php index c7696e3..ffcb4e7 100644 --- a/src/Services/Mailer.php +++ b/src/Services/Mailer.php @@ -13,7 +13,7 @@ public function __construct(Api $api) { $this->apiService = $api; } - function send(Message $mail) { + function send(Message $mail): void { $this->apiService->send($mail); } From 75dc7523bad637319550fc6685e0bb7b92328e30 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 30 Sep 2019 10:34:24 +0200 Subject: [PATCH 06/16] =?UTF-8?q?Typos=20v=20composer.json=20-=20"^~"=20v?= =?UTF-8?q?=20nette=20verz=C3=ADch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4530477..bd69fdb 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ "minimum-stability": "stable", "require": { "php": ">=7.1", - "nette/mail": "^~3.0", + "nette/mail": "~3.0", "tracy/tracy": "^2.3", - "nette/di": "^~3.0", + "nette/di": "~3.0", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { From 380701202f59ca003bd7afa2b08b7f31d836acc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Sun, 4 Oct 2020 07:09:19 +0200 Subject: [PATCH 07/16] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8c9e45b..2b1fef4 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ "minimum-stability": "stable", "require": { "php": ">=5.5.0", - "nette/mail": "^2.3", + "nette/mail": "^2.3 || ^3.0", "tracy/tracy": "^2.3", - "nette/di": "^2.3", + "nette/di": "^2.3 || ^3.0", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { From ea6a7320bddad34f08925b5eb1c9196a4858f697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Sun, 4 Oct 2020 10:01:43 +0200 Subject: [PATCH 08/16] Update AdtMailerExtension.php --- src/DI/AdtMailerExtension.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index 45f838c..7f11c75 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -44,7 +44,7 @@ public function loadConfiguration() { ->setAutowired($config['autowireMailer']); } - public function validateConfig(array $expected, array $config = NULL, $name = NULL) { + public function validateConfig(array $expected, array $config = NULL, $name = NULL) : array { $config = parent::validateConfig($expected, $config, $name); if (empty($config['remote']['api'])) { @@ -72,5 +72,4 @@ public function validateConfig(array $expected, array $config = NULL, $name = NU return $config; } - } From af6779ff1a585656e67b44c7142495f4bddbb674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Sun, 4 Oct 2020 10:02:02 +0200 Subject: [PATCH 09/16] Update Mailer.php --- src/Services/Mailer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Services/Mailer.php b/src/Services/Mailer.php index c7696e3..c2d0459 100644 --- a/src/Services/Mailer.php +++ b/src/Services/Mailer.php @@ -13,8 +13,8 @@ public function __construct(Api $api) { $this->apiService = $api; } - function send(Message $mail) { + function send(Message $mail): void { $this->apiService->send($mail); } -} \ No newline at end of file +} From 885f544936e994cdec8423d9cba7a8372e93efae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Fri, 26 Mar 2021 09:36:11 +0100 Subject: [PATCH 10/16] Update AdtMailerExtension.php --- src/DI/AdtMailerExtension.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index c313d18..a2324e0 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -65,10 +65,6 @@ 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; } From 95dd2832bdf2fb7e7d0a456f1f2e4d1b05c540ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Fri, 26 Mar 2021 09:56:58 +0100 Subject: [PATCH 11/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3504e69..c9e63b5 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ adtMailer: 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 ``` From 4acdc1fc2619d9313c534faa0b677c2e746bfcba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lohnisk=C3=BD?= Date: Fri, 26 Mar 2021 14:56:23 +0100 Subject: [PATCH 12/16] Update AdtMailerExtension.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V této chvíli je to ještě pole 2 stringů, ale callable to ještě není. --- src/DI/AdtMailerExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index a2324e0..0f1bee0 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']) || !(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 (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)) { From d40dc0d7c971173a1e6bdb70ccecc9172ac936de Mon Sep 17 00:00:00 2001 From: Marek Valuch Date: Mon, 28 Feb 2022 15:22:39 +0100 Subject: [PATCH 13/16] Guzzle ^7.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 937eeb7..6667240 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "nette/mail": "^2.3 || ^3.0", "tracy/tracy": "^2.3", "nette/di": "^2.3 || ^3.0", - "guzzlehttp/guzzle": "^6.3" + "guzzlehttp/guzzle": "^6.3|^7.0" }, "require-dev": { }, From e86df41126094f6fbfe56d208255d1811ca86d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1?= Date: Thu, 5 Jan 2023 11:36:03 +0100 Subject: [PATCH 14/16] Support header Reply-To --- src/Services/Api.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Services/Api.php b/src/Services/Api.php index bc1569e..ce748de 100644 --- a/src/Services/Api.php +++ b/src/Services/Api.php @@ -50,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; } @@ -106,4 +110,4 @@ public function send(\Nette\Mail\Message $mail) { } class ApiException extends \Nette\IOException { -} \ No newline at end of file +} From 3cd183cac585c40fc53107999e9c54e22c92ef46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Sun, 28 May 2023 07:54:23 +0200 Subject: [PATCH 15/16] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6667240..8219025 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "minimum-stability": "stable", "require": { - "php": ">=7.1", - "nette/mail": "^2.3 || ^3.0", + "php": ">=7.4", + "nette/mail": "^2.3 | ^3.0 | ^4.0", "tracy/tracy": "^2.3", "nette/di": "^2.3 || ^3.0", "guzzlehttp/guzzle": "^6.3|^7.0" From 40dccd0fb51f790b15a0c73eed95f741ded794f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Sun, 26 Jan 2025 13:22:47 +0100 Subject: [PATCH 16/16] Update AdtMailerExtension.php --- src/DI/AdtMailerExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DI/AdtMailerExtension.php b/src/DI/AdtMailerExtension.php index 0f1bee0..d9d3af7 100644 --- a/src/DI/AdtMailerExtension.php +++ b/src/DI/AdtMailerExtension.php @@ -44,7 +44,7 @@ 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'])) {