Topics   All   MacOS (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Deprecated   Guides   Examples   Videos
New in version: 9.5   10.0   10.1   10.2   10.3   10.4   10.5   11.0   11.1   11.2    Statistic    FMM    Blog  

EmailParser.Parse

Parses an email.

Component Version macOS Windows Linux Server FileMaker iOS SDK
EmailParser 5.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "EmailParser.Parse"; EmailSource { ; Encoding } )   More

Parameters

Parameter Description Example Flags
EmailSource The source code of an email as text.
e.g. as downloaded via IMAP/POP3 functions in CURL.
$EmailContent
Encoding The text encoding for text parameter.
Default is native.
Possible encoding names: ANSI, ISO-8859-1, Latin1, Mac, Native, UTF-8, DOS, Hex, Base64 or Windows. More listed in the FAQ.
UTF8 Optional

Result

Returns Reference number or error.

Description

Parses an email.
If no error occurs, the parser will separate subject, html and plain text, attachments and inline graphics, all addresses and header entries. You can then use other functions to query values.

If you expect UTF-8 and you have the text in a field already, you may want to pass UTF-8 here for encoding.

If you have EMLX files, please use Text.ReadEMLXFile function first to unpack them and pass the email source to EmailParser.Parse function.

Examples

Parse an email and copy data into records:

Set Variable [$email; Value:MBS("EmailParser.Parse"; Email Parser::Input)]
If [MBS("IsError") = 1]
    Show Custom Dialog ["Failed to parse email."; Middle($email; 7; Length($email))]
    Exit Script []
End If
#Show text:
Set Field [Email Parser::Subject; MBS("EmailParser.Subject"; $email)]
Set Field [Email Parser::PlainText; MBS("EmailParser.PlainText"; $email)]
Set Field [Email Parser::HTMLText; MBS("EmailParser.HTMLText"; $email)]
#find all addresses
Set Variable [$EmailRecordID; Value:Get(RecordID)]
Set Variable [$i; Value:0]
Set Variable [$c; Value:MBS("EmailParser.AddressCount"; $email)]
If [$c > 0]
    Go to Related Record [Show only related records; From table: “Address”; Using layout: “Address” (Address)]
    Go to Layout [“Address” (Address)]
    Delete All Records [No dialog]
    Loop
        Set Variable [$Name; Value:MBS("EmailParser.Address"; $email; $i; "name")]
        Set Variable [$emailAddress; Value:MBS("EmailParser.Address"; $email; $i; "email")]
        Set Variable [$type; Value:MBS("EmailParser.Address"; $email; $i; "type")]
        New Record/Request
        Set Field [Address::Type; $type]
        Set Field [Address::Email; $emailAddress]
        Set Field [Address::Name; $name]
        Set Field [Address::EmailID; $EmailRecordID]
        Commit Records/Requests [Skip data entry validation; No dialog]
        #next
        Set Variable [$i; Value:$i + 1]
        Exit Loop If [$i = $c]
    End Loop
End If
#find all attachments
Set Variable [$i; Value:0]
Set Variable [$c; Value:MBS("EmailParser.AttachmentCount"; $email)]
If [$c > 0]
    Go to Related Record [Show only related records; From table: “Attachment”; Using layout: “Attachment” (Attachment)]
    Go to Layout [“Attachment” (Attachment)]
    Delete All Records [No dialog]
    Loop
        Set Variable [$Name; Value:MBS("EmailParser.Attachment"; $email; $i; "filename")]
        Set Variable [$content; Value:MBS("EmailParser.Attachment"; $email; $i; "container")]
        New Record/Request
        Set Field [Attachment::Content; $content]
        Set Field [Attachment::FileName; $name]
        Set Field [Attachment::EmailID; $EmailRecordID]
        Commit Records/Requests [Skip data entry validation; No dialog]
        #next
        Set Variable [$i; Value:$i + 1]
        Exit Loop If [$i = $c]
    End Loop
End If
#find all inline graphics
Set Variable [$i; Value:0]
Set Variable [$c; Value:MBS("EmailParser.InlineCount"; $email)]
If [$c > 0]
    Go to Related Record [Show only related records; From table: “InlineGraphics”; Using layout: “InlineGraphics” (InlineGraphics)]
    Go to Layout [“InlineGraphics” (InlineGraphics)]
    Delete All Records [No dialog]
    Loop
        Set Variable [$Name; Value:MBS("EmailParser.Inline"; $email; $i; "filename")]
        Set Variable [$content; Value:MBS("EmailParser.Inline"; $email; $i; "container")]
        New Record/Request
        Set Field [InlineGraphics::Content; $content]
        Set Field [InlineGraphics::FileName; $name]
        Set Field [InlineGraphics::EmailID; $EmailRecordID]
        Commit Records/Requests [Skip data entry validation; No dialog]
        #next
        Set Variable [$i; Value:$i + 1]
        Exit Loop If [$i = $c]
    End Loop
End If
Go to Layout [original layout]
#find a header
Set Variable [$i; Value:0]
Set Variable [$c; Value:MBS("EmailParser.HeaderCount"; $email)]
If [$c > 0]
    Loop
        Set Variable [$HeaderName; Value:MBS("EmailParser.Header"; $email; $i; "name")]
        If [$HeaderName = "X-Mailer"]
            Set Variable [$HeaderValue; Value:MBS("EmailParser.Header"; $email; $i; "value")]
            Set Field [Email Parser::Mailer; $HeaderValue]
        End If
        #next
        Set Variable [$i; Value:$i + 1]
        Exit Loop If [$i = $c]
    End Loop
End If
#cleanup
Set Variable [$r; Value:MBS("EmailParser.Free"; $email)]

Extract email text:

Set Variable [$EmailID; Value:MBS( "EmailParser.Parse"; email::raw)]
Set Field [email::raw_plainText; MBS( "EmailParser.PlainText"; $EmailID )]
Set Field [email::raw_htmltext; MBS( "EmailParser.HTMLText"; $EmailID )]
Set Variable [$r; Value:MBS( "EmailParser.Free"; $EmailID)]

Read emlx file:

# emlx test in file Contacts

Set Variable [ $path ; Value: "/Users/cs/Desktop/28213.emlx" ]
Set Variable [ $emailSourceText ; Value: MBS( "Text.ReadEMLXFile"; $path; "UTF-8") ]
If [ MBS("IsError") = 0 ]
    Set Variable [ $email ; Value: MBS( "EmailParser.Parse"; $EmailSourceText; "UTF8") ]
    If [ MBS("IsError") = 0 ]
        Show Custom Dialog [ "Email subject" ; MBS( "EmailParser.Subject"; $email ) ]
        Set Variable [ $r ; Value: MBS( "EmailParser.Free"; $email ) ]
    End If
End If

See also

Release notes

  • Version 9.4
    • Fixed bug in EmailParser.Parse where html text sometimes contained an extra = on the end.

Example Databases

Blog Entries

Created 24th September 2015, last changed 20th September 2020


EmailParser.MessageID   -   EmailParser.ParseContainer

Feedback: Report problem or ask question.




Links
MBS Xojo PDF Plugins

Start Chat