Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion: Extract information from a .msg file

I would like to make an application where the user drags in a .msg file into my web app. Then ColdFusion would extract the following fields: name, sender email, subject, etc. and pre-populate it in a form ready to be submitted. I have googled "read .msg ColdFusion" but can't seem to find any information. When I do a FileRead() I see only gibberish ÐÏࡱá > þÿ because it's encrypted. Is this even possible? I hope someone can point me in the right direction. I am also open to trying a different approach.

like image 394
isurfbecause Avatar asked May 13 '26 18:05

isurfbecause


2 Answers

As @imthepitts mentioned, the file is not encrypted, it is just in binary. However, it is not enough to just load the bytes with fileReadBinary(). You need a tool that understands the format of .msg files and can parse its contents.

If you do a quick search, there are a bunch of tools capable of parsing .msg files (most are java or .net). One such tool is POI's HSMF (Horrible Stupid Mail Format). It is already built into CF. So you may want to start there.

Here is a quick and dirty example translated from the HSMF examples:

<cfscript>
    pathToFile = "c:/path/to/someMessage.msg";
    MAPIMessage = createObject("java", "org.apache.poi.hsmf.MAPIMessage");
    message = MAPIMessage.init(pathToFile);


    try {
        WriteOutput("From: "& message.getDisplayFrom() &"<hr>");
        WriteOutput("To: "& message.getDisplayTo() &"<hr>");
        WriteOutput("CC: "& message.getDisplayCC() &"<hr>");
        WriteOutput("BCC: "& message.getDisplayBCC() &"<hr>");
        WriteOutput("Subject: "& message.getSubject() &"<hr>");
        WriteOutput("Body: "& message.getTextBody() &"<hr>");
    } catch (org.apache.poi.hsmf.exceptions.ChunkNotFoundException e) {
        WriteDump(e);
    }
</cfscript>
like image 88
Leigh Avatar answered May 18 '26 04:05

Leigh


FileRead() is for text files. If you're seeing gibberish, it's because it's a binary file. Try FileReadBinary().

like image 21
imthepitts Avatar answered May 18 '26 06:05

imthepitts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!