From ae9402f838083135c327383749bf65f613079be5 Mon Sep 17 00:00:00 2001 From: Andrey Rublev Date: Tue, 8 Nov 2016 20:32:42 +0700 Subject: [PATCH] sending mails seems working now --- package.json | 1 + server/app.js | 13 +++++-- server/routes/mandrill.js | 39 +++++++++++++++++++ src/pages/custom/custom.jsx | 74 ++++++++++++++++++------------------- 4 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 server/routes/mandrill.js diff --git a/package.json b/package.json index 1439e6f3..097a4415 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "jsonwebtoken": "^5.7.0", "koa": "^1.1.2", "koa-better-static": "^1.0.5", + "koa-bodyparser": "^2.2.0", "koa-favicon": "^1.2.0", "koa-onerror": "^1.3.1", "koa-proxy": "^0.5.0", diff --git a/server/app.js b/server/app.js index 2f18222b..d5f908c2 100644 --- a/server/app.js +++ b/server/app.js @@ -9,6 +9,8 @@ import zipcodes from './routes/zipcodes'; import loadI18n from './i18n'; import verifyJwt from './verify-jwt'; import onerror from 'koa-onerror'; +import bodyParser from 'koa-bodyparser'; +import mandrillRouter from './routes/mandrill'; export default class App extends KoaApp { @@ -16,22 +18,25 @@ export default class App extends KoaApp { super(...args); onerror(this); + if (process.env.MAILCHIMP_API_KEY === void 0) { + throw new Error(`Can't load MAILCHIMP_API_KEY from environment.`); + } + this.use(serve('public')) .use(favicon('public/images/home/top-drawer-favicon.png')) .use(makeApiProxy()) .use(makeElasticProxy()) + .use(bodyParser()) .use(zipcodes.routes()) .use(zipcodes.allowedMethods()) + .use(mandrillRouter(process.env.MAILCHIMP_API_KEY)) .use(verifyJwt) .use(loadI18n) .use(renderReact); } start() { - if (process.env.MAILCHIMP_API_KEY === undefined) { - throw new Error(`Can't load MAILCHIMP_API_KEY from environment.`); - } - const port = process.env.LISTEN_PORT ? Number(process.env.LISTEN_PORT) : 4045; + const port = process.env.LISTEN_PORT ? Number(process.env.LISTEN_PORT) : 4046; this.listen(port); diff --git a/server/routes/mandrill.js b/server/routes/mandrill.js new file mode 100644 index 00000000..c0b393b0 --- /dev/null +++ b/server/routes/mandrill.js @@ -0,0 +1,39 @@ +import makeRouter from 'koa-router'; +import { Mandrill } from 'mandrill-api/mandrill'; + +function *sendMessage(mandrillClient, params) { + return new Promise((resolve, reject) => { + mandrillClient.messages.send(params, result => { + resolve(result); + }, error => { + const err = new Error(error.message || error); + reject(err); + }); + }); +} + +export default function mandrillRouter(apiKey) { + const mandrillClient = new Mandrill(apiKey); + + const router = makeRouter() + .post('/api/node/mandrill', function*() { + const { message } = this.request.body; + + let async = false; + let ip_pool = "Main Pool"; + let send_at = "example send_at"; + + console.log("WE ARE SENDING!"); + yield sendMessage(mandrillClient, { + 'message': message, + 'async': async + }); + + this.body = {}; + }); + + return router.routes(); +} + + + diff --git a/src/pages/custom/custom.jsx b/src/pages/custom/custom.jsx index 09d15246..a344d2d6 100644 --- a/src/pages/custom/custom.jsx +++ b/src/pages/custom/custom.jsx @@ -8,45 +8,33 @@ import styles from './custom.css'; import { Form, FormField } from 'ui/forms'; import { TextInput } from 'ui/inputs'; import Button from 'ui/buttons'; -import mandrill from 'mandrill-api/mandrill'; +import { api } from 'lib/api'; type State = { - mandrill_client: any + name: string, + email: string, + message: string, } class Custom extends Component { - state: Props; - - //const mandrill_client = - componentWillMount() { - console.log("Our Key:" + process.env.MAILCHIMP_API_KEY); - this.setState({ - mandrill_client: new mandrill.Mandrill(process.env.MAILCHIMP_API_KEY) - }); - } + state: State = { + name: '', + email: '', + message: '', + }; @autobind sendCustomEmail() { - let message = { - "html": "

TopDrawer Custom Sock Request

The details are below.

", - "text": "Top Drawer Custom Sock Request. The details are below.", - "from_email": "marketing.team@topdrawer.com", - "to": "adil@adilwali.com" - } - - let async = false; - let ip_pool = "Main Pool"; - let send_at = "example send_at"; - - console.log("WE ARE SENDING!"); - this.state.mandrill_client.messages.send({ - "message": message, - "async": async - }, function(result) { - console.log(result); - }, function(e) { - console.log('A mandrill error occured:' + e.name + ' - ' + e.message); - }) + const message = { + 'html': this.state.message, + 'from_email': this.state.email, + 'to': [{ + email: 'adil@adilwali.com', + name: 'Adil Wali' + }] + }; + + api.post('/node/mandrill', { message }); } get topBanner(): HTMLElement { @@ -78,6 +66,14 @@ class Custom extends Component { ); } + @autobind + handleFormChange(event) { + const { target } = event; + this.setState({ + [target.name]: target.value, + }); + } + get reachOut(): HTMLElement { return (
@@ -92,24 +88,24 @@ class Custom extends Component {

-
- - + + - - + - -