MCPcopy
hub / github.com/typpo/textbelt / sendText

Function sendText

lib/text.js:33–75  ·  view source on GitHub ↗
(phone, message, carrier, region, cb)

Source from the content-addressed store, hash-verified

31 cb - function(err, info), NodeMailer callback
32*/
33function sendText(phone, message, carrier, region, cb) {
34 output('txting phone', phone, ':', message);
35
36 let providersList;
37 if (carrier) {
38 providersList = carriers[carrier];
39 } else {
40 providersList = providers[region || 'us'];
41 }
42
43 const transporter = nodemailer.createTransport(config.transport);
44
45 const p = Promise.all(providersList.map((provider) => {
46 const to = provider.replace('%s', phone);
47
48 const mailOptions = {
49 to,
50 subject: null,
51 text: message,
52 html: message,
53 ...config.mailOptions,
54 };
55
56 return new Promise((resolve, reject) => transporter.sendMail(mailOptions, (err, info) => {
57 if (err) return reject(err);
58 return resolve(info);
59 }));
60 }));
61
62 // If the callback is provided, simulate the first message as the old-style
63 // callback format, then return the full promise.
64 if (cb) {
65 return p.then((info) => {
66 cb(null, info[0]);
67 return info;
68 }, (err) => {
69 cb(err);
70 return err;
71 });
72 }
73
74 return p;
75}
76
77//----------------------------------------------------------------
78/* Overrides default config

Callers

nothing calls this directly

Calls 1

outputFunction · 0.85

Tested by

no test coverage detected