From 110e757933a8880710794e70eb5114708cd184f2 Mon Sep 17 00:00:00 2001 From: Aland Mulia Pratama <13522124@std.stei.itb.ac.id> Date: Thu, 28 Nov 2024 23:24:19 +0700 Subject: [PATCH 1/3] feat: add sendemail.ts --- .env.example | 5 ++++ package.json | 2 ++ pnpm-lock.yaml | 19 +++++++++++++++ src/configs/env.config.ts | 5 ++++ src/lib/sendemail.ts | 51 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 src/lib/sendemail.ts diff --git a/.env.example b/.env.example index 59c2ce4..a3ad7b1 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,11 @@ ALLOWED_ORIGINS='["http://localhost:5173"]' VAPID_PUBLIC_KEY=your_vapid_public_key VAPID_PRIVATE_KEY=your_vapid_private_key VAPID_MAILTO=example@gmail.com +SMTP_HOST=smtp.gmail.com +SMTP_USER=example@std.stei.itb.ac.id +SMTP_PORT=587 +SMTP_SECURE=true +SMTP_PASS=your_smtp_password GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secer GOOGLE_CALLBACK_URL=http://localhost:5000/api/auth/google/callback diff --git a/package.json b/package.json index bf1de44..ca1e9ab 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "googleapis": "^140.0.0", "hono": "^4.5.11", "install": "^0.13.0", + "nodemailer": "^6.9.16", "open-graph-scraper": "^6.5.1", "pg": "^8.11.3", "postgres": "^3.4.4", @@ -49,6 +50,7 @@ }, "devDependencies": { "@types/node": "^20.11.17", + "@types/nodemailer": "^6.4.17", "@types/web-push": "^3.6.3", "@typescript-eslint/eslint-plugin": "^6.4.0", "drizzle-kit": "^0.24.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d5500e2..76490e7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,6 +80,9 @@ importers: install: specifier: ^0.13.0 version: 0.13.0 + nodemailer: + specifier: ^6.9.16 + version: 6.9.16 open-graph-scraper: specifier: ^6.5.1 version: 6.5.1 @@ -108,6 +111,9 @@ importers: '@types/node': specifier: ^20.11.17 version: 20.11.30 + '@types/nodemailer': + specifier: ^6.4.17 + version: 6.4.17 '@types/web-push': specifier: ^3.6.3 version: 3.6.3 @@ -1141,6 +1147,9 @@ packages: '@types/node@20.14.15': resolution: {integrity: sha512-Fz1xDMCF/B00/tYSVMlmK7hVeLh7jE5f3B7X1/hmV0MJBwE27KlS7EvD/Yp+z1lm8mVhwV5w+n8jOZG8AfTlKw==} + '@types/nodemailer@6.4.17': + resolution: {integrity: sha512-I9CCaIp6DTldEg7vyUTZi8+9Vo0hi1/T8gv3C89yk1rSAAzoKQ8H8ki/jBYJSFoH/BisgLP8tkZMlQ91CIquww==} + '@types/pg-pool@2.0.4': resolution: {integrity: sha512-qZAvkv1K3QbmHHFYSNRYPkRjOWRLBYrL4B9c+wG0GSVGBw0NtJwPcgx/DSddeDJvRGMHCEQ4VMEVfuJ/0gZ3XQ==} @@ -2297,6 +2306,10 @@ packages: encoding: optional: true + nodemailer@6.9.16: + resolution: {integrity: sha512-psAuZdTIRN08HKVd/E8ObdV6NO7NTBY3KsC30F7M4H1OnmLCUNaS56FpYxyb26zWLSyYF9Ozch9KYHhHegsiOQ==} + engines: {node: '>=6.0.0'} + nth-check@2.1.1: resolution: {integrity: sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==} @@ -4292,6 +4305,10 @@ snapshots: dependencies: undici-types: 5.26.5 + '@types/nodemailer@6.4.17': + dependencies: + '@types/node': 20.14.15 + '@types/pg-pool@2.0.4': dependencies: '@types/pg': 8.11.6 @@ -5633,6 +5650,8 @@ snapshots: dependencies: whatwg-url: 5.0.0 + nodemailer@6.9.16: {} + nth-check@2.1.1: dependencies: boolbase: 1.0.0 diff --git a/src/configs/env.config.ts b/src/configs/env.config.ts index 57b0182..2f187ae 100644 --- a/src/configs/env.config.ts +++ b/src/configs/env.config.ts @@ -12,6 +12,11 @@ const EnvSchema = z.object({ VAPID_PUBLIC_KEY: z.string().default('not-specified'), VAPID_PRIVATE_KEY: z.string().default('not-specified'), VAPID_MAILTO: z.string().email().optional(), + SMTP_HOST: z.string(), + SMTP_USER: z.string().email().optional(), + SMTP_PORT: z.coerce.number(), + SMTP_SECURE: z.boolean().default(true), + SMTP_PASS: z.string(), GOOGLE_CLIENT_ID: z.string(), GOOGLE_CLIENT_SECRET: z.string(), GOOGLE_CALLBACK_URL: z.string().url(), diff --git a/src/lib/sendemail.ts b/src/lib/sendemail.ts new file mode 100644 index 0000000..28e47c6 --- /dev/null +++ b/src/lib/sendemail.ts @@ -0,0 +1,51 @@ +import nodemailer from 'nodemailer'; +import { env } from '~/configs/env.config'; + +// Konfigurasi transporter email menggunakan Nodemailer +const transporter = nodemailer.createTransport({ + host: env.SMTP_HOST, + port: env.SMTP_PORT, + secure: env.SMTP_SECURE, + auth: { + user: env.SMTP_USER, + pass: env.SMTP_PASS, + }, +}); + +// Interface untuk data email +export interface EmailOptions { + to: string | string[]; // Penerima, bisa satu atau beberapa + subject: string; // Subjek email + text?: string; // Pesan plain text + html?: string; // Pesan HTML + attachments?: Array<{ + filename: string; + path: string; // Path file atau URL + }>; // Daftar attachment +} + +/** + * Fungsi untuk mengirim email dengan attachment + * @param emailOptions - Opsi email seperti penerima, subjek, pesan, dan attachment + * @returns Promise hasil pengiriman email + */ +export async function sendEmail(emailOptions: EmailOptions): Promise { + try { + const mailOptions = { + from: env.SMTP_USER, // Alamat email pengirim + to: emailOptions.to, + subject: emailOptions.subject, + text: emailOptions.text, + html: emailOptions.html, + attachments: emailOptions.attachments, + }; + + // Kirim email menggunakan Nodemailer + const info = await transporter.sendMail(mailOptions); + + console.log(`Email berhasil dikirim: ${info.messageId}`); + } catch (error) { + console.error(`Gagal mengirim email: ${error}`); + throw error; + } +} From 9a371a3450f5070c5061eda74772addee0ec3738 Mon Sep 17 00:00:00 2001 From: Aland Mulia Pratama <13522124@std.stei.itb.ac.id> Date: Thu, 28 Nov 2024 23:52:12 +0700 Subject: [PATCH 2/3] feat: lint:fix --- src/lib/sendemail.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/sendemail.ts b/src/lib/sendemail.ts index 28e47c6..1a66b67 100644 --- a/src/lib/sendemail.ts +++ b/src/lib/sendemail.ts @@ -3,12 +3,12 @@ import { env } from '~/configs/env.config'; // Konfigurasi transporter email menggunakan Nodemailer const transporter = nodemailer.createTransport({ - host: env.SMTP_HOST, + host: env.SMTP_HOST, port: env.SMTP_PORT, - secure: env.SMTP_SECURE, + secure: env.SMTP_SECURE, auth: { - user: env.SMTP_USER, - pass: env.SMTP_PASS, + user: env.SMTP_USER, + pass: env.SMTP_PASS, }, }); From 4ea8a744184742a7ebc17621832a2f6e14eaada2 Mon Sep 17 00:00:00 2001 From: Aland Mulia Pratama <13522124@std.stei.itb.ac.id> Date: Thu, 28 Nov 2024 23:59:29 +0700 Subject: [PATCH 3/3] lint:fix --- src/lib/sendemail.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/sendemail.ts b/src/lib/sendemail.ts index 1a66b67..4f066bb 100644 --- a/src/lib/sendemail.ts +++ b/src/lib/sendemail.ts @@ -45,7 +45,7 @@ export async function sendEmail(emailOptions: EmailOptions): Promise { console.log(`Email berhasil dikirim: ${info.messageId}`); } catch (error) { - console.error(`Gagal mengirim email: ${error}`); + console.error(`Gagal mengirim email: ${String(error)}`); throw error; } }