Skip to content

Commit

Permalink
Enable use of retun_path on SparkPost through get/SetReturnPath on Sp…
Browse files Browse the repository at this point in the history
…arkPost messages
  • Loading branch information
nickvanderveeken committed Dec 13, 2021
1 parent e87a884 commit 4fe4df0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Mail/Message/SparkPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Service/SparkPostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
Expand Down
27 changes: 27 additions & 0 deletions tests/SlmMailTest/Service/SparkPostServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]');
$message->setReturnPath(null);
$this->assertNull($message->getReturnPath());

// nullify empty string
$message->setReturnPath('');
$this->assertNull($message->getReturnPath());

// regular use
$message->setReturnPath('[email protected]');
$this->assertEquals('[email protected]', $message->getReturnPath());

// successful transmission injection
$sparkPostServiceMock = $this->expectApiResponse(200);
$sparkPostServiceMock->send($message);
}
}

0 comments on commit 4fe4df0

Please sign in to comment.