|
|
|
@ -25,21 +25,23 @@ function parse_mail(sexp_string) {
|
|
|
|
|
|
|
|
|
|
const mail_obj = parse_mail(mail_body);
|
|
|
|
|
|
|
|
|
|
let html = mail_obj["body-html"]
|
|
|
|
|
? mail_obj["body-html"]
|
|
|
|
|
: /* HTML */ `<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<pre style="white-space: pre-wrap">${mail_obj["body-txt"]}</pre>
|
|
|
|
|
</body>
|
|
|
|
|
</html>`;
|
|
|
|
|
let html =
|
|
|
|
|
mail_obj["body-html"] && mail_obj["body-html"].match(/body/i) !== null
|
|
|
|
|
? mail_obj["body-html"]
|
|
|
|
|
: /* HTML */ `<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<pre style="white-space: pre-wrap">${mail_obj["body-txt"]}</pre>
|
|
|
|
|
</body>
|
|
|
|
|
</html>`;
|
|
|
|
|
|
|
|
|
|
const attachments = (await $`mu extract ${path}`).stdout;
|
|
|
|
|
|
|
|
|
|
function getSimpleHeader(body, header_name) {
|
|
|
|
|
const regexp = new RegExp(`:${header_name} (.*)\n`);
|
|
|
|
|
console.log("get simple header", header_name, body);
|
|
|
|
|
return body.match(regexp)[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -57,11 +59,17 @@ function escape(str) {
|
|
|
|
|
return str.replace(/</g, "<");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log({ attachments });
|
|
|
|
|
|
|
|
|
|
const to_download = attachments
|
|
|
|
|
.split("\n")
|
|
|
|
|
.slice(1)
|
|
|
|
|
.map((line) => {
|
|
|
|
|
//someimtes images have the wrong mime type, like for example in photos from straż miejska confirmation emails
|
|
|
|
|
if (line.includes(".jpg") && line.includes("application/octet-stream")) {
|
|
|
|
|
return line.replace("application/octet-stream", "image/jpg");
|
|
|
|
|
} else {
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.filter((line) => line.includes("image"))
|
|
|
|
|
.map((line) => {
|
|
|
|
|
return {
|
|
|
|
@ -119,7 +127,7 @@ const date = await formatDate(getSimpleHeader(mail_body, "date"));
|
|
|
|
|
|
|
|
|
|
html = html.replace(
|
|
|
|
|
/<body>/i,
|
|
|
|
|
`<body>` +
|
|
|
|
|
`<meta charset="utf-8"><body>` +
|
|
|
|
|
/* HTML */ `<h1>${subject}</h1>
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
@ -146,9 +154,10 @@ const output = (await $`mktemp --suffix=.html`).stdout.replace("\n", "");
|
|
|
|
|
|
|
|
|
|
await fs.writeFile(output, html);
|
|
|
|
|
|
|
|
|
|
const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`;
|
|
|
|
|
|
|
|
|
|
console.log("Wrote to", output_file);
|
|
|
|
|
const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`.replace(
|
|
|
|
|
",",
|
|
|
|
|
" "
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await $`wkhtmltopdf ${output} ${output_file}`;
|
|
|
|
|
// console.log(pwd + "output.html");
|
|
|
|
|