forked from mozilla/fxa-auth-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-emails-to-disk.js
159 lines (133 loc) · 4.58 KB
/
write-emails-to-disk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* 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
* postVerifySecondaryEmail
* recoveryEmail
* unblockCodeEmail
* verificationReminderEmail:first
* verificationReminderEmail:second
* verifyEmail
* verifySecondaryEmail
* 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')
const config = require('../config').getProperties()
const error = require('../lib/error')
const createSenders = require('../lib/senders')
var fs = require('fs')
const log = require('../lib/senders/legacy_log')(require('../lib/senders/log')('server'))
var mkdirp = require('mkdirp')
var path = require('path')
var OUTPUT_DIRECTORY = path.join(__dirname, '..', '.mail_output')
var messageToSend = process.argv[2] || ''
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)
},
close: function () {}
}
const bounces = {
// this is for dev purposes, no need to check db
check: () => P.resolve()
}
require('../lib/senders/translator')(config.i18n.supportedLanguages, config.i18n.defaultLanguage)
.then(translator => {
return createSenders(log, config, error, bounces, translator, mailSender)
})
.then((senders) => {
const mailer = senders.email._ungatedMailer
checkMessageType(mailer, messageToSend)
ensureTargetDirectoryExists()
return sendMails(mailer, getMessageTypesToWrite(mailer, messageToSend))
})
.then(() => {
console.info('done')
})
function getEmailOutputPath(subject, extension) {
var outputFilename = subject.replace(/\s+/g, '_') + '.' + extension
return path.join(OUTPUT_DIRECTORY, outputFilename)
}
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 = {
acceptLanguage: 'en;q=0.8,en-US;q=0.5,en;q=0.3"',
code: 'ae35999f861ffc81d594034eb4560af8',
email: 'testuser@testuser.com',
ip: '10.246.67.38',
location: {
city: 'Madrid',
country: 'Spain'
},
locations: [],
redirectTo: 'https://redirect.com/',
resume: 'eyJjYW1wYWlnbiI6bnVsbCwiZW50cnlwb2ludCI6bnVsbCwiZmxvd0lkIjoiM2Q1ODZiNzY4Mzc2NGJhOWFiNzhkMzMxMTdjZDU4Y2RmYjk3Mzk5MWU5NTk0NjgxODBlMDUyMmY2MThhNmEyMSIsInJlc2V0UGFzc3dvcmRDb25maXJtIjp0cnVlLCJ1bmlxdWVVc2VySWQiOiI1ODNkOGFlYS00NzU3LTRiZTQtYWJlNC0wZWQ2NWZhY2Y2YWQiLCJ1dG1DYW1wYWlnbiI6bnVsbCwidXRtQ29udGVudCI6bnVsbCwidXRtTWVkaXVtIjpudWxsLCJ1dG1Tb3VyY2UiOm51bGwsInV0bVRlcm0iOm51bGx9',
secondaryEmail: 'secondary@email',
service: 'sync',
token: '47b22cd271963448cf36da95cccfcfb342b5693d66f58aa635f9a95579431002',
timeZone: 'Europe/Madrid',
type: messageSubType,
uaBrowser: 'Firefox',
uaBrowserVersion: '51',
uaOS: 'Mac OSX',
uaOSVersion: '10.11',
unblockCode: '1ILO0Z5P',
uid: '6510cb04abd742c6b3e4abefc7e39c9f'
}
return mailer[messageType](message)
}
function checkMessageType(mailer, messageToSend) {
var messageTypes = getMailerMessageTypes(mailer)
messageTypes.push('all')
if (messageTypes.indexOf(messageToSend) === -1) {
console.error('invalid message name: `' + messageToSend + '`\n' +
'choose from: ' + messageTypes.join(', '))
process.exit(1)
}
}
function getMailerMessageTypes(mailer) {
var messageTypes = []
for (var key in mailer) {
if (
typeof mailer[key] === 'function' &&
! /^_/.test(key) && ! /^send/.test(key) && /Email$/.test(key)
) {
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)
}