3 var _ = require('lodash');
4 var nodemailer = require('nodemailer');
5 var smtpTransport = require('nodemailer-smtp-transport');
7 var MailRoom = require('../../models').MailRoom;
8 var MailMessage = require('../../models').MailMessage;
9 var ContactEmail = require('../../models').ContactEmail;
10 var Contact = require('../../models').Contact;
13 console.log('New Smtp Instance...');
15 this.smtp = nodemailer.createTransport(smtpTransport({
27 Smtp.prototype.send = function(mailMessage) {
29 var mMessage = mailMessage;
32 if (mMessage.inReplyTo) {
36 messageId: mMessage.inReplyTo
39 .then(function(mailMessageParent) {
40 return mailMessageParent
43 .then(function(mailRoom) {
47 .addMailMessage(mMessage);
68 .spread(function(contact, created) {
71 var tos = mMessage.to ? mMessage.to.split(';') : [];
76 tos.forEach(function(email) {
98 .then(function(contacts) {
100 var tos = _.map(contacts, function(elm) {
108 var ccs = mMessage.cc ? mMessage.cc.split(';') : [];
110 ccs.forEach(function(email) {
111 promises.push(Contact
132 .then(function(contacts) {
134 var ccs = _.map(contacts, function(elm) {
142 var bccs = mMessage.bcc ? mMessage.to.split(';') : [];
144 bccs.forEach(function(email) {
145 promises.push(Contact
166 .then(function(contacts) {
168 var bcs = _.map(contacts, function(elm) {
177 status: mMessage.status,
181 subject: mMessage.subject,
185 'X-Laziness-level': 1000
189 if (mMessage.MailAttachments) {
190 options.attachments = _.map(mMessage.MailAttachments, function(elm) {
197 that.smtp.sendMail(options, function(error, response) {
199 console.error('error', error);
204 .catch(function(err) {
210 messageId: response.messageId,
213 .catch(function(err) {
219 .catch(function(err) {
220 console.error('err', err);
226 id: mMessage.MailRoomId
229 subject: mMessage.subject,
232 MailAccountId: that.doc.MailAccountId
235 .spread(function(mailRoom) {
239 .addMailMessage(mMessage);
260 .spread(function(contact, created) {
263 var tos = mMessage.to ? mMessage.to.split(';') : [];
268 tos.forEach(function(email) {
269 promises.push(Contact
290 .then(function(contacts) {
292 var tos = _.map(contacts, function(elm) {
300 var ccs = mMessage.cc ? mMessage.cc.split(';') : [];
302 ccs.forEach(function(email) {
303 promises.push(Contact
324 .then(function(contacts) {
326 var ccs = _.map(contacts, function(elm) {
334 var bccs = mMessage.bcc ? mMessage.to.split(';') : [];
336 bccs.forEach(function(email) {
337 promises.push(Contact
358 .then(function(contacts) {
360 var bcs = _.map(contacts, function(elm) {
369 status: mMessage.status,
373 subject: mMessage.subject,
377 'X-Laziness-level': 1000
381 if (mMessage.MailAttachments) {
382 options.attachments = _.map(mMessage.MailAttachments, function(elm) {
389 that.smtp.sendMail(options, function(error, response) {
391 console.error('error', error);
396 .catch(function(err) {
402 messageId: response.messageId,
405 .catch(function(err) {
411 .catch(function(err) {
412 console.error('err', err);
417 module.exports = Smtp;