Connection refused when PHP sends email locally

SYSTEM INFORMATION
OS type and version: Ubuntu Server 20.04.03 LTS
Webmin version: 1.981
Virtualmin version: 6.17-3
Related products version: Postfix 3.4.13, PHP 7.4.3, PHPMailer 6.5.1

So I have a PHP website with a form that emails info to the email address the user submits. I’m using PHPMailer to login to a local user account I created with Virtualmin to send the email. However, I’m getting this error 1 minute after I submit the form. Basically, the PHPMailer script can’t connect to the mail server, both sharing the same public IP. I’ve tried adjusting firewalld to allow port 587, but no change. I ran telnet www.***.org 587 on the server but it gives connection refused as well. I can’t ping www.***.org either. I can ping 1.1.1.1 and telnet from the server but it seems like it won’t loopback to itself. Is there a firewall or network setting I’m missing?

2021-10-27 19:50:41 Connection: opening to mail.***.org:587, timeout=300, options=array()
2021-10-27 19:51:48 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.***.org:587 (Connection refused) [/home/********/php/PHPMailer/src/SMTP.php line 388]
2021-10-27 19:51:48 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Here’s the PHPMailer code:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '/home/hostname/php/PHPMailer/src/Exception.php';
require '/home/hostname/php/PHPMailer/src/PHPMailer.php';
require '/home/hostname/php/PHPMailer/src/SMTP.php';
session_start();
function email($type,$mID,$MailSubject,$MailBody) {
        $trans=array("<p><h"=>"<h","<p><div"=>"<div","<p><ol"=>"<ol","<p><ul"=>"<ul","<p><li"=>"<li","<p><p"=>"<p","<p><p>"=>"<p>","</h1></p>"=>"</h1>","</h2></p>"=>"</h2>","</h3></p>"=>"</h3>","</>
        $MailHost="mail.***.org";
        $MailUsername='info@***.org';
        $MailPassword='<notrealpassword>';
        $MailFromEmail=$MailUsername;
        $MailBody="<p>".$MailBody."</p>";

        error_reporting(E_STRICT | E_ALL);
        date_default_timezone_set('America/Chicago');
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = $MailHost;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure="tls";
        $mail->SMTPKeepAlive = true;
        $mail->Port = 587;
        $mail->Username = $MailUsername;
        $mail->Password = $MailPassword;
        $mail->setFrom($MailFromEmail,$MailFromName);
        $mail->Subject = $MailSubject;

        require '/home/hostname/php/topBody.php';
        if(!empty($MailBody)) {
                $mail->msgHTML($topBody.strtr(str_replace(array("\r\n","\n\n","\n"), "</p><p>",htmlspecialchars_decode($MailBody)),$trans).(isset($MailFooter) ? $MailFooter : "")."</body></html>");
                $mail->AltBody = strip_tags($MailBody);
                $mail->addAddress('info@***.org');
                        if(!$mail->send()) {
                echo "Mailer Error (" . str_replace("@", "&#64;", "info@***.org") . ') ' . $mail->ErrorInfo . '<br />';
                        }
                        $mail->clearAddresses(); $mail->clearAttachments();
        }
        unset($MailBody); unset($MailSubject); unset($mID); unset($type);
}
?>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.