3 var _ = require('lodash');
4 var path = require('path');
5 var config = require('../environment');
6 var ImapListener = require("mail-listener2");
7 var MailServerIn = require('../../models').MailServerIn;
8 var MailMessage = require('../../models').MailMessage;
9 var MailRoom = require('../../models').MailRoom;
10 var Contact = require('../../models').Contact;
11 var ContactEmail = require('../../models').ContactEmail;
12 var sequelize = require('../../models').sequelize;
16 create: function (doc) {
20 function onUpdate(doc) {
21 if (!doc.changed('state') && !doc.changed('source') && doc.username === imap.imap._config.user) {
22 console.log("MAIL - Account " + doc.username + " IMAP UPDATED");
28 function onRemove(doc) {
29 if (doc.username === imap.imap._config.user) {
30 console.log("MAIL - Account " + doc.username + " IMAP DESTROY");
36 function onSave(doc) {
43 imap = new ImapListener({
44 username: doc.username,
45 password: doc.password,
50 rejectUnauthorized: false
52 mailbox: doc.mailbox, // mailbox to monitor
53 searchFilter: [doc.filter], // the search filter being used after an IDLE notification has been retrieved
54 markSeen: true, // all fetched email willbe marked as seen and not fetched next time
55 fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,,
57 streamAttachments: false
58 }, // options to be passed to mailParser lib.
59 attachments: true, // download attachments as they are encountered to the project directory
61 directory: path.join(config.root, 'server', 'files', 'attachments', '/')
62 } // specify a download directory for attachments
67 imap.on("server:connected", function () {
68 console.log("MAIL - Account " + doc.username + " IMAP CONNECTED");
69 // SETUP CONNECTED STATUS
72 .then(function (msi) {
78 .catch(function (err) {
83 imap.on("server:disconnected", function () {
84 console.log("MAIL - Account " + doc.MailAccountId + " IMAP DISCONNECTED");
87 .then(function (msi) {
93 .catch(function (err) {
98 imap.on("error", function (err) {
99 console.log("MAIL - Account " + doc.MailAccountId + " IMAP ERROR", err);
102 .then(function (msi) {
109 .catch(function (err) {
114 imap.on("mail", function (msg, seqno, attributes) {
117 var _mFrom, _mTo, _mCc, _mBcc;
119 function createMailMessage(mailRoom) {
124 messageId: msg.messageId,
125 subject: msg.subject,
126 from: _.pluck(msg.from, 'address').join(';'),
127 to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
128 cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
129 bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
131 html: msg.html || null,
132 text: msg.text || null,
133 MailAttachments: msg.attachments || [],
134 MailRoomId: mailRoom.id,
138 function createFrom(mailMessage) {
139 _mMessage = mailMessage;
144 email: _mMessage.from
147 email: _mMessage.from
152 function createTo(mFrom, created) {
154 var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
157 tos.forEach(function (email) {
158 promises.push(ContactEmail
172 function createCc(mTo) {
174 var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
175 _mTo = _.map(mTo, function (elm) {
179 ccs.forEach(function (email) {
180 promises.push(ContactEmail
194 function createBcc(mCc) {
196 var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
197 _mCc = _.map(mCc, function (elm) {
201 bccs.forEach(function (email) {
202 promises.push(ContactEmail
216 function setBcc(mBcc) {
217 _mBcc = _.map(mBcc, function (elm) {
224 return sequelize.transaction(function (t) {
226 console.log('msg', msg);
230 messageId: msg.inReplyTo[0]
236 .then(function (mailMessageParent) {
237 return mailMessageParent
240 .then(function (mailRoom) {
245 messageId: msg.messageId,
246 subject: msg.subject,
247 from: _.pluck(msg.from, 'address').join(';'),
248 to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
249 cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
250 bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
252 html: msg.html || null,
253 text: msg.text || null,
254 MailAttachments: msg.attachments || [],
255 MailRoomId: mailRoom.id,
263 .then(function (mailMessage) {
264 _mMessage = mailMessage;
269 email: _mMessage.from
272 email: _mMessage.from
277 .spread(function (mFrom, created) {
279 var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
282 tos.forEach(function (email) {
283 promises.push(ContactEmail
298 .then(function (mTo) {
300 var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
301 _mTo = _.map(mTo, function (elm) {
305 ccs.forEach(function (email) {
306 promises.push(ContactEmail
321 .then(function (mCc) {
323 var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
324 _mCc = _.map(mCc, function (elm) {
328 bccs.forEach(function (email) {
329 promises.push(ContactEmail
344 .then(function (mBcc) {
345 _mBcc = _.map(mBcc, function (elm) {
379 roomId: msg.messageId,
380 subject: msg.subject,
381 from: _.pluck(msg.from, 'address').join(';'),
382 MailAccountId: doc.MailAccountId
386 .then(function (mailRoom) {
391 messageId: msg.messageId,
392 subject: msg.subject,
393 from: _.pluck(msg.from, 'address').join(';'),
394 to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
395 cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
396 bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
398 html: msg.html || null,
399 text: msg.text || null,
400 MailAttachments: msg.attachments || [],
401 MailRoomId: mailRoom.id,
409 .then(function (mailMessage) {
410 _mMessage = mailMessage;
415 email: _mMessage.from
418 email: _mMessage.from
423 .spread(function (mFrom, created) {
425 var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
428 tos.forEach(function (email) {
429 promises.push(ContactEmail
444 .then(function (mTo) {
446 var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
447 _mTo = _.map(mTo, function (elm) {
451 ccs.forEach(function (email) {
452 promises.push(ContactEmail
467 .then(function (mCc) {
469 var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
470 _mCc = _.map(mCc, function (elm) {
474 bccs.forEach(function (email) {
475 promises.push(ContactEmail
490 .then(function (mBcc) {
491 _mBcc = _.map(mBcc, function (elm) {
522 }).then(function (result) {
523 // Transaction has been committed
524 // result is whatever the result of the promise chain returned to the transaction callback
526 }).catch(function (err) {
527 // Transaction has been rolled back
528 // err is whatever rejected the promise chain returned to the transaction callback
533 imap.on("attachment", function (attachment) {
534 console.log(attachment.path);
540 MailServerIn.afterCreate(function (doc) {
543 // HANDLE ACCOUNT UPDATE/DELETE
544 MailServerIn.afterUpdate(function (doc) {
547 MailServerIn.afterDestroy(function (doc) {