-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from hmif-itb/feat/send-email
[BE] Send Email 86eqrn7ff
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
} | ||
} |