Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subaccount #25

Merged
merged 7 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SparkPost EU: `https://api.eu.sparkpost.com/api/v1`

**Guzzle options**

You are able to specify [Guzzle options](http://docs.guzzlephp.org/en/stable/request-options.html) in the SparkPost config section `guzzle`.
You are able to specify [Guzzle options](http://docs.guzzlephp.org/en/stable/request-options.html) in the SparkPost config section `guzzle`.

Just include the additional configuration in your `config/services.php`.

Expand Down Expand Up @@ -105,7 +105,7 @@ SPARKPOST_SECRET=__Your_key_here__

**3. Set Mail Driver**

You need to set your mail driver to SparkPost.
You need to set your mail driver to SparkPost.

You can do this by setting the environment variable `MAIL_MAILER` in your `.env` file

Expand Down Expand Up @@ -148,3 +148,14 @@ sparkpost_delete_supression('[email protected]');
```php
sparkpost_check_email('[email protected]');
```

## Mail Subaccounts

To send an email using a [SparkPost mail subaccount](https://support.sparkpost.com/docs/user-guide/subaccounts), add the desired subaccount id to the message header before sending:
```php
$subaccount_id = 1234;
$this->withSymfonyMessage(function ($message) use ($subaccount_id) { // 'this' is a mailable
$headers = $message->getHeaders();
$headers->addTextHeader('subaccount_id', $subaccount_id);
});
```
12 changes: 8 additions & 4 deletions src/Transport/SparkPostTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
{
$recipients = $this->getRecipients($message);

$headers = ['Authorization' => $this->key];
$subaccount_id = $message->getHeaders()->get('subaccount_id');
if ($subaccount_id) {
$headers['X-MSYS-SUBACCOUNT'] = $subaccount_id->getValue();
}

$response = $this->client->request('POST', $this->getEndpoint() . '/transmissions', [
'headers' => [
'Authorization' => $this->key,
],
'headers' => $headers,
'json' => array_merge([
'recipients' => $recipients,
'content' => [
Expand Down Expand Up @@ -227,4 +231,4 @@ public function deleteSupression($email): JsonResponse
]);
}
}
}
}