-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailer.js
More file actions
61 lines (53 loc) · 1.59 KB
/
emailer.js
File metadata and controls
61 lines (53 loc) · 1.59 KB
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
var nodemailer = require('nodemailer');
const { google } = require("googleapis");
const oauth2Client = new google.auth.OAuth2(
process.env.CLIENT_ID, // ClientID
process.env.CLIENT_SECRET, // Client Secret
process.env.REDIRECT_URI // Redirect URL
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN
});
module.exports.sender = async function (Email, text, subject){
try {
const accessToken = await oauth2Client.getAccessToken()
const smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: `${process.env.EMAIL_EMAIL}@gmail.com`,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: accessToken,
}});
var mailOptions = {
from: `${process.env.EMAIL_EMAIL}@gmail.com`,
to: Email,
subject: subject,
text: text
};
smtpTransport.sendMail(mailOptions, (error, response) => {
error ? console.log(error) : console.log('Email Sent Succesfully!');
smtpTransport.close();
});
} catch (error) {
console.log(error)
}
};
// var transporter = nodemailer.createTransport({
// service: 'gmail',
// auth: {
// user: process.env.EMAIL_EMAIL,
// pass: process.env.EMAIL_PASS
// }
// });
// module.exports.sender = function (Email, text, subject){
// var mailOptions = {
// from: `${process.env.EMAIL_EMAIL}@gmail.com`,
// to: Email,
// subject: subject,
// text: text
// };
// transporter.sendMail(mailOptions);
// };