Skip to content

Commit 2cc62d3

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

File tree

10 files changed

+160
-164
lines changed

10 files changed

+160
-164
lines changed

.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
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+
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+
}

netlify/functions/send-email/package-lock.json

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "send-mail",
3+
"dependencies": {
4+
"@netlify/functions": "^1.3.0",
5+
"nodemailer": "^6.8.0"
6+
},
7+
"devDependencies": {
8+
"@types/node": "^16.11.68",
9+
"@types/nodemailer": "^6.4.6"
10+
}
11+
}

package-lock.json

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

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"scripts": {
66
"analyze": "source-map-explorer 'dist/static/js/*.js'",
7-
"build": "react-scripts build && rm -rf dist && mv build dist",
7+
"build": "react-scripts build",
88
"eject": "react-scripts eject",
99
"lint": "eslint --ext .ts,.tsx src",
1010
"lint:fix": "eslint --fix .",
@@ -28,7 +28,7 @@
2828
},
2929
"dependencies": {
3030
"@sweetalert/with-react": "0.1.1",
31-
"@types/node": "14.11.2",
31+
"@types/node": "16.11.68",
3232
"@types/react": "16.14.32",
3333
"@types/react-dom": "16.9.16",
3434
"@types/react-helmet": "6.1.5",
@@ -40,14 +40,11 @@
4040
"axios": "1.1.3",
4141
"concurrently": "7.4.0",
4242
"cors": "2.8.5",
43-
"dotenv": "16.0.3",
44-
"express-validator": "6.14.2",
4543
"formik": "2.2.9",
4644
"i18next": "21.10.0",
4745
"i18next-browser-languagedetector": "6.1.8",
4846
"loglevel": "1.8.0",
4947
"node-sass": "7.0.3",
50-
"nodemailer": "6.8.0",
5148
"nodemon": "2.0.20",
5249
"normalize.css": "8.0.1",
5350
"react": "16.14.0",
@@ -71,6 +68,6 @@
7168
]
7269
},
7370
"engines": {
74-
"node": "14"
71+
"node": "16"
7572
}
7673
}

0 commit comments

Comments
 (0)