mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 382 forks source link

Decode html BODY #791

Closed lucydjo closed 4 years ago

lucydjo commented 4 years ago

Hi !

I want to get email body -> HTML

imaps.connect(config).then(function (connection) {

    connection.openBox('INBOX').then(function () {

        // Fetch emails from the last 24h
        var delay = 24 * 3600 * 1000;
        var yesterday = new Date();
        yesterday.setTime(Date.now() - delay);
        yesterday = yesterday.toISOString();
        var searchCriteria = ['ALL', ['SINCE', yesterday]];
        var fetchOptions = { bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)','1.1'], struct: true };

        // retrieve only the headers of the messages
        return connection.search(searchCriteria, fetchOptions);
    }).then(function (messages) {

        var attachments = [];

        messages.forEach(function (message) {
            var parts = imaps.getParts(message.attributes.struct);

            console.log(message.parts[1].body);
        });

        return Promise.all(attachments);
    }).then(function (attachments) {
        console.log(attachments);
        // =>
        //    [ { filename: 'cats.jpg', data: Buffer() },
        //      { filename: 'pay-stub.pdf', data: Buffer() } ]
    });
});

Return ->

parts: [
    {
      which: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
      size: 193,
      body: [Object]
    },
    {
      which: '1.1',
      size: 1865,
      body: '=C2=A0\r\n' +
        '\r\n' +
        '[   ]( https://fr.sendinblue.com )  Finalisez la cr=C3=A9ation de votre =\r\n' +
        'compte  =C2=A0  Votre adresse e-mail a bien =C3=A9t=C3=A9 enregistr=C3=A9e =\r\n' +
        'sur Sendinblue.=C2=A0Pour valider votre compte et commencer =C3=A0 envoyer =\r\n' +
        "des campagnes d'emailing, veuillez compl=C3=A9ter votre profil en cliquant =\r\n" +
        'sur le lien ci-dessous :  =C2=A0\r\n' +
        '[ CONFIRMER MON ADRESSE E-MAIL ]( https://app.sendinblue.=\r\n' +
        'com/account/register/8a5564435b0767aa72c1883f17cf96a0.=\r\n' +
        '573fe0d9e0e7dfd0176527d3d14ba1be871be2e55a432ff20c98134c614736ce )     =\r\n' +
        "=C2=A0  Si vous n'avez pas associ=C3=A9 votre adresse e-mail =C3=A0 un =\r\n" +
        'compte Sendinblue, veuillez ignorer ce message et ne pas cliquer sur le =\r\n' +
        'lien ci-dessus.  =C2=A0  Si vous rencontrez des probl=C3=A8mes avec le =\r\n' +
        "bouton ci-dessus, veuillez copier-coller l'URL suivante dans votre =\r\n" +
        'navigateur Web.\r\n' +
        'https://app.sendinblue.com/account/register/8a5564435b0767aa72c1883f17cf96a=\r\n' +
        '0.573fe0d9e0e7dfd0176527d3d14ba1be871be2e55a432ff20c98134c614736ce   =C2=A0=\r\n' +
        '   =C2=A0\r\n' +
        'Nous restons =C3=A0 votre disposition\r\n' +
        "Visitez notre=C2=A0[ centre d'aide ]( https://help.sendinblue.com/hc/fr =\r\n" +
        ')=C2=A0afin de d=C3=A9couvrir nos guides pratiques et notre FAQ.\r\n' +
        '=C2=A0\r\n' +
        'Vous ne trouvez pas ce que vous cherchez =3F\r\n' +
        "Veuillez=C2=A0[ contacter notre =C3=A9quipe d'assistance ]( https://fr.=\r\n" +
        'sendinblue.com/support/add-ticket ).   =C2=A0   =C2=A0  Sendinblue  55, rue=\r\n' +
        " d'Amsterdam 75008 Paris France  =C2=A0=C2=A0[ Politique anti-spam et =\r\n" +
        "d'emailing ]( https://fr.sendinblue.com/legal/antispampolicy )=C2=A0 =C2=A0=\r\n" +
        ' =C2=A0[ Conditions g=C3=A9n=C3=A9rales ]( https://fr.sendinblue.=\r\n' +
        'com/legal/generalterms )=C2=A0=C2=A0   [  ]( https://www.facebook.=\r\n' +
        'com/SendinblueFrance )  [  ]( https://twitter.com/SendinBlue=5Ffr )  [  ]( =\r\n' +
        'https://www.linkedin.com/company/sendinblue )  [  ]( https://fr.sendinblue.=\r\n' +
        'com/blog )\r\n' +
        '\r\n'
    }
  ],

And i just need html or text. Why so complexe ?

Thx.

mscdex commented 4 years ago

Duplicate of https://github.com/mscdex/node-imap/issues/688.