chadxz / imap-simple

Wrapper over node-imap, providing a simpler api for common use cases
MIT License
245 stars 79 forks source link

How to fetch email as HTML body ? #54

Closed wiflux closed 3 years ago

wiflux commented 5 years ago

Currently I am getting email body including headers and inline attachment. How to get or parse this into html format ?

Example email :- --00000000000027fdc7057d257064\r\nContent-Type: multipart/alternative; boundary="00000000000027fdc4057d257063"\r\n\r\n--00000000000027fdc4057d257063\r\nContent-Type: text/plain; charset="UTF-8"\r\n\r\nI am fine.\r\n\r\n\r\n[image: kakatua.jpeg]\r\n\r\nOn Sun, Dec 16, 2018 at 9:05 PM <dharminder@dp360crm.com> wrote:\r\n\r\n> Hi,\r\n> How are you ?\r\n> *Dharminder Team*\r\n> *DP360 CRM*\r\n>\r\n\r\n\r\n-- \r\nDharmender Singh Negi\r\nPHP Developer\r\n\r\n--00000000000027fdc4057d257063\r\nContent-Type: text/html; charset="UTF-8"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n<div dir=3D"ltr"><div><br></div>I am fine.=C2=A0<div><br></div><div><br><di=\r\nv><div><img src=3D"cid:ii_jpr1ztly0" alt=3D"kakatua.jpeg" width=3D"276" hei=\r\nght=3D"183"><br></div></div></div></div><br><div class=3D"gmail_quote"><div=\r\n dir=3D"ltr">On Sun, Dec 16, 2018 at 9:05 PM &lt;<a href=3D"mailto:dharmind=\r\ner@dp360crm.com">dharminder@dp360crm.com</a>&gt; wrote:<br></div><blockquot=\r\ne class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s=\r\nolid rgb(204,204,204);padding-left:1ex"><p>Hi,<br>\r\nHow are you ?</p>\r\n<div style=3D"letter-spacing:0px"><em><span style=3D"line-height:5px"><stro=\r\nng>D<span style=3D"line-height:10px">harminder Team</span></strong></span><=\r\n/em></div>\r\n\r\n<div style=3D"letter-spacing:0px"><em><span style=3D"line-height:10px"><str=\r\nong>DP360 CRM</strong></span></em></div>\r\n\r\n\r\n\r\n\r\n\r\n</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=\r\n class=3D"gmail_signature">Dharmender Singh Negi<br>PHP Developer<br></div>\r\n\r\n--00000000000027fdc4057d257063--\r\n--00000000000027fdc7057d257064\r\nContent-Type: image/jpeg; name="kakatua.jpeg"\r\nContent-Disposition: inline; filename="kakatua.jpeg"\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <ii_jpr1ztly0>\r\nX-Attachment-Id: ii_jpr1ztly0\r\n\r\n/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMSEhUSEhMVFhUXFxUVFRUXFxUVFxUVFRUWFxUV\r\nFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0lHyUtLS0tLS0tLTAt\r\nLS0tLS0tLS0tLS0tLS8tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLf/AABEIALcBFAMBIgACEQED\r\nEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAFAAIDBAYBBwj/xABAEAABBAEBBAcDCQYGAwAAAAABAAID\r\nEQQhBRIxQQYiUWFxgZEHE6EUMkJSkrHB0fAjYnKC4fEVJDNzorJTk9L/xAAaAQACAwEBAAAAAAAA\r\nAAAAAAACAwABBAUG/8QALhEAAgIBAwIFAgYDAQAAAAAAAAECAxEEITESQQUTIlFxMpFCUmGBobEU\r\nNOEj/9oADAMBAAIRAxEAPwCPbEnFVdnE2u7RktO2e1CuRsvpDUDk2d67E1RTcVpTMzW5A964CuSB\r\nSxM7ULKaHMatLsSHRAXSMjAL3NaNNSaGvej+HtKGPqvka00CASBYJoUeHFLt4wRB+JincNELxtu4\r\n7iAJW6gkcrDfneY5jlp2ooJGuFtII7j5FJhHLDIHaJhyKUkoQqeTVVdXkGQQfNaqPfqq/v1GJLKw\r\nRrfUKwSyyIblIiQqGYNFuikkaIAGY6phnoJmU+ih0sy0VMCaL/ylSsktCGSK/jOTGxTReY1TMbSi\r\njKmDUqUQGg5s5ttCJNiVLZTaCKtCijsOjHYoZrd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EWSt3ZXODCR81rSTcl\r\nHqu3Wk7vOq05J2M0jfYT7unuMr3xFpYzi4RsBc3wJPEDwYq5F+ZEq5MbJWljxbTxH4jsI7VncDY7\r\nhNGw/Rdd9rWG789PVbdmyY217x7rfLHFGIy0WHNDnPdvtsbodqNPmlNbjV1g5rmgua1wB3nftCxg\r\nawWSXFrjXAbvHgk2aeU2madLr/IU0u6+z9xkzlTYdVbIHu3ve/cpriG7hc4nf92wVY3bdy10BUcm\r\nG5g3i5vzGOIBJ6z37nu7qi4USTw0KJ1y9jNGyJfhdokuwN0XEJpyeAtKcY9UklqisoysaY0+0klG\r\nsEGOXbXUlRDhcvYvZltkzY+442+M7hPaOLfgQPJJJZdVFSreS0bN5XN5JJeWs2ZshweG7Qk3nvcf\r\npOc71cT+KpR8VxJen7GCRtNhu4LStd1UkkcOBiOQm1FkM0SSQTCfAFyxRtWcaYriSx53Mc36hZbb\r\nQbJZRtJJSYplrBnJNItubwSST6N1uFHgo5OCqONHuvCSSu2KT2DXJq8KTQK3K7RJJPi9jVEzeRIQ\r\n4lpIOosGtCRY8NAomPeSHbxsCgdLqqq+NVpS4kiVklsmDKEW8tF2GLQanTUUao1Viq1rS1bihB4l\r\nx0r5x0B1IrhRSSU82fuyeXD2ROMUcN59a6b7ufHneqkOzhVsJa4VRNuA3TYG7dULNdlpJIfOsTzl\r\ngeXF7YK7sMNFbz7oWd46ltkGuWpJ7rUcUdkAueQCDRcSNDY4rqSvzZvuxsa4eyNFCzRJJJQcf//Z\r\n--00000000000027fdc7057d257064--\r\n

