-
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.
- Loading branch information
1 parent
2374bef
commit b687d9b
Showing
8 changed files
with
3,270 additions
and
263 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
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
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 |
---|---|---|
@@ -1,33 +1,52 @@ | ||
import express from 'express'; | ||
import { Resend } from 'resend'; | ||
import nodemailer from 'nodemailer'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const router = express.Router(); | ||
const resend = new Resend(process.env.RESEND_API_KEY); | ||
|
||
const transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
host: 'smtp.gmail.com', | ||
port: 465, | ||
secure: true, | ||
auth: { | ||
user: process.env.GMAIL_USER, | ||
pass: process.env.GMAIL_APP_PASSWORD, | ||
}, | ||
}); | ||
|
||
router.use(express.json()); // Middleware to parse JSON request bodies | ||
|
||
router.get('/', (req, res) => { | ||
res.send({ message: 'Welcome to email-service!' }); | ||
}); | ||
|
||
router.post('/send', async (req, res) => { | ||
console.log('Request body:', req.body); | ||
const { email, subject, content } = req.body; | ||
|
||
if (!email || !subject || !content) { | ||
return res.status(400).json({ error: 'Email, subject, and content are required' }); | ||
} | ||
console.log('Sending email to:', email); | ||
|
||
try { | ||
const response = await resend.emails.send({ | ||
from: "Acme <[email protected]>", | ||
// Send email using Nodemailer | ||
const info = await transporter.sendMail({ | ||
from: `Excited User <${process.env.GMAIL_USER}>`, // Sender email | ||
to: email, | ||
subject, | ||
html: content, | ||
}) | ||
subject: subject, | ||
html: content, // HTML content (or use 'text' property for plain text) | ||
}); | ||
|
||
return res.status(200).json({ message: 'Email sent successfully', data: response.data }); | ||
console.log('Email sent:', info); | ||
return res.status(200).json({ message: 'Email sent successfully', data: info }); | ||
} catch (error) { | ||
console.error('Error sending email:', error); | ||
res.status(500).json({ error: 'Failed to send email', details: error.message }); | ||
} | ||
}); | ||
|
||
export default router; | ||
export default router; |
Oops, something went wrong.