diff --git a/includes/mail.inc b/includes/mail.inc
index a97c788f09..7e127e87fb 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_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");
+}
 
 /**
  * Special characters, defined in RFC_2822.
@@ -360,27 +371,28 @@ 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);
+  $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
+  // Convert line endings.
+  $text = preg_replace('@\r?\n@', $line_endings, $text);
   // See if soft-wrapping is allowed.
   $clean_indent = _drupal_html_to_text_clean($indent);
   $soft = strpos($clean_indent, ' ') === FALSE;
   // Check if the string has line breaks.
-  if (strpos($text, "\n") !== FALSE) {
+  if (strpos($text, $line_endings) !== FALSE) {
     // Remove trailing spaces to make existing breaks hard, but leave signature
     // marker untouched (RFC 3676, Section 4.3).
-    $text = preg_replace('/(?(?<!^--) +\n|  +\n)/m', "\n", $text);
+    $text = preg_replace('/(?(?<!^--) +{$line_endings}|  +{$line_endings})/m', $line_endings, $text);
     // Wrap each line at the needed width.
-    $lines = explode("\n", $text);
+    $lines = explode($line_endings, $text);
     array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent)));
-    $text = implode("\n", $lines);
+    $text = implode($line_endings, $lines);
   }
   else {
     // Wrap this line.
     _drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent)));
   }
   // Empty lines with nothing but spaces.
-  $text = preg_replace('/^ +\n/m', "\n", $text);
+  $text = preg_replace('/^ +{$line_endings}/m', $line_endings, $text);
   // Space-stuff special lines.
   $text = preg_replace('/^(>| |From)/m', ' $1', $text);
   // Apply indentation. We only include non-'>' indentation on the first line.
@@ -411,6 +423,7 @@ function drupal_wrap_mail($text, $indent = '') {
  *   The transformed string.
  */
 function drupal_html_to_text($string, $allowed_tags = NULL) {
+  $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
   // Cache list of supported tags.
   static $supported_tags;
   if (empty($supported_tags)) {
@@ -436,9 +449,9 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
   $urls = _drupal_html_to_mail_urls();
   $footnotes = '';
   if (count($urls)) {
-    $footnotes .= "\n";
+    $footnotes .= $line_endings;
     for ($i = 0, $max = count($urls); $i < $max; $i++) {
-      $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
+      $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . $line_endings;
     }
   }
 
@@ -492,7 +505,7 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
         case '/blockquote':
           if (count($lists)) {
             // Append closing quote for inline quotes (immediately).
-            $output = rtrim($output, "> \n") . "\"\n";
+            $output = rtrim($output, ("> " . $line_endings)) . "\"" . $line_endings;
             $chunk = ''; // Ensure blank new-line.
           }
           // Fall-through
@@ -529,7 +542,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)) . $line_endings;
           $output = _drupal_html_to_text_pad($output, '-');
           break;
 
@@ -623,10 +636,11 @@ function drupal_mail_format_display_name($string) {
  * Callback for array_walk() within drupal_wrap_mail().
  */
 function _drupal_wrap_mail_line(&$line, $key, $values) {
+  $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
   // Use soft-breaks only for purely quoted or unindented text.
-  $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
+  $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? (" " . $line_endings) : $line_endings);
   // Break really long words at the maximum width allowed.
-  $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n", TRUE);
+  $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? (" " . $line_endings) : $line_endings, TRUE);
 }
 
 /**
@@ -671,13 +685,14 @@ function _drupal_html_to_text_clean($indent) {
  * @see drupal_html_to_text()
  */
 function _drupal_html_to_text_pad($text, $pad, $prefix = '') {
+  $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
   // Remove last line break.
   $text = substr($text, 0, -1);
   // Calculate needed padding space and add it.
-  if (($p = strrpos($text, "\n")) === FALSE) {
+  if (($p = strrpos($text, $line_endings)) === FALSE) {
     $p = -1;
   }
   $n = max(0, 79 - (strlen($text) - $p) - strlen($prefix));
   // Add prefix and padding, and restore linebreak.
-  return $text . $prefix . str_repeat($pad, $n) . "\n";
+  return $text . $prefix . str_repeat($pad, $n) . $line_endings;
 }
diff --git a/modules/system/system.mail.inc b/modules/system/system.mail.inc
index 9a17f55f6f..d1fcee1383 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.
@@ -64,7 +65,7 @@ class DefaultMailSystem implements MailSystemInterface {
     $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);
+    $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
