Skip to content

Commit 914d2aa

Browse files
fixed mailing system
1 parent 51aea7f commit 914d2aa

File tree

4 files changed

+137
-129
lines changed

4 files changed

+137
-129
lines changed

backend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"module": "index.ts",
44
"type": "module",
55
"scripts": {
6-
"start": "bun run index.ts",
6+
"start": "bun run index.ts --env-file=.env",
77
"compile": "bun build --compile index.ts",
8-
"dev": "bun run --hot index.ts"
8+
"dev": "bun run --hot index.ts --env-file=.env"
99
},
1010
"dependencies": {
1111
"cors": "^2.8.5",

backend/src/routes/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ app.post('/login', LoginValidate, async (req, res) => {
2828
}
2929
const token = generateAccessToken(email, user.username)
3030
const rt = await generateRefreshToken(email, user.username)
31-
await sendLoginMail(email, user.username)
31+
await sendLoginMail(user.email, user.username)
3232
res.status(200).send({status: 200, message: "User logged in", access_token: token, refresh_token: rt.token})
3333
} catch(_) {
3434
res.status(500).send({status: 500, message: "Unknown Error"})

backend/src/routes/register.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ app.post('/register', RegisterValidate, async (req, res) => {
3131
await sendRegMail(email, username)
3232
res.status(201).send({status:201, message: "User Created", access_token: token, refresh_token: rt.token})
3333
} catch(_) {
34+
console.error(_);
35+
3436
res.status(500).send({status:500, message: "Unknown Error"})
3537
}
3638
})

backend/src/services/mail.ts

Lines changed: 132 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,145 @@
11
import { createTransport } from "nodemailer";
22

3-
const {sendMail} = createTransport({
3+
const transport = createTransport({
44
host: process.env.SMTP_HOST,
55
port: parseInt(process.env.SMTP_PORT!),
66
secure: parseInt(process.env.SMTP_PORT!) == 465,
77
auth: {
88
type: "oauth2",
99
user: process.env.SMTP_USER,
10-
refreshToken: process.env.SMTP_REFRESH_TOKEN
11-
}
10+
refreshToken: process.env.SMTP_REFRESH_TOKEN,
11+
clientId: process.env.SMTP_CLI_ID,
12+
clientSecret: process.env.SMTP_CLI_SEC
13+
},
14+
requireTLS: true
1215
})
1316

14-
const registrationEmailHtml = (username: string) => `
15-
<!DOCTYPE html>
16-
<html lang="en">
17-
<head>
18-
<meta charset="UTF-8">
19-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
20-
<style>
21-
body {
22-
font-family: Arial, sans-serif;
23-
line-height: 1.6;
24-
color: #333;
25-
}
26-
.container {
27-
max-width: 600px;
28-
margin: 0 auto;
29-
padding: 20px;
30-
background-color: #f7f7f7;
31-
border-radius: 8px;
32-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
33-
}
34-
.header {
35-
text-align: center;
36-
padding-bottom: 20px;
37-
border-bottom: 1px solid #eaeaea;
38-
}
39-
.content {
40-
margin-top: 20px;
41-
}
42-
.footer {
43-
margin-top: 30px;
44-
text-align: center;
45-
font-size: 12px;
46-
color: #999;
47-
}
48-
</style>
49-
</head>
50-
<body>
51-
<div class="container">
52-
<div class="header">
53-
<h1>Thank You for Registering, ${username}!</h1>
54-
</div>
55-
<div class="content">
56-
<p>Dear ${username},</p>
57-
<p>We're thrilled to have you on board. Thank you for registering with us!</p>
58-
<p>We hope you enjoy your experience. If you have any questions, feel free to reach out.</p>
59-
</div>
60-
<div class="footer">
61-
<p>&copy; ${new Date().getFullYear()} Dev.Calender. All rights reserved.</p>
62-
</div>
63-
</div>
64-
</body>
65-
</html>
66-
`;
67-
68-
export const sendRegMail = async (email: string, username: string) => {
69-
await sendMail({
70-
from: process.env.SMTP_USER,
71-
to: email,
72-
subject: "Registration Confirmation",
73-
html: registrationEmailHtml(username)
74-
})
17+
export const sendRegMail = async (email, username) => transport.sendMail({
18+
from: process.env.SMTP_USER,
19+
to: email,
20+
subject: "Registration Confirmation",
21+
html: `
22+
<!DOCTYPE html>
23+
<html lang="en">
24+
<head>
25+
<meta charset="UTF-8">
26+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
27+
<style>
28+
body {
29+
font-family: Arial, sans-serif;
30+
line-height: 1.6;
31+
color: #333;
7532
}
33+
.container {
34+
max-width: 600px;
35+
margin: 0 auto;
36+
padding: 20px;
37+
background-color: #f7f7f7;
38+
border-radius: 8px;
39+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
40+
}
41+
.header {
42+
text-align: center;
43+
padding-bottom: 20px;
44+
border-bottom: 1px solid #eaeaea;
45+
}
46+
.content {
47+
margin-top: 20px;
48+
}
49+
.footer {
50+
margin-top: 30px;
51+
text-align: center;
52+
font-size: 12px;
53+
color: #999;
54+
}
55+
</style>
56+
</head>
57+
<body>
58+
<div class="container">
59+
<div class="header">
60+
<h1>Thank You for Registering, ${username}!</h1>
61+
</div>
62+
<div class="content">
63+
<p>Dear ${username},</p>
64+
<p>We're thrilled to have you on board. Thank you for registering with us!</p>
65+
<p>We hope you enjoy your experience. If you have any questions, feel free to reach out.</p>
66+
</div>
67+
<div class="footer">
68+
<p>&copy; ${new Date().getFullYear()} Dev.Calender. All rights reserved.</p>
69+
</div>
70+
</div>
71+
</body>
72+
</html>
73+
`
74+
}, (err, info) => {
75+
if (err) {
76+
console.error(err);
77+
}
78+
console.log(info.response);
79+
})
7680

77-
const loginEmailHtml = (username: string) => `
78-
<!DOCTYPE html>
79-
<html lang="en">
80-
<head>
81-
<meta charset="UTF-8">
82-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
83-
<style>
84-
body {
85-
font-family: Arial, sans-serif;
86-
line-height: 1.6;
87-
color: #333;
88-
}
89-
.container {
90-
max-width: 600px;
91-
margin: 0 auto;
92-
padding: 20px;
93-
background-color: #f7f7f7;
94-
border-radius: 8px;
95-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
96-
}
97-
.header {
98-
text-align: center;
99-
padding-bottom: 20px;
100-
border-bottom: 1px solid #eaeaea;
101-
}
102-
.content {
103-
margin-top: 20px;
104-
}
105-
.footer {
106-
margin-top: 30px;
107-
text-align: center;
108-
font-size: 12px;
109-
color: #999;
110-
}
111-
</style>
112-
</head>
113-
<body>
114-
<div class="container">
115-
<div class="header">
116-
<h1>New login from ${username}!</h1>
117-
</div>
118-
<div class="content">
119-
<p>Dear ${username},</p>
120-
<p>This is an automated email to let you know that we have a new login from your account.</p>
121-
<p>If you think someone else accessed your account, please contact support.</p>
122-
</div>
123-
<div class="footer">
124-
<p>&copy; ${new Date().getFullYear()} Dev.Calender. All rights reserved.</p>
125-
</div>
126-
</div>
127-
</body>
128-
</html>
129-
`;
130-
131-
export const sendLoginMail = async (email: string, username: string) => {
132-
await sendMail({
133-
from: process.env.SMTP_USER,
134-
to: email,
135-
subject: "New login from your account",
136-
html: loginEmailHtml(username)
137-
})
81+
export const sendLoginMail = async (email, username) => transport.sendMail({
82+
from: process.env.SMTP_USER,
83+
to: email,
84+
subject: "New login from your account",
85+
html: `
86+
<!DOCTYPE html>
87+
<html lang="en">
88+
<head>
89+
<meta charset="UTF-8">
90+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
91+
<style>
92+
body {
93+
font-family: Arial, sans-serif;
94+
line-height: 1.6;
95+
color: #333;
13896
}
139-
export default sendMail
97+
.container {
98+
max-width: 600px;
99+
margin: 0 auto;
100+
padding: 20px;
101+
background-color: #f7f7f7;
102+
border-radius: 8px;
103+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
104+
}
105+
.header {
106+
text-align: center;
107+
padding-bottom: 20px;
108+
border-bottom: 1px solid #eaeaea;
109+
}
110+
.content {
111+
margin-top: 20px;
112+
}
113+
.footer {
114+
margin-top: 30px;
115+
text-align: center;
116+
font-size: 12px;
117+
color: #999;
118+
}
119+
</style>
120+
</head>
121+
<body>
122+
<div class="container">
123+
<div class="header">
124+
<h1>New login from ${username}!</h1>
125+
</div>
126+
<div class="content">
127+
<p>Dear ${username},</p>
128+
<p>This is an automated email to let you know that we have a new login from your account.</p>
129+
<p>If you think someone else accessed your account, please contact support.</p>
130+
</div>
131+
<div class="footer">
132+
<p>&copy; ${new Date().getFullYear()} Dev.Calender. All rights reserved.</p>
133+
</div>
134+
</div>
135+
</body>
136+
</html>
137+
`
138+
}, (err, info) => {
139+
if (err) {
140+
console.error(err);
141+
}
142+
console.log(info.response);
143+
})
144+
145+
export default transport.sendMail

0 commit comments

Comments
 (0)