2016-05-20 17:01:28 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write emails to disk. Output is written to ./.mail_output/<email type>.html
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* node ./scripts/write-to-disk.js <email type>
|
|
|
|
*
|
|
|
|
* Where <email type> is one of:
|
|
|
|
* all
|
|
|
|
* newDeviceLoginEmail
|
|
|
|
* passwordChangedEmail
|
|
|
|
* passwordResetEmail
|
|
|
|
* passwordResetRequiredEmail
|
|
|
|
* postVerifyEmail
|
|
|
|
* recoveryEmail
|
2016-08-08 17:49:43 +03:00
|
|
|
* unblockCodeEmail
|
2016-05-20 17:01:28 +03:00
|
|
|
* verificationReminderEmail:first
|
|
|
|
* verificationReminderEmail:second
|
|
|
|
* verifyEmail
|
|
|
|
* verifyLoginEmail
|
|
|
|
*
|
|
|
|
* Emails that are written to disk can be previewed in Firefox
|
|
|
|
* to give a rough idea of how they would render in real life.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var P = require('bluebird')
|
2017-03-08 19:40:21 +03:00
|
|
|
const config = require('../config').getProperties()
|
2017-03-09 20:59:57 +03:00
|
|
|
const error = require('../lib/error')
|
2017-02-24 18:31:07 +03:00
|
|
|
const createSenders = require('../lib/senders')
|
2016-05-20 17:01:28 +03:00
|
|
|
var fs = require('fs')
|
2017-02-24 18:31:07 +03:00
|
|
|
const log = require('../lib/senders/legacy_log')(require('../lib/senders/log')('server'))
|
2016-05-25 12:10:27 +03:00
|
|
|
var mkdirp = require('mkdirp')
|
2016-05-20 17:01:28 +03:00
|
|
|
var path = require('path')
|
|
|
|
|
|
|
|
var OUTPUT_DIRECTORY = path.join(__dirname, '..', '.mail_output')
|
|
|
|
|
2016-05-25 12:10:27 +03:00
|
|
|
var messageToSend = process.argv[2] || ''
|
2016-05-20 17:01:28 +03:00
|
|
|
|
|
|
|
var mailSender = {
|
|
|
|
sendMail: function (emailConfig, done) {
|
|
|
|
var htmlOutputPath = getEmailOutputPath(emailConfig.subject, 'html')
|
|
|
|
fs.writeFileSync(htmlOutputPath, emailConfig.html)
|
|
|
|
|
|
|
|
var textOutputPath = getEmailOutputPath(emailConfig.subject, 'txt')
|
|
|
|
fs.writeFileSync(textOutputPath, emailConfig.text)
|
|
|
|
|
|
|
|
done(null)
|
2016-05-25 12:10:27 +03:00
|
|
|
},
|
2016-05-20 17:01:28 +03:00
|
|
|
|
2016-05-25 12:10:27 +03:00
|
|
|
close: function () {}
|
2016-05-20 17:01:28 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 20:59:57 +03:00
|
|
|
const db = {
|
|
|
|
emailBounces: () => P.resolve([])
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:40:21 +03:00
|
|
|
require('../lib/senders/translator')(config.i18n.supportedLanguages, config.i18n.defaultLanguage)
|
|
|
|
.then(translator => {
|
2017-03-09 20:59:57 +03:00
|
|
|
return createSenders(log, config, error, db, translator, mailSender)
|
2017-03-08 19:40:21 +03:00
|
|
|
})
|
2017-02-16 11:08:27 +03:00
|
|
|
.then((senders) => {
|
2017-03-28 00:55:08 +03:00
|
|
|
const mailer = senders.email._ungatedMailer
|
2016-05-25 12:10:27 +03:00
|
|
|
checkMessageType(mailer, messageToSend)
|
|
|
|
|
|
|
|
ensureTargetDirectoryExists()
|
|
|
|
|
|
|
|
return sendMails(mailer, getMessageTypesToWrite(mailer, messageToSend))
|
2016-05-20 17:01:28 +03:00
|
|
|
})
|
|
|
|
.then(() => {
|
2016-06-24 20:59:52 +03:00
|
|
|
console.info('done')
|
2016-05-20 17:01:28 +03:00
|
|
|
})
|
|
|
|
|
2016-05-25 12:10:27 +03:00
|
|
|
function getEmailOutputPath(subject, extension) {
|
|
|
|
var outputFilename = subject.replace(/\s+/g, '_') + '.' + extension
|
|
|
|
return path.join(OUTPUT_DIRECTORY, outputFilename)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-20 17:01:28 +03:00
|
|
|
function sendMails(mailer, messagesToSend) {
|
|
|
|
return P.all(messagesToSend.map(sendMail.bind(null, mailer)))
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendMail(mailer, messageToSend) {
|
|
|
|
var parts = messageToSend.split(':')
|
|
|
|
var messageType = parts[0]
|
|
|
|
var messageSubType = parts[1]
|
|
|
|
|
|
|
|
var message = {
|
2016-07-28 19:17:40 +03:00
|
|
|
acceptLanguage: 'en;q=0.8,en-US;q=0.5,en;q=0.3"',
|
2016-05-20 17:01:28 +03:00
|
|
|
code: 'ae35999f861ffc81d594034eb4560af8',
|
|
|
|
email: 'testuser@testuser.com',
|
2016-07-28 19:17:40 +03:00
|
|
|
ip: '10.246.67.38',
|
|
|
|
location: {
|
|
|
|
city: 'Madrid',
|
|
|
|
country: 'Spain'
|
|
|
|
},
|
2016-05-20 17:01:28 +03:00
|
|
|
locations: [],
|
|
|
|
redirectTo: 'https://redirect.com/',
|
|
|
|
resume: 'eyJjYW1wYWlnbiI6bnVsbCwiZW50cnlwb2ludCI6bnVsbCwiZmxvd0lkIjoiM2Q1ODZiNzY4Mzc2NGJhOWFiNzhkMzMxMTdjZDU4Y2RmYjk3Mzk5MWU5NTk0NjgxODBlMDUyMmY2MThhNmEyMSIsInJlc2V0UGFzc3dvcmRDb25maXJtIjp0cnVlLCJ1bmlxdWVVc2VySWQiOiI1ODNkOGFlYS00NzU3LTRiZTQtYWJlNC0wZWQ2NWZhY2Y2YWQiLCJ1dG1DYW1wYWlnbiI6bnVsbCwidXRtQ29udGVudCI6bnVsbCwidXRtTWVkaXVtIjpudWxsLCJ1dG1Tb3VyY2UiOm51bGwsInV0bVRlcm0iOm51bGx9',
|
|
|
|
service: 'sync',
|
|
|
|
token: '47b22cd271963448cf36da95cccfcfb342b5693d66f58aa635f9a95579431002',
|
2016-07-28 19:17:40 +03:00
|
|
|
timeZone: 'Europe/Madrid',
|
2016-05-20 17:01:28 +03:00
|
|
|
type: messageSubType,
|
|
|
|
uaBrowser: 'Firefox',
|
2016-08-08 17:49:43 +03:00
|
|
|
uaBrowserVersion: '51',
|
2016-05-20 17:01:28 +03:00
|
|
|
uaOS: 'Mac OSX',
|
|
|
|
uaOSVersion: '10.11',
|
2016-10-07 13:26:54 +03:00
|
|
|
unblockCode: '1ILO0Z5P',
|
2016-05-20 17:01:28 +03:00
|
|
|
uid: '6510cb04abd742c6b3e4abefc7e39c9f'
|
|
|
|
}
|
|
|
|
|
|
|
|
return mailer[messageType](message)
|
|
|
|
}
|
|
|
|
|
2016-05-25 12:10:27 +03:00
|
|
|
function checkMessageType(mailer, messageToSend) {
|
|
|
|
var messageTypes = getMailerMessageTypes(mailer)
|
|
|
|
messageTypes.push('all')
|
|
|
|
|
|
|
|
if (messageTypes.indexOf(messageToSend) === -1) {
|
2016-06-24 20:59:52 +03:00
|
|
|
console.error('invalid message name: `' + messageToSend + '`\n' +
|
2016-05-25 12:10:27 +03:00
|
|
|
'choose from: ' + messageTypes.join(', '))
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getMailerMessageTypes(mailer) {
|
|
|
|
var messageTypes = []
|
|
|
|
|
|
|
|
for (var key in mailer) {
|
2017-03-07 17:59:16 +03:00
|
|
|
if (
|
|
|
|
typeof mailer[key] === 'function' &&
|
|
|
|
! /^_/.test(key) && ! /^send/.test(key) && /Email$/.test(key)
|
|
|
|
) {
|
2016-05-25 12:10:27 +03:00
|
|
|
messageTypes.push(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageTypes.sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageTypesToWrite(mailer, messageToSend) {
|
|
|
|
if (messageToSend === 'all') {
|
|
|
|
return getMailerMessageTypes(mailer)
|
|
|
|
} else {
|
|
|
|
return [messageToSend]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureTargetDirectoryExists() {
|
|
|
|
mkdirp.sync(OUTPUT_DIRECTORY)
|
|
|
|
}
|