PHPMailer GoDaddy Server SMTP Connection Refused
Moving a hosting account from Windows to Linux can introduce subtle environment shifts that break established integrations. You might assume that if your PHPMailer settings worked for months on a Windows box, they will function identically on the new server without modification. However, when you encounter an SMTP-> ERROR: Failed to connect to server: Connection refused (111), it usually indicates a network-level blockage rather than a configuration error in your code.
The error code 111 specifically means "Connection refused." This is distinct from a timeout or a DNS failure; the TCP handshake either failed to reach the remote port, or the remote system explicitly rejected the connection attempt. In the context of GoDaddy hosting, this behavior is almost always triggered by firewall rules applied at the server level (Nginx/Apache) or by security policies within the cloud infrastructure that differ between Windows and Linux environments.
Verifying the Hostname and Port Configuration
Before diving into server-side logs, ensure your PHPMailer configuration aligns with GoDaddy's current documentation for Secure Server SMTP. The source material indicates a switch from smtpout.secureserver.net to potentially other hosts, but the core issue often lies in how the connection is initiated.
Your code snippet uses port 465 with SSL:
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
This is a valid configuration for GoDaddy. However, some legacy Linux environments or specific firewall configurations block incoming connections on port 465 unless the application server is explicitly whitelisted. If your site was running on IIS (Windows), the networking stack might have handled these ports differently than the standard Nginx/Apache stack on a Linux VPS or shared host.
If you switch to TLS with port 587, ensure SMTPSecure is set to tls:
$mail->SMTPSecure = "tls";
$mail->Port = 587;
Using port 25 is frequently blocked by ISPs and hosting providers to prevent spam relay abuse. Attempting to connect on ports like 80 or 3535 will almost certainly result in a connection refused error, as those are not standard SMTP relay ports for GoDaddy.
Investigating Server-Side Firewall Rules
The transition from Windows to Linux often exposes the site to stricter default security policies. On a Linux server, the ufw (Uncomplicated Firewall) or the underlying kernel firewall (iptables) might be blocking outbound connections from PHP scripts to GoDaddy's SMTP relay if the application is not running on the same domain as the mail server, or if the specific IP of your site is not trusted.
GoDaddy's tech support may see standard web server logs showing a successful 200 OK for your script execution, but they might miss the fact that the underlying socket connection to the SMTP relay is being dropped by an intermediate firewall layer. To diagnose this without direct access to deep kernel logs, you can use command-line tools to simulate what PHP is doing.
Run the following from your server terminal (SSH) to test connectivity:
openssl s_client -connect smtpout.secureserver.net:465 -starttls smtp
# Or for TLS 587
openssl s_client -connect smtpout.secureserver.net:587
If this command hangs or returns "Connection refused," the issue is network-level, not PHP code. If it connects successfully but you still get errors in PHP, the problem might be related to SSL certificate validation or specific cURL/Socket timeouts configured in php.ini.
Troubleshooting Specific PHP Configurations
Sometimes the environment variables or PHP settings differ between OS types. Check if openssl is properly installed and configured on your new Linux box:
php -m | grep ssl
php -i | grep openssl
If SSL modules are missing, SMTP over port 465 will fail. Additionally, ensure that the mail() function isn't being used elsewhere in your stack which might conflict with PHPMailer's explicit socket usage.
If you have access to your server's main log files (e.g., /var/log/mail.log or application logs), look for entries preceding the script execution that indicate connection drops. If GoDaddy support cannot see these in their dashboard, it suggests the blockage is happening before the mail queue even touches their relay logic.
In many cases where a Windows-to-Linux migration breaks SMTP, enabling debug mode ($mail->SMTPDebug = 1) provides the most immediate clue. The debug output will show exactly which port is being targeted and whether the handshake completes. If you see CONNECT followed immediately by an error, the network path is blocked. If you see CONNECT OK, then the issue lies within authentication credentials or certificate validation on the server side.
For production environments where debugging is not desirable, ensure that your PHPMailer instance is properly configured to handle SSL handshakes gracefully without throwing fatal errors. If you are using a shared hosting plan, remember that some providers restrict outbound SMTP connections from user directories unless explicitly allowed via .htaccess or specific file permissions.
