Skip to content

Commit

Permalink
Use Netlify function for email
Browse files Browse the repository at this point in the history
  • Loading branch information
Janaka-Steph committed Oct 19, 2022
1 parent 0ed7549 commit 4107e5f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 176 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PUBLIC_URL=/function/bitcoin-studio-client
REACT_APP_SEND_EMAIL_URL=/api/send-email
REACT_APP_SEND_EMAIL_URL=/.netlify/functions/send-email
SMTP_GMAIL_PASS=***
SMTP_GMAIL_USER=***@gmail.com
PORT=8081
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ logs
*.log

# Misc
commands.adoc
commands.adoc
# Local Netlify folder
.netlify
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
16
38 changes: 38 additions & 0 deletions netlify/functions/send-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Handler} from '@netlify/functions'
import nodemailer from 'nodemailer'

export const handler: Handler = async (event, context) => {
try {
if (!event.body) throw new Error('event.body missing')
const data = JSON.parse(event.body);
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.SMTP_GMAIL_USER,
pass: process.env.SMTP_GMAIL_PASS
}
});
const mailOptions = {
from: `"${data.from_name}" <${data.from_email}>`,
to: '[email protected]',
subject: 'Bitcoin Studio Website Form',
text: data.message
}
const messageInfo = await transporter.sendMail(mailOptions);
console.log('messageInfo', messageInfo);
return {
statusCode: 200,
body: JSON.stringify({message: "Mail sent successfully"}),
headers: {'Content-Type': 'application/json'}
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: JSON.stringify({error: (error as any).message}),
headers: {'Content-Type': 'application/json'}
};
}
}
164 changes: 81 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4107e5f

Please sign in to comment.