-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor email verification template and reset password template * update `sendMail` function for better reusability * send email to the receiver on production only
- Loading branch information
Showing
5 changed files
with
105 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import "server-only"; | ||
|
||
import { EmailVerificationTemplate } from "./templates/email-verification"; | ||
import { ResetPasswordTemplate } from "./templates/reset-password"; | ||
import { render } from "@react-email/render"; | ||
import { env } from "@/env"; | ||
import { EMAIL_SENDER } from "@/lib/constants"; | ||
import { createTransport, type TransportOptions } from "nodemailer"; | ||
import type { ComponentProps } from "react"; | ||
|
||
export enum EmailTemplate { | ||
EmailVerification = "EmailVerification", | ||
PasswordReset = "PasswordReset", | ||
} | ||
|
||
export type PropsMap = { | ||
[EmailTemplate.EmailVerification]: ComponentProps<typeof EmailVerificationTemplate>; | ||
[EmailTemplate.PasswordReset]: ComponentProps<typeof ResetPasswordTemplate>; | ||
}; | ||
|
||
const getEmailTemplate = <T extends EmailTemplate>(template: T, props: PropsMap[NoInfer<T>]) => { | ||
switch (template) { | ||
case EmailTemplate.EmailVerification: | ||
return { | ||
subject: "Verify your email address", | ||
body: render( | ||
<EmailVerificationTemplate {...(props as PropsMap[EmailTemplate.EmailVerification])} />, | ||
), | ||
}; | ||
case EmailTemplate.PasswordReset: | ||
return { | ||
subject: "Verify your email address", | ||
body: render( | ||
<ResetPasswordTemplate {...(props as PropsMap[EmailTemplate.PasswordReset])} />, | ||
), | ||
}; | ||
default: | ||
throw new Error("Invalid email template"); | ||
} | ||
}; | ||
|
||
const smtpConfig = { | ||
host: env.SMTP_HOST, | ||
port: env.SMTP_PORT, | ||
auth: { | ||
user: env.SMTP_USER, | ||
pass: env.SMTP_PASSWORD, | ||
}, | ||
}; | ||
|
||
const transporter = createTransport(smtpConfig as TransportOptions); | ||
|
||
export const sendMail = async <T extends EmailTemplate>( | ||
to: string, | ||
template: T, | ||
props: PropsMap[NoInfer<T>], | ||
) => { | ||
if (env.NODE_ENV !== "production") { | ||
console.log("📨 Email sent to:", to, "with template:", template, "and props:", props); | ||
return; | ||
} | ||
|
||
const { subject, body } = getEmailTemplate(template, props); | ||
|
||
return transporter.sendMail({ from: EMAIL_SENDER, to, subject, html: body }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.