Laravel error on GMAIL smtp usage: 'Connection could not be established with host "mailpit:1025"'
The error message Connection could not be established with host "mailpit:1025" is a classic symptom of environment variable leakage in your development setup. The specific exception php_network_getaddresses: getaddrinfo for mailpit failed confirms that the Laravel application is attempting to resolve the hostname mailpit rather than the configured smtp.gmail.com.
This discrepancy usually indicates that your .env file contains conflicting configurations or that you are running the application in a Docker container where specific environment variables override the local settings. Even if your .env file clearly states MAIL_HOST=smtp.gmail.com, Laravel prioritizes environment variables defined at runtime. If there is a stray MAIL_HOST variable set to mailpit in your Docker Compose stack or a previous session, it will take precedence over the file.
To resolve this, you must ensure that the MAIL_HOST environment variable strictly matches the Gmail SMTP endpoint. In many local development setups involving Mailpit, developers configure their .env files to point to http://mailpit:1025 for testing. However, if you intend to send actual emails via Gmail, you must explicitly remove or override any reference to Mailpit in your environment.
Here is the corrected configuration block for your .env file, ensuring all parameters align with Gmail's requirements:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=est.juan.arango1@unimilitar.edu.co
MAIL_PASSWORD=[your-google-app-password]
MAIL_ENCRYPTION=tls
There is a critical configuration error in your current setup regarding encryption. You have set MAIL_ENCRYPTION=ssl while using port 587 and smtp.gmail.com. Gmail's standard SMTP submission port 587 uses TLS (Transport Layer Security), not SSL (Secure Sockets Layer). While some older servers might accept SSL on this port, modern best practices and Laravel's underlying mail transport classes expect tls for port 587. If you switch to port 465, which is traditionally used for SMTPS (SSL), you must ensure your server specifically supports that protocol. For Gmail, the combination of port 587 and TLS encryption is the standard, supported method.
Regarding the stream configuration in config/mail.php, enabling allow_self_signed and disabling peer verification (verify_peer: false) is a valid approach for local development when using self-signed certificates (like those often generated by Mailpit). However, this configuration does not fix the hostname mismatch. The error persists because the application is still looking for a host named "mailpit".
In your route definition, you are catching Throwable and echoing it. While this helps with debugging, it exposes internal errors to your users if not handled carefully in production. For local development, logging the full stack trace using Laravel's logging facade (Log::error($e->getMessage())) is a cleaner approach than returning raw HTML error messages.
If you are running this application within a Docker ecosystem, check your docker-compose.yml or the environment variables passed into the container. Ensure that the variable MAIL_HOST inside the container explicitly points to smtp.gmail.com. Sometimes, build scripts or previous deployment artifacts inject default values (like mailpit) that persist across restarts unless cleared.
Additionally, verify that you are using a valid Google App Password. Standard two-factor authentication passwords generated via Google's account page are not sufficient for SMTP access; you must generate a specific 16-character app password within your Google Account security settings and use that as the MAIL_PASSWORD.
If you continue to face issues after correcting the encryption type and ensuring the host variable is clean, consider clearing the cache again using php artisan config:clear to ensure no old configuration arrays are cached in memory. However, the root cause remains the hostname mismatch, which requires a direct edit to your environment variables or Docker environment injection.