Skip to content

Commit 41dc397

Browse files
committed
refactored email sending into a new util file
1 parent 9a19440 commit 41dc397

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

server/utils/auth.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { prisma } from './prisma'
44
import { emailOTP } from 'better-auth/plugins/email-otp'
55
import nodemailer from 'nodemailer'
66
import { createAuthMiddleware, APIError } from 'better-auth/api'
7+
import { sendEmail } from './email'
78

89
const transporter = nodemailer.createTransport({
910
service: 'gmail',
@@ -28,12 +29,7 @@ export const auth = betterAuth({
2829
plugins: [
2930
emailOTP({
3031
async sendVerificationOTP({ email, otp, type }) {
31-
await transporter.sendMail({
32-
from: process.env.EMAIL_USER,
33-
to: email,
34-
subject: 'OTP',
35-
html: `${otp}`,
36-
})
32+
sendEmail(email, 'OTP', `${otp}`)
3733
},
3834
}),
3935
],

server/utils/email.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import nodemailer from 'nodemailer'
2+
3+
const transporter = nodemailer.createTransport({
4+
service: 'gmail',
5+
auth: {
6+
user: process.env.EMAIL_USER,
7+
pass: process.env.EMAIL_PASS,
8+
},
9+
})
10+
11+
export const sendEmail = async (to: string, subject: string, html: string) => {
12+
try {
13+
await transporter.sendMail({
14+
from: process.env.EMAIL_USER,
15+
to,
16+
subject,
17+
html,
18+
})
19+
} catch (error) {
20+
console.error('Error sending email:', error)
21+
}
22+
}

0 commit comments

Comments
 (0)