Laravel本地Homestead用Mailtrap发件时SMTP连接超时#110问题求助
Hey there, let's work through this frustrating connection timeout issue you're hitting when trying to send emails via Mailtrap on your Homestead server. I've dealt with similar headaches before, so here's a step-by-step breakdown of what to check and fix:
Double-check your Mailtrap SMTP configuration in
.env
It's easy to mistype credentials or settings. Make sure your.envhas the exact values from your Mailtrap inbox page:MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=your_mailtrap_inbox_username MAIL_PASSWORD=your_mailtrap_inbox_password MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=your-test-email@example.com MAIL_FROM_NAME="${APP_NAME}"Note: For port 2525,
MAIL_ENCRYPTIONshould be set tonull(not an empty string or "tls"). If you switch to port 587 or 465 later, update encryption totlsorsslrespectively.Test connectivity from Homestead to Mailtrap
SSH into your Homestead server and run these commands to verify the server can reach Mailtrap's SMTP endpoint:- Ping to check DNS resolution:
ping smtp.mailtrap.io
If this fails, your Homestead instance has DNS issues. Update your server's DNS to use Google (8.8.8.8) or Cloudflare (1.1.1.1) in/etc/resolv.conf. - Telnet to test port access:
telnet smtp.mailtrap.io 2525
If you get a timeout here, the problem is network-level—even if your local firewall allows 2525, Homestead might be blocked or lack outbound access.
- Ping to check DNS resolution:
Verify Homestead's internal firewall rules
Even if your local firewall is clear, check if Homestead's internal firewall (ufw/iptables) is blocking outbound traffic on 2525:- Check ufw status:
sudo ufw status - If port 2525 isn't allowed, add the rule:
sudo ufw allow out 2525/tcp - For iptables, list rules with
sudo iptables -Land add an allow rule if needed.
- Check ufw status:
Clear Laravel's configuration cache
Sometimes Laravel caches old config values even after you update.env. Run these commands to reset the cache:php artisan config:clear php artisan cache:clearThen try sending the test email again.
Try alternative Mailtrap ports
Some server environments restrict port 2525. Switch to Mailtrap's encrypted ports and update your.env:- For TLS (port 587):
MAIL_PORT=587 MAIL_ENCRYPTION=tls - For SSL (port 465):
MAIL_PORT=465 MAIL_ENCRYPTION=ssl
- For TLS (port 587):
Confirm Homestead has outbound internet access
If none of the above works, check if your Homestead instance can reach external websites at all:curl https://google.com
If this fails, your Homestead network setup (like NAT mode, port forwarding, or host machine network) has an issue. Verify yourHomestead.yamlnetwork settings and restart the instance withhomestead reload --provision.
内容的提问来源于stack exchange,提问作者sbarb