HZSamir commented 5 years ago

+1

vitalragaz commented 5 years ago

You can convert the mailpart of the text to a buffer and than to an ASCII string. As example:

imaps.connect(config).then(function (connection) {
    return connection.openBox('INBOX').then(function () {
        var searchCriteria = [
            '1:5'
        ];
        var fetchOptions = {
            bodies: ['HEADER', 'TEXT'],
        };
        return connection.search(searchCriteria, fetchOptions).then(function (messages) {
            messages.forEach(function (item) {
                var all = _.find(item.parts, { "which": "TEXT" })
                var html = (Buffer.from(all.body, 'base64').toString('ascii'));
                console.log(html)
            });
        });
    });
});

EDIT:

A more friendly way is by using simpleparser from nodemailer/mailparser:

imaps.connect(config).then(function (connection) {
    return connection.openBox('INBOX').then(function () {
        var searchCriteria = [
            '1:5'
        ];
        var fetchOptions = {
            bodies: ['HEADER', 'TEXT', ''],
        };
        return connection.search(searchCriteria, fetchOptions).then(function (messages) {
            messages.forEach(function (item) {
                var all = _.find(item.parts, { "which": "" })
                var id = item.attributes.uid;
                var idHeader = "Imap-Id: "+id+"\r\n";
                simpleParser(idHeader+all.body, (err, mail) => {
                    console.log(mail.subject)
                    console.log(mail.html)
                    ....
                });
            });
        });
    });
});
walrus781 commented 5 years ago

I have similar issue, but in html object i have false, any advices, pls, i'm really stucked on this :(

dropofwill commented 4 years ago

Probably a little late for @walrus781 , but for anyone else that stumbles upon this.

I had the same issue with html: false, but it was because I didn't notice that you need to include '' in the list of bodies (e.g. bodies: ['HEADER', 'TEXT', '']). So basically all that was sent to the parser was the id header + undefined.

UnKnoWn-Consortium commented 4 years ago

@dropofwill Actually you can even drop "HEADER" and "TEXT" from the criteria if you are going to let Mailparser do the job as "" means getting the the entire message (header + body) in node-imap.