Skip to content
Open
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
38 changes: 38 additions & 0 deletions client/modules/Email/Email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Component } from 'react';

class Email extends Component {

constructor(props) {
super(props);
this.state = {
to: "",
title: "",
body: ""
};
}

render() {
return (
<div className="row">
<div>Send an email: </div>
<form onSubmit={this.submitEmail.bind(this)}>
<div className="row">
<input id="to" ref="to" type="text"></input>
<label htmlFor="to">To: </label>
</div>
<div className="row">
<input id="title" ref="title" type="text"></input>
<label htmlFor="to">Title: </label>
</div>
<div className="row">
<input id="body" ref="body" type="text"></input>
<label htmlFor="to">Body: </label>
</div>
<button type="submit" name="action">Send</button>
</form>
</div>
);
}
}

export default Email;
50 changes: 50 additions & 0 deletions server/controllers/email.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Email from '../models/email';
import cuid from 'cuid';
import sanitizeHtml from 'sanitize-html';

// ADDPOST function
export function addPost(req, res) {
// Make sure a recipient, subject, and body are entered
if (!req.body.form.to || !req.body.form.title || !req.body.form.body) {
res.status(403).end();
}

// Make sure recipient email is valid, if not respond with 400 status
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(req.body.form.to))) {
res.status(400).json({ error: true, message: 'Invalid recipient address' });
return;
}

// Initialize and send email through sendgrid
const sgMail = require('../../node_modules/@sendgrid/mail');
sgMail.setApiKey('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');
const msg = {
to: req.body.form.to,
from: '[email protected]',
subject: req.body.form.title,
text: req.body.form.body,
};
sgMail
.send(msg)
.catch(error => { res.status(400).send(error); });

// Initialize new email form
const newForm = new Email(req.body.form);

// Sanitize inputs
newForm.to = sanitizeHtml(newForm.to);
newForm.title = sanitizeHtml(newForm.title);
newForm.body = sanitizeHtml(newForm.body);

newForm.cuid = cuid();
newForm.save((err, saved) => {
// If error sending email for whatever reason, respond with 500 status
if (err) {
res.status(500).send(err);
res.json({ form: saved, error: false, message: 'ERROR sending email' });
}
// If no error, respond with 200 status
res.json({ form: saved, error: false, message: 'Email sent!' });
res.status(200).end();
});
}// addPost
13 changes: 13 additions & 0 deletions server/models/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;

// Schema for email forms
const emailSchema = new Schema({
to: { type: 'String', required: true },
title: { type: 'String', required: true },
body: { type: 'String', required: true },
cuid: { type: 'String', required: true },
dateAdded: { type: 'Date', default: Date.now, required: true }
});

export default mongoose.model('Email', emailSchema);
9 changes: 9 additions & 0 deletions server/routes/email.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from 'express';
import * as EmailController from '../controllers/email.controller';

const router = new Router();

// Add route for emails
router.route('/email').post(EmailController.addPost);

export default router;
6 changes: 6 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import posts from './routes/post.routes';
import dummyData from './dummyData';
import serverConfig from './config';

// NEW: Import module for emails
import emails from './routes/email.routes';

// Set native promises as mongoose promise
mongoose.Promise = global.Promise;

Expand All @@ -72,6 +75,9 @@ app.use(bodyParser.urlencoded({ limit: '20mb', extended: false }));
app.use(Express.static(path.resolve(__dirname, '../dist/client')));
app.use('/api', posts);

// NEW: Apply body Parser and server public assets and routes for emails
app.use('/api', emails);

// Render Initial HTML
const renderFullPage = (html, initialState) => {
const head = Helmet.rewind();
Expand Down