Skip to content

Content is undefined if the email contains an attachment #58

Description

@GTP95

Greetings,

I noticed that when I parse an email that contains an attachment, the content property is undefined (while instead, it works correctly if the email has no attachment). My code is like this (it's actually split between two files, but it gives you an idea):

const parsedEmail = parse(email)
console.log("Email's content: ", parsedEmailObject.content);
                  if (parsedEmailObject.content != null) {
                    //Extract email's body only if it actually exists

                    const bodyAsHTML = new TextDecoder().decode(
                      objectToUInt8array(parsedEmailObject.content)
                    );
                    console.log("HTML body: ", bodyAsHTML); // TODO: what if email isn't in HTML format? How do I detect this? Probably it is enough to inspect the 'contentType' parameter
                    const bodyAsText = extractEmailBodyFromHTML(bodyAsHTML);
                    console.log("Body: ", bodyAsText);
                    domEmail.body(bodyAsText);
                  } else domEmail.body(" ");

If the email doesn't have attachmments, this code correctly extracts the email's body, while if it has attachments, it fails due to parsedEmailObject.content being undefined. My questions are:

  1. How should I extract the email's body when the email has one or more attachments?
  2. How do I extract the attachments?

Thank you for your help,

GTP

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions