Skip to content

Commit

Permalink
feat: emails using resend
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek kushwaha <abhishekkushwaha1479@gmail.com>
  • Loading branch information
Abbhiishek committed May 9, 2024
1 parent bbd5369 commit d0cdf34
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/emails/emails/mail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react'
import { Html } from '@react-email/html'
import { Button } from '@react-email/button'

export function Email(props: { url: string }) {
const { url } = props

return (
<Html lang="en">
<Button href={url}>Click me</Button>
</Html>
)
}

export default Email
38 changes: 38 additions & 0 deletions packages/emails/emails/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Resend } from 'resend'

import { env } from '../env'

export const resend = new Resend(env.RESEND_API_KEY)

export interface Emails {
react: JSX.Element
subject: string
to: string[]
from: string
}

export type EmailHtml = {
html: string
subject: string
to: string[]
from: string
}
export const sendEmail = async (email: Emails) => {
await resend.emails.send(email)
}

export const sendEmailHtml = async (email: EmailHtml) => {
await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.RESEND_API_KEY}`
},
body: JSON.stringify({
to: email.to,
from: email.from,
subject: email.subject,
html: email.html
})
})
}
8 changes: 8 additions & 0 deletions packages/emails/emails/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const validateEmailNotDisposable = async (mailHost: string) => {
const response = await fetch(
`https://open.kickbox.com/v1/disposable/${mailHost}`
)
const status = await response.json()

return status.disposable
}
11 changes: 11 additions & 0 deletions packages/emails/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createEnv } from '@t3-oss/env-core'
import { z } from 'zod'

export const env = createEnv({
server: {
RESEND_API_KEY: z.string().min(1)
},
runtimeEnv: {
RESEND_API_KEY: process.env.RESEND_API_KEY
}
})
5 changes: 5 additions & 0 deletions packages/emails/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { validateEmailNotDisposable } from './emails/utils/utils'

export { validateEmailNotDisposable }

export { sendEmail, sendEmailHtml } from './emails/send'
30 changes: 30 additions & 0 deletions packages/emails/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@repo/emails",
"version": "1.0.0",
"description": "",
"main": "./index.ts",
"scripts": {
"dev:email": "email dev"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@react-email/button": "^0.0.15",
"@react-email/components": "^0.0.17",
"@react-email/head": "^0.0.8",
"@react-email/html": "^0.0.8",
"@react-email/tailwind": "^0.0.16",
"@t3-oss/env-core": "^0.10.1",
"react-email": "^2.1.2",
"resend": "^3.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@repo/typescript-config": "workspace:*",
"@types/node": "20.8.0",
"@types/react": "18.2.64",
"react": "18.2.0",
"typescript": "5.4.5"
}
}
6 changes: 6 additions & 0 deletions packages/emails/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@repo/typescript-config/react-library.json",
"include": [".", "env.ts"],
"exclude": ["dist", "build", "node_modules"]
}

0 comments on commit d0cdf34

Please sign in to comment.