Skip to content

Commit

Permalink
feat: mailer | setup nodemailer
Browse files Browse the repository at this point in the history
  • Loading branch information
MRoyhanF committed Nov 14, 2024
1 parent 3241839 commit 31a0031
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.16",
"pg": "^8.13.0",
"swagger-ui-express": "^5.0.1"
},
Expand Down
33 changes: 33 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import express from "express";
import morgan from "morgan";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "./docs/swagger2.json" with { type: "json" };
import nodemailer from 'nodemailer';

import authRoutes from "./routes/authRoutes.js";
import userRoutes from "./routes/userRoutes.js";
Expand Down Expand Up @@ -38,6 +39,38 @@ app.get("/api/v1/error", () => {
throw new Error("This is an error route");
});

// Konfigurasi Nodemailer
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
service: 'gmail',
port: 587,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD
}
});

// Endpoint untuk mengirim email
app.post('/send-email', async (req, res) => {
const { to, subject, text } = req.body;

const mailOptions = {
from: process.env.EMAIL_USER,
to,
subject,
text
};

try {
const info = await transporter.sendMail(mailOptions);
console.log('Email terkirim: ', info.response);
res.status(200).json({ message: 'Email berhasil dikirim', info: info.response });
} catch (error) {
console.error('Error saat mengirim email:', error);
res.status(500).json({ message: 'Gagal mengirim email', error });
}
});

Sentry.setupExpressErrorHandler(app);

// app.use(handleError);
Expand Down

0 comments on commit 31a0031

Please sign in to comment.