HiraokaHyperTools / msgreader

40 stars 9 forks source link

Cannot convert MSG without TO field #28

Closed bahamut657 closed 2 years ago

bahamut657 commented 2 years ago

Hi and good job for the nice work.

During mail conversion, sometimes, it happens that some emails have empty TO field and this module fails to convert them.

Here you are the traceback: Error: Missing 'To' e-mail address! at Object.emlformat.build (/home/madwork/ivprojects/msgreader/node_modules/eml-format/lib/eml-format.js:360:13) at Object.<anonymous> (/home/madwork/ivprojects/msgreader/index.js:67:11) at Module._compile (internal/modules/cjs/loader.js:955:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10) at Module.load (internal/modules/cjs/loader.js:811:32) at Function.Module._load (internal/modules/cjs/loader.js:723:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) at internal/main/run_main_module.js:17:11

Is it possible to remove the blocking error, preventing it from conversion and saving the EML with an empty TO field? Thank you and best regards

kenjiuno commented 2 years ago

Hi

During mail conversion,

Could you please show the conversion code you mention about?

bahamut657 commented 2 years ago

Here you are:

const fs = require("fs");
const MsgReader = require("@kenjiuno/msgreader").default;
const mime = require("mime-types");
const newLineExpr = new RegExp("\n\r", "g");

const inputMailFile = process.argv[2];
const outputMailFile = process.argv[3];

if (!inputMailFile || !outputMailFile) {
  console.log("Invalid parameters");
  process.exit(2);
}

const msgFileBuffer = fs.readFileSync(inputMailFile);
const inputMsg = new MsgReader(msgFileBuffer);
const msgInfo = inputMsg.getFileData();

const {
  body,
  senderName,
  senderEmail,
  subject,
  recipients,
  attachments,
  headers,
} = msgInfo;

const protoEML = {
  from: senderEmail,
  to: recipients
    .map(({ name, email, recipType }) =>{
            console.log(recipType);
      return recipType === "to" ? { name, email } : null
    }
    )
    .filter((entry) => entry !== null),
  cc: recipients
    .map(({ name, email, recipType }) =>
      recipType === "cc" ? { name, email } : null
    )
    .filter((entry) => entry !== null),
  bcc: recipients
    .map(({ name, email, recipType }) =>
      recipType === "bcc" ? { name, email } : null
    )
    .filter((entry) => entry !== null),
  subject,
  text: body,
  html:
    "<html><head></head><body>" +
    body.replace(newLineExpr, "<br />") +
    "</body></html>",
  attachments: [],
};
kenjiuno commented 2 years ago

This code fragment seems to have no problem.

Error: Missing 'To' e-mail address!

Please verify that tested msg file has at least one to recipient entry. This demo site can verify msg file: https://hiraokahypertools.github.io/msgreader_demo/

Maybe dummy to recipient is required in case no to entry is available.

For example, adding the following dummy (as fallback) may help this situation:

{ "to": [{ "name": "undisclosed-recipients" }] }

Changed code:

function applyFallbackRecipients(array, fallback) {
  if (array.length === 0) {
    array.push(fallback);
  }
  return array;
}

const protoEML = {
  from: senderEmail,
  to: applyFallbackRecipients(recipients
    .map(({ name, email, recipType }) => {
      console.log(recipType);
      return recipType === "to" ? { name, email } : null
    }
    )
    .filter((entry) => entry !== null), { name: "undisclosed-recipients" }),
  cc: recipients
    .map(({ name, email, recipType }) =>
      recipType === "cc" ? { name, email } : null
    )
    .filter((entry) => entry !== null),
  bcc: recipients
    .map(({ name, email, recipType }) =>
      recipType === "bcc" ? { name, email } : null
    )
    .filter((entry) => entry !== null),
  subject,
  text: body,
  html:
    "<html><head></head><body>" +
    body.replace(newLineExpr, "<br />") +
    "</body></html>",
  attachments: [],
};
bahamut657 commented 2 years ago

It works perfectly with this fallback.

Thanks for sharing fallback mechanism.

Closing this issue. Best regards