Connection refused when PHP sends email locally

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 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.

Here’s the PHPMailer code:

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';
function email($type,$mID,$MailSubject,$MailBody) {

        error_reporting(E_STRICT | E_ALL);
        $mail = new PHPMailer(true);
        $mail->Host = $MailHost;
        $mail->SMTPAuth = true;
        $mail->SMTPKeepAlive = true;
        $mail->Port = 587;
        $mail->Username = $MailUsername;
        $mail->Password = $MailPassword;
        $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);
                        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.