Centos – Problems opening port 465 for smtp on centos

centosiptablessmtp

I'm using phpMailer to send off purchase confirmations using a google apps address/smtp. The whole thing was working great on the development box, but now that it's live, it seems that the port I'm using (465) is blocked.

So I edit iptables with:

-A INPUT -p tcp -dport 465 -j ACCEPT  
-A INPUT -p tcp -sport 465 -j ACCEPT

My understanding is that this allows inbound & outbound connections on 465 (correct me if I'm wrong). After restarting iptables:

iptables -nL  
Chain INPUT (policy ACCEPT)
target   prot   opt   source      destination
ACCEPT   tcp    --    0.0.0.0/0   0.0.0.0/0   tcp dpt:465
ACCEPT   tcp    --    0.0.0.0/0   0.0.0.0/0   tcp spt:465

However I still receive "Failed to connect to server: Permission denied" as debug from phpMailer. From what I've read, this usually indicates that the port is closed, & evidently that is the case.

Any hints on how to debug this further? Any help would be much appreciated.

In case it helps, here's the php:

$mail = new PHPMailer();        
$mail->IsSMTP();
$mail->SMTPDebug = 1; //!DEV
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';      
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = $emailAddress;
$mail->Password = $emailPassword;
$mail->SetFrom($emailAddress, 'PURCHASE ORDER');
$mail->AddAddress($customerEmail);
$mail->AddAddress($salesEmail); 
$mail->Subject = $subject;
$mail->Body = $body;        
$mail->Send() or die('Error: ' . $mail->ErrorInfo);

*Edit: The mailer works flawlessly on the development server – any advice on ways to compare what may be different? Same OS, same php… I'm stumped!

Best Answer

You've told your server to accept incoming connections on port 465, but then you're also telling it to connect to smtp.gmail.com which is an outbound connection.

You will need to open the same ports, but on the OUTPUT chain instead of the INPUT one:

-A OUTPUT -p tcp --dport 465 -j ACCEPT

To edit from the terminal the command would be:

iptables -A OUTPUT -p tcp --dport 465 -j ACCEPT