Extra line breaks in save IMAP attachment

We accept customer inventory files in CSV or tab-delimited format via email.
I am writing an IMAP message parser that will take certain customer’s files
and save them to a directory to be processed.

I’m having a problem with the CRLF in the saved attachments.

When the attachment is downloaded and viewed from the message, this is what
I see in my editor:


"269.25","0269.250V","NS",45,"FUSE"
"451007","","NS",4725,"FUSE"
"4031865","","NS",5,"ECA"
"9284270808","BC640","NS",4000,"TRANSISTOR"
"5205980-4","","NS",20,"HARDWARE"

But, when I use IMAP functions to get the attachment and save it to a file,
this is what I see when I open the file in my editor:


"269.25","0269.250V","NS",45,"FUSE"

"451007","","NS",4725,"FUSE"

"4031865","","NS",5,"ECA"

"9284270808","BC640","NS",4000,"TRANSISTOR"

"5205980-4","","NS",20,"HARDWARE"


How can I prevent this from happening?

Added this before saving the attachment:


$attachments[$i]['attachment'] = preg_replace('`[\\r\
]+`',"\
",$attachments[$i]['attachment']);