Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

Laravel tries to send mail to mailpit:1025 instead of your real SMTP server

ErrorConnection could not be established with host "mailpit:1025"

Short answer

Laravel is still using the development mail host from the starter .env, mailpit on port 1025, which only exists inside Laravel Sail. Your real values are not reaching the app: either .env was never changed, the config is cached, or a server-level variable overrides the file.

  • Applies to Laravel 10, Laravel 11, Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the running app which mail host it really uses. Do this in the same environment as the failing request, not on your laptop.

    php artisan tinker --execute="dump(config('mail.mailers.smtp.host'), config('mail.mailers.smtp.port'));"
  2. 2Match what you see, then jump to its fix

    1. 1Tinker prints mailpit and 1025.The starter .env values were never replacedFix
    2. 2The .env file shows the new host, but tinker or the error still shows mailpit. A file bootstrap/cache/config.php exists.Configuration is cached, so the edited .env is not readFix
    3. 3Tinker still shows mailpit after config:clear, and .env is correct.A server-level environment variable overrides .envFix

What does the error look like?

  • Connection could not be established with host "mailpit:1025"
  • Symfony\Component\Mailer\Exception\TransportException

Cause 1 of 3

The starter .env values were never replaced

1Check

The Laravel 10 starter .env.example ships MAIL_HOST=mailpit and MAIL_PORT=1025. That host name only resolves inside a Sail Docker network, so on shared hosting or a VPS it cannot be reached.

grep -n "^MAIL_" .env

2Fix

Put the host, port and login your mail provider gave you into .env. Keep one MAIL_MAILER line only.

MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your-login
MAIL_PASSWORD=your-app-password

3Verify

Clear config, then read the values back in tinker.

php artisan config:clear
php artisan tinker --execute="dump(config('mail.mailers.smtp.host'));"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

Configuration is cached, so the edited .env is not read

1Check

After config:cache the framework no longer loads .env during requests or Artisan commands. It uses the cached array.

ls -l bootstrap/cache/config.php

2Fix

Purge the cached configuration, and rebuild it only after .env is correct.

php artisan config:clear
php artisan config:cache

3Verify

Tinker now prints the new host.

php artisan tinker --execute="dump(config('mail.mailers.smtp.host'));"

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

A server-level environment variable overrides .env

1Check

Laravel allows any variable in .env to be overridden by an external server or system environment variable. Docker Compose files, PHP-FPM pool settings, a panel's environment screen and CI secrets are the usual places.

printenv | grep '^MAIL_'
grep -rn "MAIL_HOST" docker-compose*.yml /etc/php 2>/dev/null

2Fix

Change or remove the MAIL_* variable where it is defined, then restart PHP-FPM, the container or the queue workers so they see it.

sudo systemctl restart php8.3-fpm
php artisan queue:restart

3Verify

Tinker, run through the same process, prints the host you set.

php artisan tinker --execute="dump(config('mail.mailers.smtp.host'));"

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Replace every MAIL_* line when you copy .env.example to a server, not just the password.
  • Run config:cache only as a deploy step, after .env is final.
  • Keep mail settings in one place: either .env or the platform environment, not both.
  • Restart queue workers after a mail setting changes, because they keep old config in memory.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Email

PHP mail() returns true but the email never arrives

Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing

mail() returning true only means the message was accepted for delivery, not that it arrived.

4 causes, each with a fix

Email

PHPMailer addCC() adds no CC recipient

The mail arrives for the To address but not for the CC address

addCC() returns false when it rejects the address, and PHPMailer then sends without that CC.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.