EmailParser.Parse
-----------------

Parses an email.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [EmailParser](component_EmailParser.md) | [5.3](newinversion53.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "EmailParser.Parse"; EmailSource { ; Encoding } ) 

**MBS**( **"EmailParser.Parse";** /\* Parses an email. \*/  
**$EmailSource**; /\* The source code of an email as text.  
e.g. as downloaded via IMAP/POP3 functions in CURL.e.g. $EmailContent \*/   
**$Encoding**) /\* Optional; The text encoding for text parameter.   
Default is native.e.g. UTF8 \*/ 

### 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](https://www.monkeybreadsoftware.com/filemaker/faq.shtml). | 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](TextReadEMLXFile.md) function first to unpack them and pass the email source to EmailParser.Parse function.  
  
If you need to parse msg files from Outlook, please check the msg-extractor python tool. It can be run via [Shell](component_Shell.md) functions to unpack msg files:  
<https://github.com/TeamMsgExtractor/msg-extractor>  
### 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](EmailParserSubject.md)"; $email )\]  
Set Field \[Email Parser::PlainText ; MBS ("[EmailParser.PlainText](EmailParserPlainText.md)"; $email )\]  
Set Field \[Email Parser::HTMLText ; MBS ("[EmailParser.HTMLText](EmailParserHTMLText.md)"; $email )\]  
\#find all addresses   
Set Variable \[$EmailRecordID ; Value:Get(RecordID)\]  
Set Variable \[$i ; Value:0\]  
Set Variable \[$c ; Value:MBS ("[EmailParser.AddressCount](EmailParserAddressCount.md)"; $email )\]  
If \[$c &gt; 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](EmailParserAddress.md)"; $email ; $i ; "name")\]  
 Set Variable \[$emailAddress ; Value:MBS ("[EmailParser.Address](EmailParserAddress.md)"; $email ; $i ; "email")\]  
 Set Variable \[$type ; Value:MBS ("[EmailParser.Address](EmailParserAddress.md)"; $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](EmailParserAttachmentCount.md)"; $email )\]  
If \[$c &gt; 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](EmailParserAttachment.md)"; $email ; $i ; "filename")\]  
 Set Variable \[$content ; Value:MBS ("[EmailParser.Attachment](EmailParserAttachment.md)"; $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](EmailParserInlineCount.md)"; $email )\]  
If \[$c &gt; 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](EmailParserInline.md)"; $email ; $i ; "filename")\]  
 Set Variable \[$content ; Value:MBS ("[EmailParser.Inline](EmailParserInline.md)"; $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](EmailParserHeaderCount.md)"; $email )\]  
If \[$c &gt; 0\]  
 Loop  
 Set Variable \[$HeaderName ; Value:MBS ("[EmailParser.Header](EmailParserHeader.md)"; $email ; $i ; "name")\]  
 If \[$HeaderName = "X-Mailer"\]  
 Set Variable \[$HeaderValue ; Value:MBS ("[EmailParser.Header](EmailParserHeader.md)"; $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.Release](EmailParserRelease.md)"; $email )\]  
Extract email text:

 Set Variable \[$EmailID ; Value:MBS ( "EmailParser.Parse"; email::raw )\]  
Set Field \[email::raw\_plainText ; MBS ( "[EmailParser.PlainText](EmailParserPlainText.md)"; $EmailID )\]  
Set Field \[email::raw\_htmltext ; MBS ( "[EmailParser.HTMLText](EmailParserHTMLText.md)"; $EmailID )\]  
Set Variable \[$r ; Value:MBS ( "[EmailParser.Release](EmailParserRelease.md)"; $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](TextReadEMLXFile.md)"; $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](EmailParserSubject.md)"; $email ) \]   
 Set Variable \[ $r ; Value: MBS ( "[EmailParser.Release](EmailParserRelease.md)"; $email ) \]   
 End If  
End If  
### See also

- [EmailParser.Hash](EmailParserHash.md)
- [EmailParser.List](EmailParserList.md)
- [EmailParser.ParseContainer](EmailParserParseContainer.md)
- [EmailParser.ParseFile](EmailParserParseFile.md)
- [EmailParser.PlainText](EmailParserPlainText.md)
- [EmailParser.Release](EmailParserRelease.md)
- [EmailParser.Source](EmailParserSource.md)
- [EmailParser.Subject](EmailParserSubject.md)
- [IsError](IsError.md)
- [Text.ReadEMLXFile](TextReadEMLXFile.md)

### Release notes

- **Version 12.0**
    - Fixed a problem in email parser, which didn't get multi part right on Linux for [EmailParser.Parse](https://www.mbsplugins.eu/EmailParserParse.shtml), [EmailParser.ParseContainer](https://www.mbsplugins.eu/EmailParserParseContainer.shtml) and [EmailParser.ParseFile](https://www.mbsplugins.eu/EmailParserParseFile.shtml).
- **Version 9.4**
    - Fixed bug in [EmailParser.Parse](http://www.mbsplugins.eu/EmailParserParse.shtml) where html text sometimes contained an extra = on the end.

### Example Databases

- [CURL/Email/Email Parser and Preview](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/Email/Email%20Parser%20and%20Preview.shtml#1ScriptAnchor_)
- [CURL/Email/Email Parser](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/Email/Email%20Parser.shtml#1ScriptAnchor_)

### Blog Entries

- [MBS Plugin Advent calendar: 23 - MailParser](https://www.mbsplugins.de/archive/2023-12-23/MBS_Plugin_Advent_calendar_23_/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 11.6pr3](https://www.mbsplugins.de/archive/2021-12-14/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.4pr1](https://www.mbsplugins.de/archive/2019-08-08/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)

This function is free to use.

Created 24th September 2015 , last changed 24th February 2026

  
[EmailParser.MessageID](EmailParserMessageID.md) - [EmailParser.ParseContainer](EmailParserParseContainer.md)

[HTML Version](EmailParserParse.shtml)