Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vilfago's avatar
Level 20

Change sparkpost default API uri to Sparkpost.eu

Hi all,

I just install email sending with Sparkpost (EU app, for some reasons), and I was able to make it work in modifying the send() function in SparkPostTransport.php (\vendor\laravel\framework\src\Illuminate\Mail\Transport)

from https://api.sparkpost.com/api/v1/transmissions to https://api. eu .sparkpost.com/api/v1/transmissions

But I think it will be overrided at my next upgrade... How can I adjust the API uri outside of the vendor folder ?

I cannot manage it mail by mail, as I use the standard auth route of Laravel.

Thank you for your help !

0 likes
4 replies
Vilfago's avatar
Vilfago
OP
Best Answer
Level 20

Seems that when I search for the right keyword (custom mail driver), I find more answer :)

https://stackoverflow.com/questions/44901912/creating-own-mail-provider-for-laravel-5-4 It was a good input, but don't perfectly work for Laravel 5.6.14

Below, how I did it :

app\Mail folder

new SparkPostEUTransport.php

namespace App\Mail;

use Swift_Mime_SimpleMessage;
use GuzzleHttp\ClientInterface;
use Illuminate\Mail\Transport\SparkPostTransport;

class SparkPostEUTransport extends SparkPostTransport
{
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
    {
        $this->beforeSendPerformed($message);

        $recipients = $this->getRecipients($message);

        $message->setBcc([]);

        $response = $this->client->post('https://api.eu.sparkpost.com/api/v1/transmissions', [
            'headers' => [
                'Authorization' => $this->key,
            ],
            'json' => array_merge([
                'recipients' => $recipients,
                'content' => [
                    'email_rfc822' => $message->toString(),
                ],
            ], $this->options),
        ]);

        $message->getHeaders()->addTextHeader(
            'X-SparkPost-Transmission-ID', $this->getTransmissionId($response)
        );

        $this->sendPerformed($message);

        return $this->numberOfRecipients($message);
    }
}

new SparkPostEUTransportManager.php

namespace App\Mail;

use Illuminate\Mail\TransportManager;

class SparkPostEUTransportManager extends TransportManager
{
    /**
     * Create an instance of the SparkPost Swift Transport driver.
     *
     * @return \App\Mail\SparkPostEUTransport
     */
    protected function createSparkPostEUDriver()
    {
        $config = $this->app['config']->get('services.sparkposteu', []);

        return new SparkPostEUTransport(
            $this->guzzle($config), $config['secret'], $config['options'] ?? []
        );
    }

}

app\Providers folder

new MailServiceProvider.php

<?php
namespace App\Providers;

use Illuminate\Mail\MailServiceProvider as MailProvider;
use App\Mail\SparkPostEUTransportManager as TransportManager;

class MailServiceProvider extends MailProvider
{
    protected function registerSwiftTransport()
    {
        $this->app->singleton('swift.transport', function ($app) {
            return new TransportManager($app);
        });
    }
}

config folder

update services.php - new line :

'sparkposteu' => [
        'secret' => 'your-secret-key',
    ],

update app.php - new line :

'providers' => [
    /*
    * Laravel Framework Service Providers...
    */
    [...]
    //Illuminate\Mail\MailServiceProvider::class, //to be deleted
    [...]

    /*
    * Application Service Providers...
    */
    [...]
    App\Providers\MailServiceProvider::class, //to be added
    [...]
],

.env

MAIL_DRIVER=SPARKPOSTEU

I just hope there is no big change in Mail management in the following versions of laravel :)

Ozan's avatar

This is as easy as adding a key/value pair to your config/services.php.

    'sparkpost' => [
        'secret' => env('SPARKPOST_SECRET'),
        'options' => [
            'endpoint' => 'https://api.eu.sparkpost.com/api/v1/transmissions',
        ],
    ],
5 likes
basvandertogt's avatar

This is not working for Laravel 5.5 Endpoint is hardcoded: Line 57: vendor/laravel/framework/src/Illuminate/Mail/Transport/SparkPostTransport.php

realtebo's avatar

Thanks @ozan , you saved my life this afternoon.

your solution work with laravel 5.8 as today

Please or to participate in this conversation.