Fix parsing of emails attached to other emails in PST files
MODIFY
CHANGES.txt
CHANGES.txt
@@ -17,6 +17,8 @@ Release 1.26 - ???
* Great optimization in ForkParser (TIKA-3237).
+ * Fix parsing of emails attached to other emails in PST files (TIKA-3004).
+
Release 1.25 - 11/25/2020
* Fix inconsistent license in xmpcore (TIKA-3204).

MODIFY
tika-parser-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/pst/OutlookPSTParser.java
tika-parser-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/pst/OutlookPSTParser.java
@@ -25,12 +25,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
-import com.pff.PSTAttachment;
-import com.pff.PSTException;
-import com.pff.PSTFile;
-import com.pff.PSTFolder;
-import com.pff.PSTMessage;
-import com.pff.PSTRecipient;
import org.apache.tika.exception.TikaException;
import org.apache.tika.extractor.EmbeddedDocumentExtractor;
import org.apache.tika.extractor.EmbeddedDocumentUtil;
@@ -48,6 +42,13 @@ import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
+import com.pff.PSTAttachment;
+import com.pff.PSTException;
+import com.pff.PSTFile;
+import com.pff.PSTFolder;
+import com.pff.PSTMessage;
+import com.pff.PSTRecipient;
+
/**
* Parser for MS Outlook PST email storage files
*/
@@ -115,6 +116,23 @@ public class OutlookPSTParser extends AbstractParser {
if (pstFolder.getContentCount() > 0) {
PSTMessage pstMail = (PSTMessage) pstFolder.getNextChild();
while (pstMail != null) {
+ parseMailAndAttachments(handler, pstMail, embeddedExtractor);
+ pstMail = (PSTMessage) pstFolder.getNextChild();
+ }
+ }
+
+ if (pstFolder.hasSubfolders()) {
+ for (PSTFolder pstSubFolder : pstFolder.getSubFolders()) {
+ handler.startElement("div", createAttribute("class", "email-folder"));
+ handler.element("h1", pstSubFolder.getDisplayName());
+ parseFolder(handler, pstSubFolder, embeddedExtractor);
+ handler.endElement("div");
+ }
+ }
+ }
+
+ private void parseMailAndAttachments(XHTMLContentHandler handler, PSTMessage pstMail,
+ EmbeddedDocumentExtractor embeddedExtractor) throws SAXException, IOException, TikaException {
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("", "class", "class", "CDATA", "embedded");
attributes.addAttribute("", "id", "id", "CDATA", pstMail.getInternetMessageId());
@@ -131,19 +149,6 @@ public class OutlookPSTParser extends AbstractParser {
parserMailItem(handler, pstMail, mailMetadata, embeddedExtractor);
handler.endElement("div");
-
- pstMail = (PSTMessage) pstFolder.getNextChild();
- }
- }
-
- if (pstFolder.hasSubfolders()) {
- for (PSTFolder pstSubFolder : pstFolder.getSubFolders()) {
- handler.startElement("div", createAttribute("class", "email-folder"));
- handler.element("h1", pstSubFolder.getDisplayName());
- parseFolder(handler, pstSubFolder, embeddedExtractor);
- handler.endElement("div");
- }
- }
}
private void parserMailItem(XHTMLContentHandler handler, PSTMessage pstMail, Metadata mailMetadata,
@@ -231,6 +236,12 @@ public class OutlookPSTParser extends AbstractParser {
try {
PSTAttachment attach = email.getAttachment(i);
+ PSTMessage attachedEmail = attach.getEmbeddedPSTMessage();
+ if (attachedEmail != null) {
+ parseMailAndAttachments(xhtml, attachedEmail, embeddedExtractor);
+ continue;
+ }
+
// Get the filename; both long and short filenames can be used for attachments
String filename = attach.getLongFilename();
if (filename.isEmpty()) {

TIKA-3004: Fix parsing of emails attached to other emails in PST files