Bruno Camarneiro
Bruno Camarneiro

Reputation: 555

Convert mail atachment to file

I'm checking a mailbox using mail-notifier:

var notifier = require('mail-notifier');

var imap = {
      username: "[email protected]",
      password: "hardcorepassword",
      host: "hardcorehost",
      port: 143, // imap port
      tls: false,
      tlsOptions: { rejectUnauthorized: false }
};

notifier(imap).on('mail',function(mail){
         console.log("GOT MAIL");
         console.log(mail);

}).start();

And I'm able to get all the text but my main objective is get the attachment. I think I also get the attachement because I have access to this info

attachments:
   [ { contentType: 'application/pdf',
       fileName: 'hardcorefilename.pdf',
       contentDisposition: 'attachment',
       transferEncoding: 'base64',
       generatedFileName: 'helios-ip-integration-document-fw260.pdf',
       contentId: 'ba0391e4323aa65cb1204e41aa36a82d@mailparser',
       checksum: '2ad15bae0d1c33bcdf2f35d4839061db',
       length: 789634,
       content: <Buffer 25 50 44 46 2d 31 2e 35 0d 0a 25 b5 b5 b5 b5 0d 0a 31 20
 30 20 6f 62 6a 0d 0a 3c 3c 2f 54 79 70 65 2f 43 61 74 61 6c 6f 67 2f 50 61 67 6
5 73 20 32 20 30 ...> } ] }

I tried to find a module that could do that but with no luck. How can I convert from the encoded content to a pdf document?

Upvotes: 1

Views: 520

Answers (1)

Bruno Camarneiro
Bruno Camarneiro

Reputation: 555

Found it!

require("fs").writeFile("out.pdf", mail.attachments[0].content, 'base64', function(err) {
   console.log(err);
});

Upvotes: 2

Related Questions