Problem sending emails via PHPMailer due to DNS settings

Viewed 26

This is my first post so please be gentle.

I've been trying to send automatic SMTP emails via PHPMailer using my webhost as the server. Below is the script I am using.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';

//Load Composer's autoloader
//require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 4; //SMTP::DEBUG_SERVER;  
    $mail->isSMTP();                   
    $mail->Mailer = "smtp";
    $mail->Host       = 'surrey.redbackinternet.net';   
    $mail->SMTPAuth   = true;                   
    $mail->Username   = 'noreply@mydomain.com';     
    $mail->Password   = 'password';      
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587; 

    //Recipients
    $mail->setFrom('noreply@mydomain.com', 'xxx');
    $mail->addAddress('contact@mydomain.com');

    //Content
    $mail->isHTML(true);  
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>

Previously, I was using Gmail as the server, which worked fine, but now I want to change this. When I run the script with the new details I get authentication error - "SMTP ERROR: Password command failed: 535 Incorrect authentication data". The full PHPMailer script log is shown in the image below.

PHPMailer log

I know the username and passwords used are correct and have seen this error seems to be returned whenever a connection cannot be established. I have a hunch it's related to my email DNS settings, though I could be completely wrong here. When I check my DNS and MX settings using dnschecker.org all seems ok as far as I can naively tell. But using hardenize.com I see my domain fails their mail server and email TLS checks. The information provided is lacking but it does say "A network error occurred while we were trying to communicate with a server. Error message: java.net.SocketTimeoutException: Read timed out". I should add here that my site uses Cloudflare as a CDN.

Any ideas on what the issue is or how to resolve it?

Thanks in advance.

0 Answers
Related