I can send email from outside but not using phpmailer (error 111)

centos7email-serversmtp

I can send email via my webserver from my computer (using Thunderbird), but not from my website using 'phpmailer'. Oddly enough, when I run 'telnet mail.myserver.com 25' on the server, it says 'connection refused', the same error message I'm getting with phpmailer (error 111 – "Connection refused"). However, emails are sent fine through Thunderbird (which is set up to port 25 also, only to use encrypted passwords), I can also connect with telnet just fine from my computer to the mail server.
Any idea what may be the reason for this strange problem? I'm using the latest phpmailer version, and the CentOS7 server is running php 5.6.18.

Output of netstat -an | grep 25:

tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN
tcp6       0      0 :::25                   :::*                    LISTEN

Phpmailer config:

require_once("./stuff/phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = $SMTPHOST;
$mail->SMTPAuth = true;                 
$mail->Port = 25;
$mail->SMTPDebug = 2;
$mail->Username = $SMTPUSER;    
$mail->setFrom($email,$name);
$mail->AddAddress($AdminMail);
$mail->Subject = "Here is the email";
$mail->Body = "I am an email";
if(!$mail->send()){..}

By the way, PHP's mail() method works also, only SMTP seems to cause the problem. So when I change $mail->IsSMTP() to $mail->IsMail(), it works.

Best Answer

I'm assuming that mail.myserver.com is both the SMTP server and the webserver running phpmailer, if not than this is not your issue.

It sounds like your SMTP server isn't listening on localhost but only on it's network IP. If so you need to configure your MTA to listen on localhost in addition to it's external interface. This is going to be different depending on your MTA.

For postfix (the default MTA for CentOS7) in main.cf set something like this:

inet_interfaces = all

or if you don't want to listen on all interfaces

inet_interfaces = 1.2.3.4, 127.0.0.1, [::1]

and restart postfix.