-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (66 loc) · 2 KB
/
index.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
const AwsService = require('./service/awsService')
const Template = require('./template/template')
let template = new Template()
// service option can be aws or normal
async function mailService(serviceObj) {
let returnObj
let awsService = new AwsService(serviceObj.auth)
switch (serviceObj.emailInfo.service) {
case 'aws':
case 'normal':
returnObj = await sendEmail(serviceObj.emailInfo, serviceObj.template, awsService, serviceObj.attachment)
break;
default:
break;
}
return returnObj
}
async function sendEmail(emailInfo, templateOption, awsService, attachmentObj) {
let response
try {
let mailOptions
const obj = {
username: emailInfo.username,
string: generateRandomString(),
expirationTime: Date.now()
}
if (emailInfo.toEmail && templateOption && emailInfo.fromEmail) {
let userobj = {
username: emailInfo.username,
fromEmail: emailInfo.fromEmail,
toEmail: emailInfo.toEmail
}
if (templateOption === 'activateAccount') {
const link = await generateLink(obj, emailInfo.hostname)
mailOptions = await template.activateEmail(userobj, link)
} else if (templateOption === 'attachment') {
mailOptions = await template.attachmentEmail(userobj, attachmentObj)
}
response = await awsService.mailSend(mailOptions)
} else {
response = {
statusCode: 400,
message: 'Missing parameters'
}
}
return response
} catch (error) {
return {
statusCode: 400,
message: 'Server got error,Failed to send mail.'
}
}
}
async function generateLink(obj, hostname) {
const string = obj.username + '/' + obj.string + '/' + obj.expirationTime
const generateString = Buffer.from(string).toString('base64')
const link = hostname + generateString
return link
}
function generateRandomString(length) {
return Math.random()
.toString(36)
.replace(/[^a-zA-Z0-9]+/g, '')
.substr(0, length)
}
exports.EmailService = mailService