in reply to Remove attachment data from the read file

Instead of parsing the text myself, I'd use Email::Simple and Email::MIME to extract the subject and body, while discarding any attachments. You may have to experiment with messages to figure out how they appear to Email::MIME in the case where they have attachments and where they don't, but that's likely to be far simpler than writing your own parsing code.