-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (149 loc) · 4.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const nodemailer = require('nodemailer')
const express = require('express')
const cors = require('cors')
const app = express()
const user = '[email protected]'
const pass = 'qfdncjopudabuqjz'
app.use(cors({ origin: '*' }))
app.use(express.json())
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: user,
pass: pass
},
port: 465
})
// Function to send email
const sendEmail = async mailOptions => {
try {
console.log('Sending email to:', mailOptions.to)
const info = await transporter.sendMail(mailOptions)
console.log('Email sent successfully:', info.response)
return info
} catch (error) {
console.error('Error sending email:', error)
throw error
}
}
// HTML Template with Bootstrap styling in both English and Portuguese
const emailTemplate = (DriverName, DriverEmail, DriverPassword) => {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Onboarding Information</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background-color: #f8f9fa;">
<div class="container mt-5">
<div class="card">
<div class="card-header bg-primary text-white text-center">
<h2>Welcome to Driver-Mode App</h2>
</div>
<div class="card-body">
<h4>Hi ${DriverName},</h4>
<p>Thank you for registering with us at Driver-Mode App.</p>
<p>Here are your login details:</p>
<p><strong>Email:</strong> ${DriverEmail}</p>
<p><strong>Password:</strong> ${DriverPassword}</p>
<p>Thank you for joining!</p>
</div>
<div class="card-footer text-muted text-center">
<small>Driver-Mode App © 2024</small>
</div>
</div>
</div>
<!-- Portuguese Version -->
<div class="container mt-5">
<div class="card">
<div class="card-header bg-primary text-white text-center">
<h2>Bem-vindo ao Aplicativo Driver-Mode</h2>
</div>
<div class="card-body">
<h4>Olá ${DriverName},</h4>
<p>Obrigado por se registrar conosco no aplicativo Driver-Mode.</p>
<p>Aqui estão seus detalhes de login:</p>
<p><strong>Email:</strong> ${DriverEmail}</p>
<p><strong>Senha:</strong> ${DriverPassword}</p>
<p>Obrigado por se juntar a nós!</p>
</div>
<div class="card-footer text-muted text-center">
<small>Driver-Mode App © 2024</small>
</div>
</div>
</div>
</body>
</html>
`
}
// Route to send mail to the driver
app.post('/mailToDriver', async (req, res) => {
const { DriverName, DriverEmail, DriverPassword } = req.body
console.log('Received request to /mailToDriver with body:', req.body)
const invalidFields = []
if (!DriverName) invalidFields.push('DriverName')
if (!DriverEmail) invalidFields.push('DriverEmail')
if (!DriverPassword) invalidFields.push('DriverPassword')
if (invalidFields.length) {
console.error('Invalid fields:', invalidFields)
return res
.status(400)
.send({ message: 'Invalid fields: ' + invalidFields.join(', ') })
}
if (
DriverEmail.length < 6 ||
DriverEmail.indexOf('@') === -1 ||
DriverEmail.indexOf('.') === -1
) {
console.error('Invalid Email:', DriverEmail)
return res.status(400).send({ message: 'Invalid Email' })
}
const mailOptions = {
from: '[email protected]',
to: DriverEmail,
subject: 'Onboarding Information for Driver-Mode App',
html: emailTemplate(DriverName, DriverEmail, DriverPassword)
}
try {
const info = await sendEmail(mailOptions)
return res.status(200).send({ message: 'Mail sent successfully', info })
} catch (err) {
console.error('Failed to send mail to driver:', err)
return res.status(500).send({ message: 'Internal server error' })
}
})
app.get('/', (req, res) => {
res.send('Driver Mode SMTP API')
})
app.post('/sendEmail', async (req, res) => {
const { to, subject, text } = req.body
console.log('Received request to /sendEmail with body:', req.body)
try {
const info = await sendEmail({
from: user,
to: to,
subject: subject,
text: text
})
await transporter.sendMail({
to: to,
subject: subject,
text: text,
info
})
res.status(200).send({
message: 'Email sent successfully'
})
} catch (error) {
console.error('Failed to send email:', error)
return res.status(500).send({ message: 'Internal server error' })
}
})
const port = process.env.PORT || 6000
app.listen(port, () => {
console.log('Server started on port ' + port)
})