diff --git a/includes/mail.inc b/includes/mail.inc
index a97c788f09..33031b859b 100644
--- a/includes/mail.inc
+++ b/includes/mail.inc
@@ -10,7 +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_MAJOR_VERSION') && PHP_MAJOR_VERSION < 8 ) {
+  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");
+}
 
 
 /**
@@ -360,8 +372,6 @@ interface MailSystemInterface {
  *   The content of the email as a string with formatting applied.
  */
 function drupal_wrap_mail($text, $indent = '') {
-  // Convert CRLF into LF.
-  $text = str_replace("\r", '', $text);
   // See if soft-wrapping is allowed.
   $clean_indent = _drupal_html_to_text_clean($indent);
   $soft = strpos($clean_indent, ' ') === FALSE;
@@ -436,9 +446,9 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
   $urls = _drupal_html_to_mail_urls();
   $footnotes = '';
   if (count($urls)) {
-    $footnotes .= "\n";
+    $footnotes .= MAIL_LINE_ENDINGS;
     for ($i = 0, $max = count($urls); $i < $max; $i++) {
-      $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
+      $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . MAIL_LINE_ENDINGS;
     }
   }
 
@@ -529,7 +539,7 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
         // Horizontal rulers
         case 'hr':
           // Insert immediately.
-          $output .= drupal_wrap_mail('', implode('', $indent)) . "\n";
+          $output .= drupal_wrap_mail('', implode('', $indent)) . MAIL_LINE_ENDINGS;
           $output = _drupal_html_to_text_pad($output, '-');
           break;
 
diff --git a/modules/system/system.mail.inc b/modules/system/system.mail.inc
index 9a17f55f6f..21adc830f0 100644
--- a/modules/system/system.mail.inc
+++ b/modules/system/system.mail.inc
@@ -19,8 +19,9 @@ class DefaultMailSystem implements MailSystemInterface {
    *   The formatted $message.
    */
   public function format(array $message) {
+    $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
     // Join the body array into one string.
-    $message['body'] = implode("\n\n", $message['body']);
+    $message['body'] = implode($line_endings, $message['body']);
     // Convert any HTML to plain-text.
     $message['body'] = drupal_html_to_text($message['body']);
     // Wrap the mail body for sending.
@@ -62,9 +63,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 vcariable 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
