Connection could not be established with host mailtrap.io [php_network_getaddresses: getaddrinfo failed: No such host is known. #0]
The error php_network_getaddresses: getaddrinfo failed: No such host is known is a definitive DNS resolution failure. When the PHP socket layer attempts to connect, it cannot translate the hostname string into an IP address. In the context of your specific stack, this means your server simply does not know where smtp.gmail.com (or your new target, mailtrap.io) lives on the network.
This is distinct from a connection timeout or a TLS handshake error. The underlying system call to resolve the domain name has returned empty results. While you mention using these settings for six months successfully, the fact that it broke only after rebuilding and uploading to staging points directly to an environmental change rather than a code logic error.
Why DNS Resolution Fails in Staging
The most common culprit when this error appears suddenly on a deployment is missing DNS records or restricted network access. If your local development machine resolves mailtrap.io correctly but the staging server does not, one of two things is happening:
- Missing A Record: The domain name has not been propagated to the specific nameservers serving your staging environment, or the TTL was too low and the old cache hasn't refreshed.
- Blocked DNS Queries: The firewall on the staging server might be blocking outbound UDP port 53 (used by DNS). If the PHP process cannot query an external resolver (like
8.8.8.8), it cannot resolve the hostname.
Check your /etc/hosts file on the staging server first. Sometimes, a local override in development gets accidentally committed to the production .env or deployed via a script that assumes hostnames are resolvable locally. If you are using mailtrap.io, ensure you have added the specific A records provided by Mailtrap to your DNS provider's zone file and allowed sufficient time for propagation.
Configuring the Connection Context
Looking at your config/mail.php configuration, you are correctly setting up the SMTP driver with TLS encryption on port 587. The syntax is sound, and the environment variables are being picked up properly. However, if DNS resolution is failing, no amount of correct configuration in mail.php will fix it.
If your server is running behind a corporate firewall or a restrictive cloud provider (like some older AWS VPC configurations), you may need to configure a custom DNS resolver for the PHP process. You can do this by modifying the environment variable SYSTEMD_RESOLVCONF or setting the RESolv.conf file directly on the server to point to a reliable public DNS server like 1.1.1.1.
# Example of ensuring /etc/resolv.conf has valid nameservers
sudo nano /etc/resolv.conf
# Add:
nameserver 8.8.8.8
nameserver 1.1.1.1
Handling Authentication and Credentials
Your .env file shows MAIL_USERNAME and MAIL_PASSWORD. It is critical to ensure these credentials are valid for the specific domain you are targeting. If you switched from Gmail to Mailtrap, you must verify that:
- You have created a new account in Mailtrap.
- You have generated an API key or SMTP username/password specifically for the integration you are using.
- The credentials are not being cached in your local development environment and accidentally deployed.
If you are using Gmail, ensure that "Less secure apps" is no longer the solution (it was deprecated years ago) and that you are using an App Password or OAuth2 flow. However, given the error message explicitly mentions mailtrap.io in the title despite the code showing smtp.gmail.com, there is a high probability you intend to switch hosts but haven't updated the MAIL_HOST variable in your .env file yet.
Verifying Connectivity
Before assuming the application logic is flawed, verify connectivity from the staging server itself using standard command-line tools:
# Test DNS resolution
nslookup mailtrap.io
# or
ping mailtrap.io
# Test SMTP port accessibility
telnet mailtrap.io 587
# or
nc -zv mailtrap.io 587
If nslookup returns an error, the issue is purely network/DNS. If telnet hangs or fails immediately, the firewall is blocking the connection. Once the hostname resolves to an IP and the port accepts the connection, update your .env file with the new Mailtrap credentials and ensure the MAIL_HOST variable matches the new domain exactly.
