Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 8.2   8.3   8.4   8.5   9.0   9.1   9.2   9.3   9.4   9.5    Statistic  

EmailParser.Parse

Parses an email.

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

Parameters

Parameter Description Example value
EmailSource The source code of an email. $EmailContent
Encoding Optional
The text encoding for text parameter.
Default is native.
Possible encoding names: ANSI, Arabic-Mac, Arabic-Win, Baltic-Win, CentralEurope-Mac, ChineseSimp-Mac, ChineseSimp-Win, ChineseTrad-Mac, ChineseTrad-Win, Cyrillic-Mac, Cyrillic-Win, EasternEurope-Win, Greek-Mac, Greek-Win, Hebrew-Mac, Hebrew-Win, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-15, Korean-Johab, Korean-Mac, Korean-Win, Latin1, Mac, Native, ShiftJIS-Mac, ShiftJIS-Win, Turkish-Mac, Turkish-Win, UTF-8, DOS or Windows. Pass native to use the native encoding of the current platform.
UTF8

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 than 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.

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)]

See also

Example Databases

Blog Entries

Release notes

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

Created 24th September 2015, last changed 8th March 2017


EmailParser.MessageID   -   EmailParser.ParseContainer

Feedback: Report problem or ask question.




Links
MBS Xojo Plugins