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); + } }