-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail.js
58 lines (50 loc) · 1.78 KB
/
gmail.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
//
// ********************** GMAIL SETUP **********************
//
// Call this from node with : (note the parentheses)
// const path = __dirname; // Current directory where main.js was launched
// const gmail = require(path + '/libraries/gmail.js')();
// Responsible for sending emails
module.exports = function (credentialsLocation) {
// Set up file access and read credentials
const fs = require('fs');
/*
The credentials for the email server should be in the file that is passed as "credentialLocation"
This should be a text file that is JSON formatted with username and password and a default email destination
For example :
{
"defaultRecipient" : "***[email protected] here***",
"username" : "***emailUserName here***",
"password" : "***emailPassword here ***"
}
*/
let credentials = JSON.parse(fs.readFileSync(credentialsLocation,'utf8'));
let nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service :'gmail',
auth: {
user: credentials.username,
pass: credentials.password
}
});
// "email" is an object with keys "recipient", "subject", "message" -- all optional
return function (email) {
// Defaults that will get overriden by "email" object
let mailOptions = {
"to" : credentials.defaultRecipient,
"sender" : credentials.username // This field has to be filled out for it to work with Public Mobile
}
for (let key in email) {
mailOptions[{ // This object is used to translate between "email" given parameters and what nodeMailer is expecting
"recipient" : "to",
"subject" : "subject",
"message" : "text",
}[key]] = email[key]
}
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error)
}
});
}
}