Working with emails : emlParser

by Heather Laine on September 23, 2022 No comments

Have you ever wanted to do more with your emails than just read them in your inbox? Do you need to display them in different formats, or handle parts of them separately? Using LiveCode and emlParser you can easily do post-processing on your emails (if you’ll pardon the pun).

What is it?

emlParser is a deceptively simple library that 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 I’ll talk about in this blog post.

The sample app

Displaying an email in a Browser Object inside LiveCode

This is a very simple sample. It contains a url field, a button to pick your email and a browser object for display. Attached to the button is the only code in the app, and it’s just 9 lines long. This is it:

on mouseUp pButtonNumber
   answer file "select an email"
   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

Looking at the code

What’s 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 we’re 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. And as you see, we have a fully formatted email, now 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 or refile them or… well you get the idea. This library gives you the tools to manipulate your emails.

Available commands and functions

As of writing this post, the emlParser library offers you the following options:

emlToArrayConverts EML data to a LiveCode ArrayemlToArray(pEMLData)
getAttachmentsThis function extracts all the attachments from a pMailArray Array.getMailText(pMailArray, pForcebase64)
getContentExtracts body section from EML data.getContent(pData)
getEmbeddedFileByCIDThis function searches and returns the file embedded in pEMLArray with content ID.getEmbeddedFileByCID (pCID, pEMLArray)
getFullMailContentAsHTMLThis function returns the email in html format, if pForceBase64 is true then codified as base64.getFullMailContentAsHTML
(pMailArray, pForceBase64, 
pEmbedImages)
getHeadersHeader section as String.getHeaders(pData)
getMailTextThis function is intended to obtain simplified version of email body.getMailText(pMailArray, pForcebase64)
See the LiveCode Dictionary for more information on each term.

How to get it

This library is available as part of the Enhancements Pack offer, which ends on 27th September 2022. It is also available separately for purchase in our Extensions Store.

Heather LaineWorking with emails : emlParser

Related Posts

Take a look at these posts

Join the conversation

*