-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (39 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require('express');
require('dotenv').config();
const app = express();
const path = require('path');
const port = 3000;
const nodemailer = require('nodemailer');
app.use(express.json()); // Middleware to parse JSON
app.use(express.static('public'));
app.get('/', (req, res) => {
// hada html this is html
res.sendFile(path.join(__dirname, 'public', 'send.html'));
});
// Nodemailer
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS // It's recommended to use application-specific passwords for Gmail but u need to enable the 2-enable configuration
}
});
app.post('/send', async (req, res) => {
const { to, subject, text } = req.body;
try {
let info = await transporter.sendMail({
from: '"siham" <${process.env.GMAIL_USER}>', // sender address
to: to, // The recipient's address
subject: subject, // The subject
text: text, // The email body
});
console.log('Message sent: %s', info.messageId);
res.send('Email sent successfully');
} catch (error) {
console.error('Error sending email: ', error);
res.status(500).send('Error sending email');
}
});
app.listen(port, () => {
console.log(`Email sender app listening at http://localhost:${port}`);
});