Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var nodemailer = require("nodemailer");
- var smtpTransport = require("nodemailer-smtp-transport");
- var Imap = require("imap");
- var inspect = require("util").inspect;
- const simpleParser = require("mailparser").simpleParser;
- var WebSocketServer = require("ws").Server;
- wss = new WebSocketServer({ port: 5000 });
- // Nodemailer SMTP transporter
- var transporter = nodemailer.createTransport(
- smtpTransport({
- service: "gmail",
- host: "smtp.gmail.com",
- port: 465,
- secure: true,
- auth: {
- user: "bilcenterodense@gmail.com",
- pass: "bco123qwe"
- },
- tls: {
- ciphers: "SSLv3",
- rejectUnauthorized: false
- }
- })
- );
- // Objekt til klient
- var mailObject = {};
- // IMAP hvor vi henter mails (Google)
- var imap = new Imap({
- user: "bilcenterodense@gmail.com",
- password: "bco123qwe",
- host: "imap.gmail.com",
- port: 993,
- tls: true
- });
- var mailOptions = {
- from: "bilcenterodense@gmail.com",
- to: "",
- subject: "",
- text: "" // Skal være en streng!
- };
- // Websocket forbindelse med klient
- wss.on("connection", function(ws) {
- console.log("Websocket Client connected");
- ws.on("message", function(e) {
- var request = JSON.parse(e);
- // console.log(request);
- if (request.type === "test") {
- console.log("Test success: " + request.message);
- ws.send(JSON.stringify("Server recieved: " + request.message));
- }
- if (request.type === "post") {
- mailOptions = {
- from: request.mailObject.from,
- to: request.mailObject.to,
- subject: request.mailObject.subject,
- text: request.mailObject.text
- };
- transporter.sendMail(mailOptions, function(error, info) {
- if (error) {
- console.log(error);
- } else {
- console.log("Email sent: " + info.response);
- }
- });
- }
- if (request.type === "get") {
- function openInbox(cb) {
- imap.openBox("INBOX", true, cb);
- }
- imap.once("ready", function() {
- openInbox(function(err, box) {
- if (err) throw err;
- // IMAP henter ALLE mails fra GMail
- imap.search(["UNSEEN"], function(err, results) {
- if (err) throw err;
- var f = imap.fetch(results, { bodies: "" });
- f.on("message", function(msg, seqno) {
- console.log("Message #%d", seqno);
- var prefix = "(#" + seqno + ") ";
- msg.on("body", function(stream, info) {
- simpleParser(stream, (err, parsed) => {
- mailObject = parsed; // Gemmer seneste mail i mailObject
- console.log(mailObject);
- });
- // console.log(prefix + "Body");
- // stream.pipe(fs.createWriteStream("msg-" + seqno + "-body.txt"));
- });
- msg.once("attributes", function(attrs) {
- console.log(
- prefix + "Attributes: %s",
- inspect(attrs, false, 8)
- );
- });
- msg.once("end", function() {
- console.log(prefix + "Finished");
- });
- });
- f.once("error", function(err) {
- console.log("Fetch error: " + err);
- });
- f.once("end", function() {
- console.log("Done fetching all messages!");
- imap.end();
- });
- });
- });
- });
- imap.once("error", function(err) {
- console.log(err);
- });
- // Når vi er færdige, sender vi mails til klienten
- imap.once("end", function() {
- console.log("Connection ended, sending mails to klient..");
- ws.send(JSON.stringify(mailObject));
- });
- imap.connect();
- }
- });
- ws.on("close", function() {
- ws.close();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement