Apple Mail software added Carriage Return<0x0D> in a .txt file attachment before each CRLF pair

macOS Mohave 10.14.6

Mail Version 12.4 (3445.104.14)


Recently I traced the problem to Mail. After a search, it appeared that the problem dated back to 2009 as stated in the following discussion messages:

https://discussions.apple.com/thread/2235362


A recent message thread in this community forum: https://discussions.apple.com/thread/251841991 confirms it still existed after 10 years.


There was an external message thread about the same subject and it quoted the 2009 message thread as well at: https://stackoverflow.com/questions/37557912/excel-for-mac-2016-save-as-csv-bad-line-endings


Really hope this to be fixed after such a long time so that Mail can be used for sending text/csv file attachment exactly as original file.


Anyone is aware of any workaround within the Mail app other than changing the file attachment extension name etc?


Is there a bug filed for this issue for Mail? How to file bugs for Apple Mail App?

MacBook Air 13″, macOS 10.14

Posted on Mar 24, 2021 5:44 PM

Reply

Similar questions

7 replies

Mar 25, 2021 1:12 PM in response to AppSC8

If a CSV is generated by Excel on Windows, its line-endings will be '0x0D' carriage returns (CR) when sent to a Mac and will need to be replaced with '0x0A' linefeeds (LF) for normal usage on the Mac. Neither the operating system nor Apple Mail will resolve these mismatched line-ending challenges. I don't have Excel (Mac or Windows) so unclear if when exporting a CSV from Excel on the Mac, if it uses CR, CRLF, or LF line-endings.


That said, if you want to include a CSV file as an Apple Mail attachment, you may want to zip the CSV beforehand, so its transit across the Internet and different servers does not impact its content or line-endings.


I just sent a hand-coded CSV with confirmed LF line endings as an attachment in Apple Mail (macOS 11.2.3) between two different email systems. When I saved the received attachment and verified the line-endings with hexdump, nothing had changed from the original CSV.

Mar 25, 2021 5:51 PM in response to MrHoffman

Hello MrHoffman,


I was trying to clarify the issue I'm facing that it was not specific to CSV files, nor it was related to how line feed was handled on macOS vs other systems even though those information would be helpful for someone encountered CSV file attachment related issues. I was not dealing with Excel or other Microsoft software at all.


When I discovered this problem I was just trying to send a plain txt file attachment as shown in my previous message. And the receiving party kept reporting MD5 mismatch on the file. My initial reaction was actually along the line of some line feed conversion issues across different systems.


But incidentally when I tried using other mail client (web mail interface) this issue did not happen, then I started looking into search Mail related issues and found at least there was one message thread related to this dated back to 2009 https://discussions.apple.com/thread/2235362 and there was no resolution in the end. In that thread, there were detailed discussions about the file attachment in the outgoing mail suggesting wrong handling by Mail which I'm not an expert in that area to comment on.


You brought up a very interesting point in your message that the mail server in the middle could be in the play. So I did a quick test by using my Yahoo email account to send a email with this mail-test.txt file attachment.


First I composed the email in Safari using Yahoo email web page interface, the downloaded mail-test.txt was exactly same as the original file. Then I used Apple Mail to compose the email with the same Yahoo email account (of course, the smtp/pops for the Yahoo account needs to be setup in Mail), but this time the downloaded file had the extra 0x0D in there. It appears this test still point to Mail had something to do with it.


Although it may not be conclusive as each email sent may hop through different servers, but so far all the text attachment emails (>10) sent via Mail all showed this issue. Other web mail clients (Gmail & Yahoo) I've tried (>10) with the same text file attachment would reach destination exactly the same.


Will you be able to do a quick test?

<I wish there is a way I can attach my mail-test.txt to the message which would make things much easier for the test>

  1. Compose a text file with 3 lines.
  2. Check it has 0x0D, 0x0A at the end of each line in a hex editor.
  3. Send an email with this text file as an attachment to another email account.
  4. Check the received file at the other email account whether you see extra blank lines in between/extra 0x0D in hex editor.


I'd like to find out if it is a real problem with Mail that can be replicated or some other factors cause this issue (such as the Mail version I'm using etc).

Mar 26, 2021 3:54 PM in response to MrHoffman

MrHoffman,


Thanks for your reply and sharing your experiences. I'm not sure why it was viewed as to be Microsoft Windows files related issue or CSV specific. At this point, it looks more to be an Apple Mail text file attachment issue.


Sure the workarounds you suggested would be fine for the short term although it forces users to go through extra steps that may not be desirable in the long run. It will be beneficial to understand the root cause and have Apple Mail fixed if it has been broken as documented in https://discussions.apple.com/thread/2235362


