I was also trying to send emails directly via SMTP in WordPress without using any plugins and faced some problems. If anyone else is facing the same issue in 2024, I hope this will help.
Add this two filter to your theme functions.php file
add_filter("wp_mail_from_name", function(){
return "MAIL_FROM_NAME";
});
add_filter("wp_mail_from", function(){
return "MAIL_FROM";
});
Then add the phpmailer_init action
add_action('phpmailer_init', function($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.gmail.com'; // Replace with your SMTP host (e.g., smtp.gmail.com)
$phpmailer->SMTPAuth = true; // Enable SMTP authentication
$phpmailer->Username = '[email protected]'; // SMTP username
$phpmailer->Password = 'SMTP_PASSWORD'; // SMTP password
$phpmailer->SMTPSecure = 'ssl'; // Encryption: 'ssl' or 'tls'
$phpmailer->Port = 465; // TCP port to connect to
$phpmailer->From = '[email protected]'; // From email
$phpmailer->FromName = 'FROM_NAME'; // From name
$phpmailer->addReplyTo('[email protected]');
// $phpmailer->SMTPDebug = 2;
});