PHPMailer could not connect to SMTP host
When you encounter the error SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known, it usually points to a DNS resolution failure rather than a configuration syntax issue within your application. This specific message from the PHP mailer stack indicates that the underlying operating system cannot translate the hostname you provided into an IP address.
The error log explicitly states getaddrinfo failed. This function attempts to resolve a hostname to one or more addresses. When it returns "No such host is known," the network layer has given up before even attempting a TCP handshake. In the context of PHPMailer, this happens immediately when the library tries to open the socket connection to the SMTP server specified in $site['smtp_host'].
Resolving Hostname Issues
The most common cause for this specific error is an incorrect or unreachable hostname configuration. In your provided config snippet, you are setting:
$site['smtp_host'] = "smtp.<my-domain>.com";
If <my-domain> is a placeholder and not replaced with your actual domain name, the string passed to PHPMailer will literally contain the text <my-domain>. Most DNS servers will reject this because they interpret angle brackets as delimiters for email addresses or IP ranges, not as part of a standard hostname. Even if you replaced the placeholder with a real domain, such as smtp.example.com, the error persists if that specific subdomain does not exist in your DNS records.
To resolve this, you must ensure that:
- The Hostname is Valid: The string passed to
$site['smtp_host']must be a fully qualified domain name (FQDN) that exists in the public DNS or your private network's internal DNS. - DNS Records Exist: You need an A record (or CNAME) pointing
smtp.<your-domain>.comto the IP address of your mail server. If you are using a shared hosting environment, you likely cannot create these records yourself and must use the host provider's pre-configured SMTP credentials.
Verifying Network Reachability
Before concluding that the application code is flawed, verify the network path from where the PHP script executes to the mail server. The error getaddrinfo failed can also occur if the server running PHP cannot reach the internet or access the specific DNS resolver configured for that machine.
You can test this resolution directly on the server using the command line:
ping smtp.your-actual-domain.com
# Or use nslookup/dig for more detailed info
nslookup smtp.your-actual-domain.com
If these commands fail with "unknown host" or "non-existent domain," the issue lies entirely in your DNS configuration, not your PHPMailer instantiation. Conversely, if ping works but PHPMailer fails, check the firewall rules on the mail server to ensure it accepts connections on port 587 (or 25/465 depending on your encryption settings).
Authentication and Security Considerations
Your configuration includes $site['smtp_username'] set to an email address. While this is a common practice, modern SMTP servers often require the username to be the actual login account used by the mail server, not necessarily the sender's email alias. If the host requires specific authentication credentials different from the email address, ensure you are passing the correct username string.
Additionally, always enable SSL or TLS encryption. Connecting to port 587 without encryption is a security risk and often triggers rejection by modern providers like Gmail or corporate Exchange servers. Ensure your FreakMailer (or standard PHPMailer) instance has the following settings enabled:
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'tls'; // Recommended for port 587
// $mailer->Port = 587;
If you are managing email infrastructure in a Laravel project, this manual configuration approach often feels clunky. For production applications, consider leveraging https://laravelcompany.com resources or the built-in mail facade with environment variables, which abstracts away these low-level SMTP connections and reduces the likelihood of misconfiguration errors like the one described above.
Final Checklist
If you are still seeing the "No such host is known" error after correcting the hostname string:
- Check for typos in the domain name.
- Ensure the server resolving the DNS has internet access.
- Verify that the mail server software (Postfix, Exim, etc.) is listening on the specified port and accepting connections from your application's IP address.
The error message is definitive: PHPMailer successfully instantiated, but the network stack failed to resolve the target hostname. Fixing the DNS record or correcting the host string variable will resolve the issue.