Eml parser ... parse mail into subject, body text, html, attachments, etc

Hi there,

has anybody successfully build a workflow which takes in a (binary) eml file and converts the different objects into a json objects? And if so, wants to share this.

Thanks in advance and best regards - Dick

Hi @dickhoning, is there a chance you can use the IMAP Email node to fetch emails instead of parsing eml files? This would seem like the simplest approach to me.

As for parsing eml files, this would require custom code. You could try running something like MQpeng/eml-parse-js: format EML file in browser env (github.com) in the Function node, but I haven’t done this myself so wouldn’t be able to provide any pointers here unfortunately.

@MutedJam Hi Tom, I will take a look at the IMAP Email node and thanks for the tip on iml-parse-js. I already looked at Mailparser :: Nodemailer myself and I was hoping some of my fellow n8n coders could help me with a working example. Cheers

This definitely seems like an interesting case. Maybe someone has actually approached this before in n8n, let’s see :slight_smile:

BTW: the IMAP Email does exactly what I want as far as parsing is concerned. Now, instead of fetching the mail messages via an IMAP account, I’m looking for an option to feed the raw text of an email message into this node …

Did You make any progress on that ?
I’m currently struggling with getting the complete header in raw text format from an incoming email and process it further.
Background is: I want to get Spam mails, get their headers and send these headers as attachment text file to SpamCop

@dickhoning try this with a Code-node, this works well for extracting everything from a .eml file.
one caveat: NODE_FUNCTION_ALLOW_EXTERNAL=mailparser must be set on your installation for this to work.

const { simpleParser } = require('mailparser');

// Get the email content from the Text Node
const emailContent = $input.first().json['data']; 

// Parse the email content using the mailparser library
async function parseEmail(content) {
    return new Promise((resolve, reject) => {
        simpleParser(content)
            .then(mail => {
                // Convert the headers from Map to an object
                const headers = {};
                for (const [key, value] of mail.headers) {
                    headers[key] = value;
                }

                resolve({
                    subject: mail.subject,
                    body: mail.text,
                    from: mail.from.text,
                    to: mail.to.text,
                    headers: headers // Include headers here
                });
            })
            .catch(error => {
                reject('Failed to parse email: ' + error.message);
            });
    });
}

const parsedEmail = await parseEmail(emailContent);

// Return the parsed email data as an array of objects
return [
    {
        json: {
            subject: parsedEmail.subject,
            body: parsedEmail.body,
            from: parsedEmail.from,
            to: parsedEmail.to,
            headers: parsedEmail.headers // Add headers to the output
        }
    }
];

@nabossha I am also facing similar issues, I am trying to parse email headers from .eml file but its in binary type, I tried using this MailParser | Nodemailer, but some where my config is wrong and giving error.

this is my post, I created for help but so far nobody able to fix it. I posted in detail about the errors. How to parse .eml file in n8n

Can you help me on this, how did you set up this mailparser to work? it would be great help from you.