To isolate the problem further, I recreated the it without touching Windows or its file formats at all. From macOS terminal a mail-test2.txt was created using vi with 3 lines of text as below, each line was ended with LF 0x0A the unix style:


Sent as an attachment in Mail to my Gmail/Yahoo account, after downloaded an extra 0x0D showed up before each LF 0x0A. Although TextEdit would not show extra blank line since there were no double 0x0D before LF 0x0A.

The same mail-test2.txt was sent by Gmail as an attachment, it was received exactly as the original file without any alteration.


I don't think it is too much to ask Apple Mail be capable sending a text file attachment without modification so the receiving end would get the original content just like many other mail clients are capable of.

Mar 25, 2021 1:28 PM in response to AppSC8

The above is correct.


The linked topic involves an export from Microsoft Office Excel.


Various other discussions involve old Excel versions, too; Office 2011 and earlier.


Use xxd or hexdump or a similar tool on that exported CSV file, and have a look at the data contents.


If the xxd or hexdump shows the \r 0x0d carriage return in the export, ask Microsoft about their export function.


\r 0x0d is a blank line in sequential file encoding on macOS and many other systems. It's how you get a blank line.


If you don't want that blank line and/or Microsoft isn't inclined to fix the export function and/or other steps, zip the file.


Other? Use tr or strings or another tool to trim the data?

https://leemendelowitz.github.io/blog/remove-carriage-return-control-character.html


Or maybe change the export settings?

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_mac-mso_o365b/csv-line-endings/2b4eedaf-22ef-4091-b7dc-3317303d2f71


CSV files are a flaming mess which looks easy, and quickly degenerates into differences in the corner cases.


No, I don't have Office around, so I can't test this.

Mar 25, 2021 2:54 PM in response to MrHoffman

Thanks for your reply and data points.


However, the problem I've traced is not specific to CSV files but in general with any text files. And it is not about how unix vs windows handles the new line differently. The point is Mail should send text file attachment following the standard (whatever that maybe), when a mail client (following the same standard) should be able to retrieve the attached text file exactly as the original file.


The issue was that Mail somehow added an 0x0D before each 0x0D 0x0A(=CR/LF) pair in the text file as an attachment when it reaches other mail systems. Below is a mail-test.txt file I had



When I received this file attachment on Gmail or Yahoo mail, the downloaded file became:

You can see the extra line in the text display and the extra 0x0D (red arrow place) in the hex dump for each line.


I've used other mail client software to send the same text file as an attachment, the downloaded file was exactly the same as the original text file (no extra 0x0D).


Below is the hex dump of the original mail-test.txt as I don't see an attach file option here. If any one of you can do a simple test to see if the problem can be duplicated, that would be great. Please use the .txt as the attached file extension as Mail may handle other file type differently.

40 31 30 30 30 0D 0A 30 30 20 30 31 20 0D 0A 71 0D 0A

Mar 25, 2021 3:41 PM in response to AppSC8

You've seemingly already settled on an answer, and seemingly aren't interested in alternatives nor in other settings, so I see no further reason to discuss this here.


I wish you well with this, but—having chased around sequential file format disagreements in heterogeneous networks and for most of thirty years, and this particular morass further involves disparate mail servers—I'm not particularly hopeful.


SMTP mail was built for ASCII, and constructs including non-ASCII text and MIME encoding and file attachments and all the rest are all... balanced atop that old infrastructure.


Were I to pursue this further, I'd follow what was suggested earlier around settings. I'd also have a look at the MIME-encoded text sent, and what was received.


But more generally, I'd either try a different export format for the CSV as was previously linked, or zip this CSV file entirely and not try to wrangle sequential file formats at all, as chasing Microsoft sequential file formats is something I've had "fun" with for decades.

Mar 26, 2021 8:08 AM in response to AppSC8

Tests? I have spent decades dealing with this exact issue, particularly with Microsoft Windows files and with inter-operations of Windows and with other platforms with their own sequential-file constructs in heterogeneous networks.


I’d zip the file. Or zip “-V”, on certain systems. Or switch the file export format selected to stream LF or DOS.


I’d prefer something other than CSV format too, as that’s less than consistently implemented.


You’re not going to get enough folks to move to matter on sequential file transfers, and those that do will not move on a timescale that’ll help you with this.


I wish you well on your quest. Have fun. Enjoy what you will learn here, too.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Mail software added Carriage Return<0x0D> in a .txt file attachment before each CRLF pair

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.