Skip to content

Commit

Permalink
Merge pull request #150 from nickvanderveeken/feature/sparkpost-retur…
Browse files Browse the repository at this point in the history
…n-path

Enable SparkPost return path
  • Loading branch information
roelvanduijnhoven authored Dec 13, 2021
2 parents e87a884 + 2f99093 commit 0a80550
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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 0a80550

Please sign in to comment.