diff --git a/includes/mail.inc b/includes/mail.inc
index a97c788f09..9495838e0c 100644
--- a/includes/mail.inc
+++ b/includes/mail.inc
@@ -10,8 +10,19 @@
  *
  * $conf['mail_line_endings'] will override this setting.
  */
-define('MAIL_LINE_ENDINGS', isset($_SERVER['WINDIR']) || (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) ? "\r\n" : "\n");
-
+if (defined('PHP_VERSION_ID') && PHP_VERSION_ID < 80000 ) {
+  if (isset($_SERVER['WINDIR']) || isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {
+    define('MAIL_LINE_ENDINGS', "\r\n");
+  }
+  else {
+    //If PHP version is below 8 and server software is not Windows then keep using LF instead of CRLF
+    define('MAIL_LINE_ENDINGS', "\n");
+  }
+}
+else {
+  //If PHP version is 8 and above use CRLF line endings
+  define('MAIL_LINE_ENDINGS', "\r\n");
+}
 
 /**
  * Special characters, defined in RFC_2822.
diff --git a/modules/system/system.mail.inc b/modules/system/system.mail.inc
index 9a17f55f6f..5d69419a37 100644
--- a/modules/system/system.mail.inc
+++ b/modules/system/system.mail.inc
@@ -62,9 +62,8 @@ class DefaultMailSystem implements MailSystemInterface {
     // line-ending format appropriate for your system. If you need to
     // override this, adjust $conf['mail_line_endings'] in settings.php.
     $mail_body = preg_replace('@\r?\n@', $line_endings, $message['body']);
-    // For headers, PHP's API suggests that we use CRLF normally,
-    // but some MTAs incorrectly replace LF with CRLF. See #234403.
-    $mail_headers = join("\n", $mimeheaders);
+    // Use here also the variable MAIL_LINE_ENDINGS
+    $mail_headers = join($line_endings, $mimeheaders);
 
     // We suppress warnings and notices from mail() because of issues on some
     // hosts. The return value of this method will still indicate whether mail
