« MBS Plugin Advent cal… | Home | MBS Plugin Advent cal… »

MBS Plugin Advent calendar: 23 - MailParser

Door 23 - MailParser

Fact of the day
With the MBS FileMaker Plugin you can not only analyze or send mails. You can also retrieve mails directly from your email account via CURL using an IMAP connection.

Our advent calendar is slowly coming to an end and so we have already reached door 23. In door 21 we saw how to send emails with inline graphics. Today we would like to look at how we can find out what is in an email if we only have the mail file. For this we have the component EmailParser.

The mail file can either be in a container, as a file on the hard disk or we have the source code of an email. In all cases, we can read the data from the mail with the MBS FileMaker Plugin. For each case we have a separate function that loads the mail into the working memory and returns the reference with which we can work. If we have a sourecode of the email we use the function EmailParser.Parse to parse the email. Do we have the mail as a file on the disk then we use the function EmailParser.ParseFile. In our example, we place the email file in a container. To parse this mail we use the function EmailParser.ParseContainer

Set Variable [ $Mail ; Value: MBS( "EmailParser.ParseContainer"; DoortTwentyThree::Mail ) ] 

Now, of course, we want to know where the mail comes from and whether it has been sent to other people. We have all this information available as addresses which we can read out using the EmailParser.Address function by specifying the index. In order to know how many addresses are involved, we query this number with EmailParser.AddressCount. Now we can loop through the individual addresses. The loop starts at index zero and runs up to index $AdressCount-1. As we go through the addresses, we want to divide them into two groups. One group stands for the sender (Sender and From) and the other group for the different recipients (TO, CC and BCC). With the function EmailParser.Address we can query not only the address, but also the type and the name of the address. In the parameters of the function we first enter the mail reference and the address index. This is followed by the selector, in which we specify which information we want. You can choose from the following selectors: type, name or email. We want the email and also the type with which we can then assign the addresses to the correct group. Depending on which group the address belongs to, it will be appended to the text in the variable $From or $To.

Set Variable [ $AddressCount ; Value: MBS( "EmailParser.AddressCount"; $Mail ) ] 
Set Variable [ $AddressIndex ; Value: 0 ] 
Set Variable [ $From ; Value: "" ] 
Set Variable [ $To ; Value: "" ] 
Loop
	Set Variable [ $Address ; Value: MBS( "EmailParser.Address"; $Mail; $AddressIndex; "email" ) ] 
	Set Variable [ $AddressType ; Value: MBS( "EmailParser.Address"; $Mail; $AddressIndex; "type" ) ] 
	If [ $AddressType = "Sender"  or  $AddressType = "From" ] 
		Set Variable [ $From ; Value: $From  & $Address & "¶" ] 
	Else If [ $AddressType = "TO"  or  $AddressType = "CC" or $AddressType = "BCC" ] 
		Set Variable [ $To ; Value: $To  & $Address & "¶" ] 
	End If
	Set Variable [ $AddressIndex ; Value: $AddressIndex + 1 ] 
	Exit Loop If [ $AddressIndex  ≥ $AddressCount ] 
End Loop
Set Field [ DoortTwentyThree::To ; $To ] 
Set Field [ DoortTwentyThree::From ; $From ] 

It would also be good to know what the mail is about, so we read the subject with EmailParser.Subject.

Set Variable [ $Subject ; Value: MBS( "EmailParser.Subject"; $Mail ) ] 
Set Field [ DoortTwentyThree::Subject ; $Subject ]

We can also use the EmailParser.ReceiveDate and EmailParser.SentDate functions to get the send and receive dates.

Set Variable [ $RecDate ; Value: MBS( "EmailParser.ReceiveDate"; $Mail ) ] 
Set Field [ DoortTwentyThree::ReceiveDate ; $RecDate ] 
# 
Set Variable [ $SentDate ; Value: MBS( "EmailParser.SentDate"; $Mail ) ] 
Set Field [ DoortTwentyThree::SentDate ; $SentDate ] 

We can either query the content of the mail as plain text or we can directly retrieve the HTML in which the formatting can also be recognized. We can then also display this in a web viewer, for example.

Set Variable [ $PlainText ; Value: MBS( "EmailParser.PlainText"; $Mail ) ] 
Set Field [ DoortTwentyThree::PlainText ; $PlainText ] 

Set Variable [ $HTML ; Value: MBS( "EmailParser.HTMLText"; $Mail ) ] 
Set Field [ DoortTwentyThree::HTML ; $HTML ] 

Now only the attachments are missing. For the attachments, we differentiate between normal attachments and inline graphics. If we use the EmailParser.AttachmentCount function to determine the number of attachments, we only determine the number of standard attachments. As with the addresses, we also have the option of querying various values with the EmailParser.Attachment function. The selectors we can use here are Filename, MimeType, MimeVersion, ContentType, ContentTransferEncoding, ContentDisposition, ContentDescription, contentId, text or container. In our case, we only want a list of the filenames of the attachments.

Set Variable [ $AttachmentCount ; Value: MBS( "EmailParser.AttachmentCount"; $Mail ) ] 
Set Variable [ $AttachmentIndex ; Value: 0 ] 
Set Variable [ $FileNames ; Value: "" ] 
Loop
	Set Variable [ $AttachmentName ; Value: MBS( "EmailParser.Attachment"; $Mail; $AttachmentIndex; "Filename" ) ] 
	Set Variable [ $FileNames ; Value: $FileNames & $AttachmentName & "¶" ] 
	Set Variable [ $AttachmentIndex ; Value: $AttachmentIndex + 1 ] 
	Exit Loop If [ $AttachmentIndex  ≥ $AttachmentCount ] 
End Loop
Set Field [ DoortTwentyThree::Attatchments ; $FileNames ] 

If you want to write a email directly as a file, you can use the EmailParser.WriteAttachment function. This writes an attachment that is defined via the index to a storage location specified with a path.

Working with InlienAttachments is very similar to the standard attachment. We use the EmailParser.InlineCount function to determine the number of inline attachments and query the information in a loop with the EmailParser.Inline function. The already known selectors are also used here. We can then use the EmailParser.WriteInline function to write the inline graphic to a file again.

Set Variable [ $InlineCount ; Value: MBS( "EmailParser.InlineCount"; $Mail ) ] 
Set Variable [ $InlineIndex ; Value: 0 ] 
Set Variable [ $InlineFileNames ; Value: "" ] 
Loop
	Set Variable [ $InlineName ; Value: MBS( "EmailParser.Inline"; $Mail; $InlineIndex; "Filename" ) ] 
	Set Variable [ $InlineFileNames ; Value: $InlineFileNames & $InlineName & "¶" ] 
	Set Variable [ $InlineIndex ; Value: $InlineIndex + 1 ] 
	Exit Loop If [ $InlineIndex  ≥ $InlineCount ] 
End Loop
Set Field [ DoortTwentyThree::InlineGraphics ; $InlineFileNames ] 

When we have finished our work, we release the storage address for the email with EmailParser.Free.

Set Variable [ $r ; Value: MBS( "EmailParser.Free"; $Mail ) ] 

Have a look to see if Santa Claus has sent you an e-mail and analyze its content. I look forward to seeing you again tomorrow for the last door.


Monkeybread Software Logo with Monkey with Santa hat
22 👈 23 of 24 👉 24

You can download the example file here: Advent2023.fmp12.

23 12 23 - 10:34