framework: Laravel 11 with symfony/mailer v7.2.0 - UnsupportedSchemeException for SMTP configuration

Laravel Version

11.34.2

PHP Version

8.3.14

Database Driver & Version

No response

Description

After upgrading Laravel 8 -> 9 -> 10 -> 11, I noticed that it threw error when trying to send an email.

> \Mail::raw('Test email', function ($message) {
    $message->to('...')->subject('Test Email');
});

Symfony\Component\Mailer\Exception\UnsupportedSchemeException  The "" scheme is not supported; supported schemes for mailer "smtp" are: "smtp", "smtps".

I verified that mail config (smtp transport) was correct.

> config('mail.mailers.smtp');
= [
    "transport" => "smtp",
... redacted ...
  ]

> config('mail');
= [
    "default" => "smtp",
    "mailers" => [
      "smtp" => [
        "transport" => "smtp",

When I upgraded to Laravel 11, Composer installed symfony/mailer with version 7.2.0.

I checked what my other Laravel 11 project had - it was 7.0.7.

So I tried to downgrade from 7.2.0 to 7.0.7, it did not throw error and I was able to send/receive email.

"require": {
    ...
    "symfony/mailer": "7.0.7",
    "laravel/framework": "^11.0",
    ...
}
> \Mail::raw('Test email', function ($message) {
    $message->to('...')->subject('Test Email');
});

= Illuminate\Mail\SentMessage {#5336}
composer show symfony/mailer
name     : symfony/mailer
descrip. : Helps sending emails
keywords :
versions : * v7.2.0
released : 2024-11-25, this week
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : https://symfony.com
source   : [git] https://github.com/symfony/mailer.git e4d358702fb66e4c8a2af08e90e7271a62de39cc
dist     : [zip] https://api.github.com/repos/symfony/mailer/zipball/e4d358702fb66e4c8a2af08e90e7271a62de39cc e4d358702fb66e4c8a2af08e90e7271a62de39cc
path     : /.../app/vendor/symfony/mailer
names    : symfony/mailer

support
source : https://github.com/symfony/mailer/tree/v7.2.0

autoload
psr-4
Symfony\Component\Mailer\ => .
exclude-from-classmap

requires
egulias/email-validator ^2.1.10|^3|^4
php >=8.2
psr/event-dispatcher ^1
psr/log ^1|^2|^3
symfony/event-dispatcher ^6.4|^7.0
symfony/mime ^7.2
symfony/service-contracts ^2.5|^3

requires (dev)
symfony/console ^6.4|^7.0
symfony/http-client ^6.4|^7.0
symfony/messenger ^6.4|^7.0
symfony/twig-bridge ^6.4|^7.0

conflicts
symfony/http-client-contracts <2.5
symfony/http-kernel <6.4
symfony/messenger <6.4
symfony/mime <6.4
symfony/twig-bridge <6.4

composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^8.2",
        "barryvdh/laravel-dompdf": "^2.2",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^11.0",
        "laravel/tinker": "^2.0",
        "maatwebsite/excel": "^3.1",
    },
    "require-dev": {
        "fakerphp/faker": "*",
        "laravel/pint": "^1.5",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^8.1",
        "phpunit/phpunit": "^10.0",
        "spatie/laravel-ignition": "^2.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

Steps To Reproduce

  1. Install Laravel 11
  2. Check that Composer installed symfony/mailer v7.2.0 or latest
  3. Try send email with smtp transport configured

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (4 by maintainers)

Commits related to this issue

Most upvoted comments

When installing mailpit with Laravel Sail, .env is set with MAIL_ENCRYPTION=null, which seems to cause this error.

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

As others have said, changing it to MAIL_ENCRYPTION=tls will solve the problem.

A fix has been merged and pending a release (https://github.com/laravel/framework/pull/53585). You should run composer require "symfony/mailer:~7.1.0" until the release.

Downgrading didn’t work for me but setting ‘scheme’ to ‘smtp’ in config ‘mailers.smtp’ did.

        'smtp' => [
            'scheme' => 'smtp', // smtp or smtps

            'transport' => 'smtp',
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', '127.0.0.1'),
            'port' => env('MAIL_PORT', 2525),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN', parse_url(env('APP_URL', 'http://localhost'), PHP_URL_HOST)),
        ],

If you’re curious why this solution works, the code responsible for creating smtpTransport from vendor\laravel\framework\src\Illuminate\Mail\MailManager.php:

    protected function createSmtpTransport(array $config)
    {
        $factory = new EsmtpTransportFactory;

        $scheme = $config['scheme'] ?? null;

        if (! $scheme) {
            $scheme = ! empty($config['encryption']) && $config['encryption'] === 'tls'
                ? (($config['port'] == 465) ? 'smtps' : 'smtp')
                : ''; // here
        }

        $transport = $factory->create(new Dsn(
            $scheme,
            $config['host'],
            $config['username'] ?? null,
            $config['password'] ?? null,
            $config['port'] ?? null,
            $config
        ));

        return $this->configureSmtpTransport($transport, $config);
    }

The scheme will default to smtp or smtps if encryption = tls is specified in your configuration:

'smtp' => [
    //....
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'port' => env('MAIL_PORT', 587),
],

Also, check your .env file to ensure the encryption value is set correctly. For example, in my case, I have:

env

MAIL_ENCRYPTION=tls

If you need a different encryption type, you will need to manually specify the scheme value. To do so, check your config/mail.php file to see if the scheme key is present. If not, add it:

'smtp' => [
    //....
    'scheme' => env('MAIL_SCHEME', 'smtp'), // smtp or smtps
],

downgrading worked, but also adding the scheme key without downgrading worked

'smtp' => [
    //....
    'scheme' => env('MAIL_SCHEME', 'smtp'), // smtp or smtps
],

downgrading worked, but also adding the scheme key without downgrading worked

'smtp' => [
    //....
    'scheme' => env('MAIL_SCHEME', 'smtp'), // smtp or smtps
],

Thanks! @mathiaswillburger it’s working.

> composer show symfony/mailer
name     : symfony/mailer
descrip. : Helps sending emails
keywords :
versions : * v7.1.6

v7.1.6 works for me

downgrading worked, but also adding the scheme key without downgrading worked

'smtp' => [
    //....
    'scheme' => env('MAIL_SCHEME', 'smtp'), // smtp or smtps
],

Thanks! @mathiaswillburger it’s working.

This fixed my ISSUE. Thanks

I have a project wich was upgraded from laravel 6 to 11, my config/mail.php doesn’t have a “mailers.smtp” key, so I notice if we add the key just like:

`/* |-------------------------------------------------------------------------- | E-Mail Encryption Protocol |-------------------------------------------------------------------------- | | Here you may specify the encryption protocol that should be used when | the application send e-mail messages. A sensible default using the | transport layer security protocol should provide great security. | */

'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'scheme' => 'smtp', // <--- add here

`

fixes the issue, in case someone has to deal with a similar issue.

To fix the probleme, in the config file mail.php, add the following to the smtp mailer :

    'mailers' => [
        'smtp' => [
            'scheme' => 'smtp',
    .....

ci does that usually, but I’ll give it a test, good idea.

Try composer clear-cache and/or php artisan optimize:clear