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

jmadlena's avatar

Mail config: URL vs HOST

Hi,

In Laravel 11 when publishing the Mail config, in the SMTP mailer section it has a url and host key in the array. Does anyone know what the URL is for? I've had a MAIL_HOST set for a long time in my .env file, but I've never had a MAIL_URL value.

So I'm not exactly sure what it's for, and I can't find anything in the Laravel docs about it.

smtp' => [
   'transport' => 'smtp',
   'url' => env('MAIL_URL'),
   'host' => env('MAIL_HOST', '127.0.0.1'),
   ...
],
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

In Laravel, the url and host keys in the SMTP mailer configuration serve different purposes, and you can use either depending on your setup.

  1. host: This is the traditional way to specify the SMTP server's address. It's the hostname or IP address of your SMTP server. For example, if you're using Gmail's SMTP, you might set MAIL_HOST=smtp.gmail.com in your .env file.

  2. url: This is a newer addition that allows you to specify the entire connection string in a single URL format. This can be useful for services that provide connection strings or when you want to encapsulate all connection details in one place. The URL can include the protocol, host, port, and even authentication details.

Here's how you might use each:

  • Using host:

    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', '127.0.0.1'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
    ],
    
  • Using url:

    'smtp' => [
        'transport' => 'smtp',
        'url' => env('MAIL_URL'),
    ],
    

In the .env file, the MAIL_URL might look something like this:

MAIL_URL=smtp://username:[email protected]:587

In summary, if you have a MAIL_URL, it can simplify your configuration by consolidating all connection details into a single string. However, if you prefer or need to specify each part of the connection separately, you can continue using MAIL_HOST along with other related environment variables.

1 like

Please or to participate in this conversation.