SHARE
TWEET

Untitled

a guest Dec 13th, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var MailListener = require("mail-listener3");
  4. var fs = require('fs');
  5.  
  6. class Receiver {
  7.  
  8.     startReceiver() {
  9.         return new Promise((resolve, reject) => {
  10.             var mailsList = [];
  11.             var mailListener = new MailListener({
  12.                 username: "nicobelich96@gmail.com",
  13.                 password: "blablabla",
  14.                 host: "imap.gmail.com",
  15.                 port: 993,
  16.                 tls: true,
  17.                 connTimeout: 10000,
  18.                 authTimeout: 5000,
  19.                 tlsOptions: {rejectUnauthorized: false},
  20.                 mailbox: "INBOX",
  21.                 searchFilter: ["UNSEEN"],
  22.                 markSeen: false,
  23.                 fetchUnreadOnStart: true,
  24.                 mailParserOptions: {streamAttachments: true},
  25.                 attachments: true,
  26.                 attachmentOptions: { directory: "attachments/" }
  27.             });
  28.  
  29.             mailListener.start(); // start listening
  30.  
  31.             mailListener.on("server:connected", function () {
  32.                  console.log("imapConnected " + mailListener.imap);
  33.             });
  34.  
  35.             mailListener.on("server:disconnected", function () {
  36.                 // console.log("imapDisconnected");
  37.             });
  38.  
  39.             mailListener.on("error", function (err) {
  40.                 reject(err);
  41.             });
  42.  
  43.             mailListener.on("mail", function (mail, seqno, attributes) {
  44.                 mailsList.push(mail);
  45.                   // console.log(mail);
  46.             });
  47.  
  48.             mailListener.on("attachment", function (attachment) {
  49.                 var output = fs.createWriteStream("./attachments/" + attachment.generatedFileName);
  50.                 attachment.stream.pipe(output);
  51.             });
  52.  
  53.             mailListener.on("done", function(attachment){
  54.                 resolve(mailsList);
  55.             });
  56.         });
  57.     }
  58. }
  59.  
  60. module.exports = Receiver;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top