wp_mail fails with “Could not instantiate mail function” but “mail()” works fine

So I managed to trace down the issue with the following method:

I created a php file with the following:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// include wordpress
require_once('wp-load.php');
$to = "[email protected]";
$msg = "test email via wordpress";

$headers = array(
  'From: The Domain<[email protected]>'
);
$headers = implode( PHP_EOL, $headers );
// use wordpress email function
$check = wp_mail($to, $msg, $msg, $headers);

var_dump($check);

and ran it on the console with php8.2 ./email_test.php
This then returned the following:

Testing WordPress Email
sendmail: the server did not accept the mail
sendmail: server message: 554-Transaction failed
sendmail: server message: 554 Unauthorized sender address.
sendmail: could not send mail (account default from /homepages/config/smtp/msmtprc.u93774155)
bool(false)

Which allowed me to go to the hoster and they fixed it by adding the domain.com as a whitelisted sender to their setup and then it worked. It seems that phpMailer is supressing those errors that you get when you run it from the console. So just run wp_mail from the console, you might get error messages that you miss otherwise.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)