Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] html = false with attachments #332

Open
krm35 opened this issue Jan 12, 2023 · 0 comments
Open

[BUG] html = false with attachments #332

krm35 opened this issue Jan 12, 2023 · 0 comments

Comments

@krm35
Copy link

krm35 commented Jan 12, 2023

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: "[email protected]",
        to: "[email protected]",
        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);
        });
    }


}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant