Mail.app- 'Remove Attachments' corrupting mails

I have a weird issue that I've not seen mentioned anywhere. Wondering if anyone can give me any debugging tips.


I'm using Mail 5.0 in Lion 10.6.1 (issue was occurring in 10.6.0 as well).


I have Mail connected to Exchange Server 2010.


I have a fairly tight restriction on mailbox sizes so I have to remove attachments from emails before I file them.


My problem is this; about 1 in 3 times I remove an attachment the 'replacement' e-mail is full of gibberish characters.


Typically if I look at the raw source of the e-mail then the text version of the mail is retained correctly- it seems to be the Base64 encoded version that is corrupted.


Any idea what I can do to debug this? I have tried removing the envelope files and removing/adding the account back in and re-syncing it but the issue has reappeared.


I've reported this to Apple on http://bugreports.apple.com but have not had a response.


Thanks in advance.

Unibody MacBook Pro 15" 2.8GHz, Mac OS X (10.6.1)

Posted on Aug 22, 2011 7:48 AM

Reply
116 replies

Apr 1, 2015 10:05 AM in response to thanimal

4 years after this problem was first reported, it is still a big problem in Yosemite. Apple store staff say they have never seen it before (3 staff) and sent me to the "Genius Bar". The Genius simply dismissed the problem blaming the fact that i was using gmail with apple's mail and calendar apps; and told me that i needed to contact gmail to resolve the problem! I felt under pressure to use an iCloud email address, which, after years of using a gmail address for all my academic work (very successfully with windows and outlook) is something i don't think i should have to do.

Apr 1, 2015 3:08 PM in response to trewq321

With all respect to the apple genius - this issue is not relevant to gmail or any other it is an apple mail issue about how mail handles the deletion of the attachments. I have tried to find a pattern like is it only when attachments are in received gmails or any other but it is indescriminent.


If you read this thread from the beginning you will see that some very knowlegable folks have looked at this and if not been able to give a fix, but provided a very simple work around that will help you.


If after 4 years the apple engineers have not sorted this out it sounds like it is not an issue for them or a priority

Apr 7, 2015 10:18 AM in response to Screaming.Pict

Got tired of waiting for Apple to fix this attachment issue, so I put together a simple AppleScript. It's free for anyone to use, but no warranties are given. To try it do the following:

  1. Create a new AppleScript file using Script Editor
  2. Copy my script below (all text between the long lines) into the file
  3. Save it in your folder ~/Library/Scripts/Applications/Mail/ (or create that folder if needed) and give it some suitable name
  4. In Script Editor, make sure you have turned ON the preference checkbox "Show Script menu in menu bar"

Now you can just select any corrupted email in Mail and run this AppleScript from the Script menu. And voila, your mail should be fixed.

(The Script menu will change into a rotating cog wheel when the script is still executing. It might take a second or two since it needs to fire up TextEdit to do the text replacements, so be patient.)


-----------------------------------------------------

--Creator: Rich In Space, 7th April 2015

tell application "Mail"

set theMsgs to the selection

repeat with theMsg in theMsgs

set mID to id of theMsg


if my isBadEncoding(theMsg) then

set theDir to POSIX path of (path to library folder from user domain) & "Mail/V2/"

set shellScr to "mdfind -onlyin " & theDir & " 'kMDItemFSName == " & quote & mID & ".emlx" & quote & "'"

set mPath to do shell script shellScr

if mPath ≠ "" then my FixEnc(POSIX file mPath as string)

end if

end repeat

end tell


on isBadEncoding(theMsg)

tell application "Mail" to set theCont to content of theMsg

set theContSum to summarize theCont


if length of theContSum ≥ 10 then

set chars to 10

else

set chars to length of theContSum

end if

set checkSum to 0

repeat with i from 1 to chars

set checkSum to checkSum + (id of character i of theContSum)

end repeat


return checkSum / chars > 256

end isBadEncoding


on FixEnc(mPath)

tell application "TextEdit"

open file mPath


set EncPar to "Content-Transfer-Encoding: base64

"

set every paragraph of front document where it = EncPar to ""


save front document

close front document saving yes

end tell

end FixEnc

-----------------------------------------------------

Apr 8, 2015 6:20 AM in response to Bruce 55

Bruce 55 wrote:

Currently I am removing the "base64" at the end of the email in text edit - it would be lovely to have a script to do this as it is a right royal pain in the ...... right now - so thanks for looking into it - much appreciated.

This is exactly what the AppleScript above is attempting to do, but obviously there is something more to the story.

Bruce 55, do you have any mail you could share with me here in raw source format – from before you ran my script?

Apr 9, 2015 6:39 AM in response to Rich In Space

Revised version of the AppleScript above (now compatible with different line endings):


-----------------------------------------------------

--Creator: Rich In Space, 7th April 2015, Revised: 9th April 2015



tell application "Mail"

set theMsgs to the selection

repeat with theMsg in theMsgs

set mID to id of theMsg


if my isBadEncoding(theMsg) then

-- if not my isBadEncoding(theMsg) then

set theDir to POSIX path of (path to library folder from user domain) & "Mail/V2/"

set shellScr to "mdfind -onlyin " & theDir & " 'kMDItemFSName == " & quote & mID & ".emlx" & quote & "'"

set mPath to do shell script shellScr

if mPath ≠ "" then my FixEnc(POSIX file mPath as string)

end if

end repeat

end tell





on isBadEncoding(theMsg)

tell application "Mail" to set theCont to content of theMsg

set theContSum to summarize theCont


if length of theContSum ≥ 10 then

set chars to 10

else

set chars to length of theContSum

end if

set checkSum to 0

repeat with i from 1 to chars

set checkSum to checkSum + (id of character i of theContSum)

end repeat


return checkSum / chars > 256

end isBadEncoding





on FixEnc(mPath)

tell application "TextEdit"

open file mPath

set vDoc to front document


set vFirstLine to paragraph 1 of vDoc

set vLineDel to character -1 of vFirstLine

set vLineDel2 to character -2 of vFirstLine

if id of vLineDel2 < 32 then set vLineDel to vLineDel2 & vLineDel


set EncPar to "Content-Transfer-Encoding: base64" & vLineDel

set NHits to count (every paragraph of vDoc where it = EncPar)

repeat NHits times

set first paragraph of vDoc where it = EncPar to ""

end repeat


save vDoc

close vDoc saving yes

end tell

end FixEnc

-----------------------------------------------------

Apr 9, 2015 6:48 AM in response to Bruce 55

Bruce 55 wrote:


happy to privately send you one of the affected emails - how would you like me to do this

Thanks Bruce 55, but no need this time. Another user (hr101) in the parallel thread above sent me one of his emails. After looking at it, it turned out to be just a line-ending character that I missed (+ a small bug in TextEdit). Please, use the revised AppleScript above and let me know how it worked.

Apr 10, 2015 11:26 AM in response to Rich In Space

Been following this thread for years. Numerous manual fixes have not worked as I often have complex nested threads with attachments.


But unbelievably, this script has worked on some troublesome emails.


HUGE THANKS. HUGE KARMA.


If it needs to be said, if a user can write an script to fix this, then Apple can certainly fix it in Mail.


And Apple should be paying you for your work.

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.

Mail.app- 'Remove Attachments' corrupting mails

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