Skip to content

Commit

Permalink
Merge pull request #62 from hmif-itb/feat/send-email
Browse files Browse the repository at this point in the history
[BE] Send Email 86eqrn7ff
  • Loading branch information
RMA1403 authored Dec 1, 2024
2 parents e6130ed + 4ea8a74 commit a284f40
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=[email protected]
SMTP_HOST=smtp.gmail.com
SMTP_USER=[email protected]
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
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/configs/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
51 changes: 51 additions & 0 deletions src/lib/sendemail.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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: ${String(error)}`);
throw error;
}
}

0 comments on commit a284f40

Please sign in to comment.