From 4fe4df0444aed2b6dbe9a3b01dc57bb694bdc527 Mon Sep 17 00:00:00 2001 From: Nick van der Veeken Date: Mon, 13 Dec 2021 13:51:52 +0100 Subject: [PATCH 1/2] Enable use of retun_path on SparkPost through get/SetReturnPath on SparkPost messages --- src/Mail/Message/SparkPost.php | 31 ++++++++++++++++++- src/Service/SparkPostService.php | 4 +++ .../Service/SparkPostServiceTest.php | 27 ++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Mail/Message/SparkPost.php b/src/Mail/Message/SparkPost.php index 1b4803e..ebb9dec 100644 --- a/src/Mail/Message/SparkPost.php +++ b/src/Mail/Message/SparkPost.php @@ -38,10 +38,18 @@ class SparkPost extends Message protected $campaignId = null; /** - * Array of attachments. Each attachment has a name, type and base64-encoded data-string without line breaks. + * @var array $attachments Array of attachments. Each attachment has a name, type and base64-encoded data-string without line breaks. */ protected $attachments = []; + /** + * @var string|null $returnPath + */ + protected $returnPath = null; + + /** + * @param array $options + */ public function __construct(array $options = []) { $this->setOptions($options); @@ -235,4 +243,25 @@ public function addAttachment(string $name, string $type, string $data): void 'data' => str_replace(["\r", "\n"], '', $data), ]; } + + /** + * Set the return path for this message. SparkPost will overwrite the local part (blabla@...) + * of the address, unless you have an enterprise account. + * @param string|null $returnPath Must be valid email address. + * @return $this + */ + public function setReturnPath(?string $returnPath): SparkPost + { + $this->returnPath = $returnPath ?: null; + return $this; + } + + /** + * Get the return path for this message. + * @return string|null The configured return path, or `null` if no return path was set + */ + public function getReturnPath(): ?string + { + return $this->returnPath; + } } diff --git a/src/Service/SparkPostService.php b/src/Service/SparkPostService.php index 631e354..b6f83db 100644 --- a/src/Service/SparkPostService.php +++ b/src/Service/SparkPostService.php @@ -98,6 +98,10 @@ public function send(Message $message): array $post['campaign_id'] = $message->getCampaignId(); } + if($message instanceof SparkPostMessage && $message->getReturnPath()) { + $post['return_path'] = $message->getReturnPath(); + } + $response = $this->prepareHttpClient('/transmissions', $post) ->send() ; diff --git a/tests/SlmMailTest/Service/SparkPostServiceTest.php b/tests/SlmMailTest/Service/SparkPostServiceTest.php index fa5ba37..2dc56ef 100644 --- a/tests/SlmMailTest/Service/SparkPostServiceTest.php +++ b/tests/SlmMailTest/Service/SparkPostServiceTest.php @@ -371,4 +371,31 @@ public function testCampaignId() $sparkPostServiceMock = $this->expectApiResponse(200); $sparkPostServiceMock->send($message); } + + + public function testReturnPath() + { + /** @var SparkPost $message */ + $message = $this->getMessageObject(); + + // default value is null + $this->assertNull($message->getReturnPath()); + + // accepts null-value as a way to unset the return path + $message->setReturnPath('non-null-value@example.com'); + $message->setReturnPath(null); + $this->assertNull($message->getReturnPath()); + + // nullify empty string + $message->setReturnPath(''); + $this->assertNull($message->getReturnPath()); + + // regular use + $message->setReturnPath('recipient@example.com'); + $this->assertEquals('recipient@example.com', $message->getReturnPath()); + + // successful transmission injection + $sparkPostServiceMock = $this->expectApiResponse(200); + $sparkPostServiceMock->send($message); + } } From 2f99093f1a89d8f185b19b5f00239bf0584e708f Mon Sep 17 00:00:00 2001 From: Nick van der Veeken Date: Mon, 13 Dec 2021 13:52:17 +0100 Subject: [PATCH 2/2] Update GuzzleHTTP to latest stable version 7.4.x --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f10becd..c93655e 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ }, "require-dev": { "container-interop/container-interop": "^1.1", - "guzzlehttp/guzzle": "^6.5", + "guzzlehttp/guzzle": "^7.4", "laminas/laminas-modulemanager": "^2.8", "laminas/laminas-mvc": "^3.1", "laminas/laminas-view": "^2.11",