Skip to content

Commit 4107e5f

Browse files
author
Janaka-Steph
committed
Use Netlify function for email
1 parent 0ed7549 commit 4107e5f

8 files changed

Lines changed: 132 additions & 176 deletions

File tree

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PUBLIC_URL=/function/bitcoin-studio-client
2-
REACT_APP_SEND_EMAIL_URL=/api/send-email
2+
REACT_APP_SEND_EMAIL_URL=/.netlify/functions/send-email
33
SMTP_GMAIL_PASS=***
4+
SMTP_GMAIL_USER=***@gmail.com
45
PORT=8081

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ logs
2424
*.log
2525

2626
# Misc
27-
commands.adoc
27+
commands.adoc
28+
# Local Netlify folder
29+
.netlify

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
14
1+
16

netlify/functions/send-email.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Handler} from '@netlify/functions'
2+
import nodemailer from 'nodemailer'
3+
4+
export const handler: Handler = async (event, context) => {
5+
try {
6+
if (!event.body) throw new Error('event.body missing')
7+
const data = JSON.parse(event.body);
8+
const transporter = nodemailer.createTransport({
9+
host: 'smtp.gmail.com',
10+
port: 465,
11+
secure: true,
12+
auth: {
13+
user: process.env.SMTP_GMAIL_USER,
14+
pass: process.env.SMTP_GMAIL_PASS
15+
}
16+
});
17+
const mailOptions = {
18+
from: `"${data.from_name}" <${data.from_email}>`,
19+
to: 'bitcoin-studio@protonmail.com',
20+
subject: 'Bitcoin Studio Website Form',
21+
text: data.message
22+
}
23+
const messageInfo = await transporter.sendMail(mailOptions);
24+
console.log('messageInfo', messageInfo);
25+
return {
26+
statusCode: 200,
27+
body: JSON.stringify({message: "Mail sent successfully"}),
28+
headers: {'Content-Type': 'application/json'}
29+
};
30+
} catch (error) {
31+
console.error(error);
32+
return {
33+
statusCode: 500,
34+
body: JSON.stringify({error: (error as any).message}),
35+
headers: {'Content-Type': 'application/json'}
36+
};
37+
}
38+
}

package-lock.json

Lines changed: 81 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)