From c8278e0ae65fc2b835803c1808688a594f24cc7d Mon Sep 17 00:00:00 2001 From: Anna Vernidub Date: Thu, 9 Jun 2016 16:07:41 +0300 Subject: [PATCH] Add nodemailer sendgrid transport for sending emails (works with transportOptions in the config.js) --- app/services/Export.js | 4 ++-- app/services/Mail.js | 24 ++++++++++++------------ config.js | 4 ++++ configurationSchema.js | 20 ++++++++++---------- package.json | 1 + 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/services/Export.js b/app/services/Export.js index ba13418..3a4dde0 100644 --- a/app/services/Export.js +++ b/app/services/Export.js @@ -242,8 +242,8 @@ exports.exportUserRegistrations = function (email, requestedDate) { filename = 'MDG_monthly_report_' + date.subtract('months', 1).format('MM_YYYY') + '.xlsx', workbook = excelBuilder.createWorkbook('./', filename); - User.findOne({}, null, { sort: { timeCreated: 1 } }).exec(function (err, user) { - oldest = moment(user.timeCreated).startOf('month'); + User.find({ timeCreated: { $exists: true } }).sort({ timeCreated: 1 }).limit(1).exec(function (err, user) { + oldest = moment(user[0].timeCreated).startOf('month'); while (date >= oldest) { months.push({ diff --git a/app/services/Mail.js b/app/services/Mail.js index 6001630..81cbab6 100644 --- a/app/services/Mail.js +++ b/app/services/Mail.js @@ -1,5 +1,5 @@ var - directTransport = require('nodemailer-direct-transport'), + sgTransport = require('nodemailer-sendgrid-transport'), nodemailer = require('nodemailer'), Configuration = require('../helpers/Configuration'), fs = require('fs'), @@ -28,7 +28,6 @@ function getOptionsForRegistrationEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.user.email, subject: "Activate your account", - generateTextFromHTML: true, html: html }; } @@ -48,12 +47,11 @@ function getOptionsForSubscriptionEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.subscription.email, subject: "MDG schedule export", - generateTextFromHTML: true, html: html, attachments: [ { - fileName: 'survey_' + params.subscription._survey._id + '.zip', - contents: new Buffer(params.data, 'binary') + filename: 'survey_' + params.subscription._survey._id + '.zip', + content: new Buffer(params.data, 'binary') } ] }; @@ -72,12 +70,11 @@ function getOptionsForReportEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.email, subject: "MDG account report", - generateTextFromHTML: true, html: html, attachments: [ { - fileName: params.filename, - contents: params.data, + filename: params.filename, + content: params.data, contentType: 'application/vnd.ms-excel' } ] @@ -98,7 +95,6 @@ function getOptionsForForgotUsernameEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.email, subject: "Your MDG usernames", - generateTextFromHTML: true, html: html }; } @@ -117,7 +113,6 @@ function getOptionsForResetPasswordEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.user.email, subject: "MDG reset password link", - generateTextFromHTML: true, html: html }; } @@ -138,14 +133,19 @@ function getOptionsForPasswordChangedEmail (params, fileName, subject) { from: Configuration.get('mail.from.' + subject), to: params.user.email, subject: "MDG reset password confirmation", - generateTextFromHTML: true, html: html }; } exports.sendMail = function (params, subject) { var - mailTransport = nodemailer.createTransport(directTransport({})), + options = { + auth: { + api_user: Configuration.get('mail.transportOptions.user'), + api_key: Configuration.get('mail.transportOptions.key') + } + }, + mailTransport = nodemailer.createTransport(sgTransport(options)), fileName = process.cwd() + Configuration.get('mail.' + subject), sendMailOptions = {}; diff --git a/config.js b/config.js index 4b6ddd8..296c71a 100644 --- a/config.js +++ b/config.js @@ -26,6 +26,10 @@ module.exports = { subscription: 'noreply@nokiadatagathering.net', report: 'report@nokiadatagathering.net' }, + transportOptions: { + user: '', + key: '' + }, emailsForUsersReport: null, emailsForUsersReport: [] } diff --git a/configurationSchema.js b/configurationSchema.js index bf7df1b..8d6d62e 100644 --- a/configurationSchema.js +++ b/configurationSchema.js @@ -291,21 +291,21 @@ module.exports = { } } }, - transport: { - type: String, - title: 'Mail transport', - description: 'Defines mail transport protocol', - defaults: 'Direct' - }, transportOptions: { title: 'Mail transport Options', description: 'Defines method for sending e-mails', children: { - service: { + user: { + type: String, + title: 'Api user', + description: 'Username for SendGrid', + defaults: '' + }, + key: { type: String, - title: 'Service', - description: 'The name of send mail service', - defaults: 'Sendmail' + title: 'Api key', + description: 'Password for SendGrid', + defaults: '' } } }, diff --git a/package.json b/package.json index 7a83dde..f7e935c 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "msexcel-builder": "0.0.2", "nodemailer": "^2.3.2", "nodemailer-direct-transport": "^3.0.7", + "nodemailer-sendgrid-transport": "^0.2.0", "passport": "0.1.17", "passport-http": "~0.2.2", "passport-local": "0.1.6",