Skip to content

Instantly share code, notes, and snippets.

@amosuro
Created November 5, 2018 12:22
Show Gist options
  • Save amosuro/3fe2daac889d64f8723bdd600081eb8a to your computer and use it in GitHub Desktop.
Save amosuro/3fe2daac889d64f8723bdd600081eb8a to your computer and use it in GitHub Desktop.
Sending Form Data with ReactJS + NodeJS/Express + Amazon SES
import React, { Component } from 'react';
const post = async (data) => {
const { url } = data;
delete data.url;
const params = {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
const response = await fetch(url, params);
if (response.status < 200 && response.status >= 300) {
const res = await response.json();
throw new Error(res);
}
return response.json();
};
class FormComponent extends Component {
state = {
error: null,
submitted: false,
fields: { name: '', email: '', message: ''}
};
submitForm(event) {
const formElement = event.target;
const {name, email, message} = formElement.elements;
// build the request payload which includes the url of the end-point we want to hit
const payload = {
url: 'api/contact',
name: name,
email: email,
message: message,
};
// call the post helper function which returns a promise, which we can use to update the
// state of our component once returned
post(payload)
.then(() => {
// on success, clear any errors and set submitted state to true
this.setState({error: null, submitted: true});
})
.catch(error => {
// on error, update error state with the error message and set submitted to false
this.setState({error: error.message, submitted: false});
});
}
render() {
return (
<form ref={this.formElement}
onSubmit={(event) => this.submitForm(event)}>
<input type="text"
name="name"
id="name" />
<input type="email"
name="email"
id="email" />
<input type="textarea"
name="message"
id="message" />
<button type="submit">Submit</button>
</form>
);
}
}
export default FormComponent;
const AWS = require('aws-sdk'); // Load the SDK for JavaScript
const htmlTemplate = (data) => {
return `
<p><strong>Name:</strong> ${data.name}</p>
<p><strong>Email:</strong> <a href="mailto:${data.email}">${data.email}</a></p>
<p><strong>Message:</strong> ${data.message}</p>
`;
};
module.exports.sendMail = (sender, receivers, data) => {
const params = {
Destination: {
ToAddresses: receivers
},
Message: {
Subject: {
Charset: 'UTF-8',
Data: 'Website Enquiry'
},
Body: {
Html: {
Charset: "UTF-8",
Data: htmlTemplate(data)
}
}
},
Source: sender,
};
const sendPromise = new AWS.SES().sendEmail(params).promise();
return sendPromise
.then((data) => data)
.catch((err) => {
throw new Error(err);
});
};
const AWS = require('aws-sdk'); // Load the SDK for JavaScript
const mailer = require("./mailer");
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
AWS.config.update({region: 'eu-west-1'}); // Set the region that you configured in AWS
// Our end-point for handling the enquiry request
app.post('/api/contact', (req, res, next) => {
return mailer.sendMail('sender@email.com', ['reciever@email.com'], req.body)
.then(() => res.send(req.body))
.catch(next);
});
app.listen(port, () => console.log(`Listening on port ${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment