Added shared mailer package to remove circular dependencies between packages. (#63)
This commit is contained in:
committed by
GitHub
parent
d18f810c6e
commit
4c7a3354b9
51
packages/mailers/nodemailer/README.md
Normal file
51
packages/mailers/nodemailer/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Mailers - @kit/mailers
|
||||
|
||||
This package is responsible for sending emails using a unified interface across the app.
|
||||
|
||||
The default mailer uses the `nodemailer` package to send emails. You can create custom mailers by extending the `Mailer` class.
|
||||
|
||||
Make sure the app installs the `@kit/mailers` package before using it.
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "my-app",
|
||||
"dependencies": {
|
||||
"@kit/mailers": "*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
By default, the package uses `nodemailer`.
|
||||
|
||||
To use [Resend](https:///resend.com)'s HTTP API, please set the environment variable `MAILER_PROVIDER` to `resend`.
|
||||
|
||||
```
|
||||
MAILER_PROVIDER=resend
|
||||
```
|
||||
|
||||
### Send an email
|
||||
|
||||
```tsx
|
||||
import { getMailer } from '@kit/mailers';
|
||||
|
||||
async function sendEmail() {
|
||||
const mailer = await getMailer();
|
||||
|
||||
return mailer.sendEmail({
|
||||
to: '',
|
||||
from: '',
|
||||
subject: 'Hello',
|
||||
text: 'Hello, World!'
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
## Resend
|
||||
|
||||
If you're using the `resend` provider, please add the following environment variables:
|
||||
|
||||
```
|
||||
RESEND_API_KEY=your-api-key
|
||||
```
|
||||
41
packages/mailers/nodemailer/package.json
Normal file
41
packages/mailers/nodemailer/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@kit/nodemailer",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"scripts": {
|
||||
"clean": "git clean -xdf .turbo node_modules",
|
||||
"format": "prettier --check \"**/*.{ts,tsx}\"",
|
||||
"lint": "eslint .",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"prettier": "@kit/prettier-config",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"nodemailer": "^6.9.15"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/mailers-shared": "workspace:^",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@types/nodemailer": "6.4.15",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
"extends": [
|
||||
"@kit/eslint-config/base",
|
||||
"@kit/eslint-config/react"
|
||||
]
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"src/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
26
packages/mailers/nodemailer/src/index.ts
Normal file
26
packages/mailers/nodemailer/src/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'server-only';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Mailer, MailerSchema } from '@kit/mailers-shared';
|
||||
|
||||
import { getSMTPConfiguration } from './smtp-configuration';
|
||||
|
||||
type Config = z.infer<typeof MailerSchema>;
|
||||
|
||||
export function createNodemailerService() {
|
||||
return new Nodemailer();
|
||||
}
|
||||
|
||||
/**
|
||||
* A class representing a mailer using Nodemailer library.
|
||||
* @implements {Mailer}
|
||||
*/
|
||||
class Nodemailer implements Mailer {
|
||||
async sendEmail(config: Config) {
|
||||
const { createTransport } = await import('nodemailer');
|
||||
const transporter = createTransport(getSMTPConfiguration());
|
||||
|
||||
return transporter.sendMail(config);
|
||||
}
|
||||
}
|
||||
21
packages/mailers/nodemailer/src/smtp-configuration.ts
Normal file
21
packages/mailers/nodemailer/src/smtp-configuration.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { SmtpConfigSchema } from '@kit/mailers-shared';
|
||||
|
||||
export function getSMTPConfiguration() {
|
||||
const data = SmtpConfigSchema.parse({
|
||||
user: process.env.EMAIL_USER,
|
||||
pass: process.env.EMAIL_PASSWORD,
|
||||
host: process.env.EMAIL_HOST,
|
||||
port: Number(process.env.EMAIL_PORT),
|
||||
secure: process.env.EMAIL_TLS !== 'false',
|
||||
});
|
||||
|
||||
return {
|
||||
host: data.host,
|
||||
port: data.port,
|
||||
secure: data.secure,
|
||||
auth: {
|
||||
user: data.user,
|
||||
pass: data.pass,
|
||||
},
|
||||
};
|
||||
}
|
||||
8
packages/mailers/nodemailer/tsconfig.json
Normal file
8
packages/mailers/nodemailer/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@kit/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user