nodemailer / mailparser

Decode mime formatted e-mails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] html = false with attachments

krm35 opened this issue · comments

commented

Hello,

The mailparser has a bug and skip the html content sometimes, apparently this is due to excess space. I tried to debug without success.

const assert = require("assert");
const fs = require("fs");
const MailComposer = require("nodemailer/lib/mail-composer");
const nodemailerOpenpgp = require("nodemailer-openpgp");
const openpgp = require("openpgp");
const {simpleParser} = require('mailparser');


for (const attachments of [[], [{filename: 'text1.txt', content: 'hello world!'}]]) {

    console.log("try with", attachments, "attachments");

    const mail = new MailComposer({
        from: "from@localhost.com",
        to: "to@localhost.com",
        subject: "subject",
        html: "<p>Hello</p>",
        attachments
    });

    mail['compile']()['build'](async (err, message) => {

        const signer = new nodemailerOpenpgp.Encrypter({
            encryptionKeys: [fs.readFileSync('./pgp-public.pem', 'utf8')],
            signingKey: fs.readFileSync('./pgp-private.pem', 'utf8'),
            passphrase: 'super long and hard to guess secret'
        });

        const chunks = [];

        signer.on('data', chunk => chunks.push(chunk));

        signer.on('end', async () => {
            const {data: decrypted} = await openpgp.decrypt({
                message: await openpgp.readMessage({
                    armoredMessage: Buffer.concat(chunks).toString('utf-8')
                }),
                decryptionKeys: await openpgp.decryptKey({
                    privateKey: await openpgp.readPrivateKey({armoredKey: fs.readFileSync('./pgp-private.pem', 'utf8')}),
                    passphrase: "super long and hard to guess secret"
                })
            });

            // parse without pgp
            parse(message);

            // works
            parse(decrypted.replace(' \n\n ', '\n\n'));
            parse(decrypted.replace('\n\n ', '\n\n'));

            // parse decrypted message doesn't work due to 2 spaces on the first line' \n\n '
            // "Content-Type: multipart/mixed; boundary=\"--_NmP-f651c6a9483b6a0f-Part_1\" \n\n ----_NmP-f651c6a9483b6a0f-Part_1\nContent-Type: text/html; charset=utf-8\nContent-Transfer-Encoding: 7bit\n\n<p>Hello</p>\n----_NmP-f651c6a9483b6a0f-Part_1\nContent-Type: text/plain; name=text1.txt\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment; filename=text1.txt\n\naGVsbG8gd29ybGQh\n----_NmP-f651c6a9483b6a0f-Part_1--\n"
            parse(decrypted);

        });
        signer.end(message);
    });

    function parse(message) {
        simpleParser(message, {}, (err, parsed) => {
            if (attachments.length) {
                assert.strictEqual(parsed.attachments.length, 1);
                assert.strictEqual(parsed.attachments[0].filename, 'text1.txt');
            }
            assert.strictEqual(parsed.html !== false, true);
        });
    }


}