forked from keystonejs/keystone-email
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-send.js
122 lines (110 loc) · 2.93 KB
/
test-send.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
/*
This file sends an email with a test template to an email address.
Usage:
[email protected] MAILGUN_API_KEY=xyz MAILGUN_DOMAIN=abc TEMPLATE=simple node test-send
For usage with nodemailer specify your transport config and auth in test.config.js, and then run:
[email protected] node test-send
*/
var Email = require('./index');
var template = process.env.TEMPLATE;
var to = process.env.TO;
var mailgunApiKey = process.env.MAILGUN_API_KEY;
var mailgunDomain = process.env.MAILGUN_DOMAIN;
var mandrillApiKey = process.env.MANDRILL_API_KEY;
var nodemailerConfig = require('./test.config.js');
if (!mandrillApiKey && (!mailgunApiKey || !mailgunDomain) && !nodemailerConfig) {
throw Error('You must provide at least one auth config');
}
// default to simple template
if (!template) {
template = 'simple';
}
var templateOptions = require('./tests/emails/' + template + '/options');
var templatePath = './tests/emails/' + template + '/template.pug';
var toArray = [
to,
{ name: 'Test Recipient', email: to.split('@').join('+1@'), vars: { testVar: 'Our test variable' } },
{ name: { first: 'Test First', last: 'Test Last' }, email: to.split('@').join('+2@') },
];
if (mailgunApiKey) {
Email.send(
// template path
templatePath,
// Email options
{
transport: 'mailgun',
},
// Template locals
templateOptions,
// Send options
{
to: toArray,
subject: 'Why hello there! ... from keystone-email ' + Date.now(),
from: { name: 'Test', email: '[email protected]' },
apiKey: mailgunApiKey,
domain: mailgunDomain,
},
// callback
function (err, result) {
if (err) {
console.error('🤕 Mailgun test failed with error:\n', err);
} else {
console.log('📬 Successfully sent Mailgun test with result:\n', result);
}
}
);
}
if (mandrillApiKey) {
Email.send(
// template path
templatePath,
// Email options
{
transport: 'mandrill',
},
// Template locals
templateOptions,
// Send options
{
to: toArray,
subject: 'Why hello there! ... from keystone-email ' + Date.now(),
from: { name: 'Test', email: '[email protected]' },
apiKey: mandrillApiKey,
},
// callback
function (err, result) {
if (err) {
console.error('🤕 Mandrill test failed with error:\n', err);
} else {
console.log('📬 Successfully sent Mandrill test with result:\n', result);
}
}
);
}
if (nodemailerConfig) {
Email.send(
// template path
templatePath,
// Email options
{
transport: 'nodemailer',
},
// Template locals
templateOptions,
// Send options
{
to: toArray,
subject: 'Why hello there! ... from keystone-email ' + Date.now(),
from: { name: 'Test', email: '[email protected]' },
nodemailerConfig: nodemailerConfig,
},
// callback
function (err, result) {
if (err) {
console.error('🤕 Nodemailer test failed with error:\n', err);
} else {
console.log('📬 Successfully sent Nodemailer test with result:\n', result);
}
}
);
}