Parsing your emails using emlParser
emlParser is a library addon for LiveCode and is available for purchase from our Extensions store if you do not already own it. It allows you to convert emails to arrays within LiveCode. Once there, you can output your array in whatever format you'd like, or otherwise manipulate it for the purposes of your app. It has a handy built in function to display the email in a browser object within LiveCode, which we'll work through in this lesson.
Download and Install emlParser
After purchasing you will find a download button for emlParser in your account, or if you purchased the Enhancements bundle then you will install it as part of that. Download the library or the bundle, run the included installer from your bundle, and you should be ready to start using emlParser.
Create the Stack UI
Create a new stack from the File menu, and drag it out to a good size. From the tools palette, drag on a field (1), a button (2) and a Browser Widget (3). Resize the field as shown to give you a long thin field suitable for holding a url address. Name the field "fileurl". Position the button as shown next to the field, and name it "..." This is to indicate to the user that it is a selector button. Position the Browser object underneath both of these and resize it so that it is big enough to display the contents of an email.
Add the Code
There are only 9 lines of code in this stack, and they live in the button. Open the Script Editor for the button by right clicking and choosing "Edit Script". Enter this code (and don't forget to click "Apply"!):
on mouseUp pButtonNumber answer file "choose an email file" if it is empty then exit to top put it into field "fileurl" local tEMLArray, tresult put emlToArray(url("file:" & field "fileurl")) into tEMLArray put getFullMailContentAsHTML(tEMLArray, false, true ) into tresult set the htmltext of widget "browser" to tresult end mouseUp
Analysing the Code
Whats going on here? First, we invite the user to pick an email by clicking the button with on it. If they pick one, great, we proceed. If not, nothing happens. Assuming an email was selected, the path to the email file (it) is put into the field we have provided to receive it, named fileurl.
Next, we declare a local variable were calling tEMLArray, and another called tresult. Then we use the main function from the emlParser library, emlToArray to get the contents of the file located at the filepath in the fileurl field, and put those contents into an array, tEMLArray. So far so good. What would we like to do with that array?
On this occasion, we are using another function of the emlParser library, the somewhat lengthily named getFullMailContentAsHTML function, to do exactly what it says on the tin. It gets the full content of the email as HTML, and it puts it into our local variable tresult.
For our final trick, we take tresult and display it in the browser widget.
Testing the Result
Switch to browse mode and choose an email file.
Note: to choose an email you need the actual file, ending in .eml, to select. If you are on a Mac, you can just drag a file out of your Mail client to your desktop, and then select it. On Windows you might need to save out the file using the "Save as.." dialog. In Gmail, you can download an email to your desktop as a .eml format. Other mail clients may have different methods for saving out your emails as an actual file.
You should now have a fully formatted email, showing within the LiveCode Browser object.
Maybe you want to print your emails to pdf. Maybe you want to scrape all the attachments out of them and file them somewhere consistent. Or perhaps you want just the text of the emails to scan them for specific content. This library gives you the tools to manipulate your emails.
After you have installed emlParser all its available commands and functions will appear in the LiveCode Dictionary. Just enter emlParser in the search box to see what other operations are available to you.
Add your comment