-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.js
66 lines (65 loc) · 1.92 KB
/
mailer.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
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
const bcrypt = require('bcrypt');
const OAuth2 = google.auth.OAuth2;
const secret = require('dotenv').config();
class Mailer {
/*
generic handler I wrote so that i don't have to configure all this boilerplate everytime.
here is the tutorial about the configuration in the google cloud system.
https://medium.com/@nickroach_50526/sending-emails-with-node-js-using-smtp-gmail-and-oauth2-316fe9c790a1
(currently only provide emailing thourgh gmail
*/
constructor(appAddress) {
this.appAddress = appAddress;
this.transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
user: appAddress,
clientId: process.env.GMAIL_CLIENT_ID,
clientSecret: process.env.GMAIL_CLIENT_SECRET,
refreshToken: process.env.GMAIL_REFRESH_TOKEN,
accessToken: this.SetGoogleAuth()
}
});
}
async SetGoogleAuth() {
const oauth2Client = new OAuth2(
process.env.GMAIL_CLIENT_ID, // ClientID
process.env.GMAIL_CLIENT_SECRET, // Client Secret
'https://developers.google.com/oauthplayground' // Redirect URL
);
oauth2Client.setCredentials({
refresh_token: process.env.GMAIL_REFRESH_TOKEN
});
return await oauth2Client.getAccessToken();
}
SendMail({ to, subject, isHTML, content, params }) {
try {
if (params) {
content = this.EmbedMailParams(params, content);
}
this.transporter.sendMail({
from: this.appAddress,
to,
subject,
generateTextFromHTML: isHTML,
html: content
});
} catch (ex) {
throw ex;
}
}
EmbedMailParams(params, content) {
//enumrating the html, if catches {~someproperty~} then replaces with value!
for (let prop in params) {
if (params.hasOwnProperty(prop)) {
//TODO: find out why string literal fucks it up..
content = content.replace('{' + prop + '}', params[prop]);
}
}
return content;
}
}
module.exports = Mailer;