Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Netlify functions to send contact form email #7

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
.*
ehthumbs.db
Thumbs.db
node_modules
client/dist/*pre*.js

# PROJECT FILES
!.browserslistrc
!.editorconfig
!.prettierignore
!.prettierrc

# Build folders
lambda
build
bower_components
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# website

www.andreacarraro.it

## Setup

### Install dependencies

Run:

```
npm i && npm i netlify-cli -g
```

### .env file

Create a `.env` file with the following values

```
SENDGRID_API_KEY=
SEND_EMAIL_TO=
```
82 changes: 82 additions & 0 deletions functions/send-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const sgMail = require('@sendgrid/mail');
var emailValidator = require('email-validator');
const SENDER_EMAIL = process.env.SEND_EMAIL_TO;
const FORM_SPAM_FIELD = 'mail';

function sendEmail({to, replyTo, subject, text}) {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
from: SENDER_EMAIL,
to,
replyTo,
subject,
text,
};

return sgMail
.send(msg)
.then(([res]) => ({statusCode: res.statusCode, body: res.body}));
}

function replyMessageTemplate({name, message, senderEmail}) {
return `Hi ${name},
thank you for contacting me.

I'll endeavour to reply to you shortly.
Below you'll find a copy of your message.

Greetings!
Andrea Carraro

------+------
${message}
------+------
andreacarraro.it | ${senderEmail}`;
}

exports.handler = function(event, context, callback) {
if (event.httpMethod !== 'POST') {
return {statusCode: 405, body: 'Method Not Allowed'};
}

const data = JSON.parse(event.body);

if (!data.message) {
callback(new Error('Validation error: no message provided'));
}

if (!data.name) {
callback(new Error('Validation error: no name provided'));
}

if (!emailValidator.validate(data.email)) {
callback(new Error('Validation error: invalid email provided'));
}

if (data[FORM_SPAM_FIELD]) {
callback(new Error('Validation error: spam field NOT empty'));
return;
}

const emailToUser = sendEmail({
to: data.email,
replyTo: SENDER_EMAIL,
subject: 'andreacarraro.it: message received!',
text: replyMessageTemplate({
name: data.name,
message: data.message,
senderEmail: SENDER_EMAIL,
}),
});

const emailToAdmin = sendEmail({
to: SENDER_EMAIL,
replyTo: data.email,
subject: 'Message sent from andreacarraro.it',
text: data.message,
});

Promise.all([emailToUser, emailToAdmin])
.then(([emailUserResponse]) => callback(null, emailUserResponse))
.catch(errors => callback(errors, null));
};
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[build]
command = "npm run build"
functions = "lambda/"
publish = "build"

[dev]
framework = "#custom"
command = "npm run start:page"
targetPort = 3000
Loading