diff --git a/README.md b/README.md index 961b3d4..b816353 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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 @@ -148,3 +148,14 @@ sparkpost_delete_supression('test@example.com'); ```php sparkpost_check_email('test@example.com'); ``` + +## 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); +}); +``` diff --git a/src/Transport/SparkPostTransport.php b/src/Transport/SparkPostTransport.php index 8fce4f6..5338272 100644 --- a/src/Transport/SparkPostTransport.php +++ b/src/Transport/SparkPostTransport.php @@ -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' => [ @@ -227,4 +231,4 @@ public function deleteSupression($email): JsonResponse ]); } } -} \ No newline at end of file +}