-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.76 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
51
52
53
54
55
56
57
58
59
60
61
62
63
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const cors = require('cors');
const app = express();
app.use(cors());
// body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/send', async ({ body: { name, email, bodyText } }, res) => {
try {
const output = `
<p>Email from My Portfolio</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${name}</li>
<li>Email: ${email}</li>
</ul>
<h3>Message</h3>
<p>${bodyText}</p>
`;
const { EMAIL, PASSWORD } = process.env;
// create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: EMAIL,
pass: PASSWORD,
},
});
// send mail with defined transport object
const info = await transporter.sendMail({
from: `"Personal Portfolio Contact" <${EMAIL}>`, // sender address
to: EMAIL, // list of receivers
subject: 'Message from Portfolio', // Subject line
text: 'Hello world?', // plain text body
html: output, // html body
});
console.log('Message sent: %s', info.messageId);
// Message sent: <[email protected]>
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
res.send({ msg: 'Email has been sent' });
} catch (err) {
console.error(err);
}
});
app.listen(process.env.PORT || 5000, () => {
console.log('Running Server on port 5000');
});