I’m using Perl & MAIL::IMAPClient to save emails from Gmail as .txt files using the IMAPClient method:
message_to_file
These files seem to contain the attachments of emails encoded as text.
Here’s some of the text file:
--0015174c1274ee7ca60495ca69d5
Content-Type: video/3gpp; name="20101112233055.3gp"
Content-Disposition: attachment; filename="20101112233055.3gp"
Content-Transfer-Encoding: base64
X-Attachment-Id: 1353288501407252480-1
AAAAHGZ0eXAzZ3A0AAADADNncDRtcDQxM2dwNgAFHyltZGF0AAABthAwrMK9/Mue7fM+95wsf9P8
WI7mPzzp/ikijbucv72j7OywVGuh5kBzo89Zra6PihxZg0zadDqihZFpsPJeG36Ihk9qZW+LLQ2u
NEd96vsqgpnLFnhhwGBWgL2Xpt0cXkW....[A LOT MORE CHARS]....AAAQAAAALAAAAAQAAABRzdHN6
AAAAAAAAACAAAAChAAAAIHN0Y28AAAAAAAAABAABHNoAASMaAALYFwAFHeU=
--0015174c1274ee7ca60495ca69d5--
I can’t find any method that will save the attachment separately. Is there any way to do this via parsing?
What you’ve got there is the raw text of a MIME-encoded email message. Most languages have a general purpose MIME library for parsing these. A quick search on CPAN reveals that
MIME::Parser might do the trick:
This will dump the various parts of the email stored in the plain text file test.txt to /var/tmp. Check out the MIME::Parser docs for further options.