Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
reyiyo committed Nov 12, 2016
1 parent fd3bf6d commit 3f81315
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .esformatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"preset": "default",
"indent": {
"value": " "
},
"plugins": [
"esformatter-quotes",
"esformatter-use-strict",
"esformatter-align"
],
"quotes": {
"type": "single",
"avoidEscape": false
},
"align": {
"ObjectExpression": 1,
"VariableDeclaration": 1,
"AssignmentExpression": 1,
"TernaryExpression": 1,
"OrExpression": 1
}
}
182 changes: 182 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"env": {
"node": true,
"es6": true
},
"rules": {
"brace-style": [2,"1tbs",{}],
"camelcase": [2,{"properties":"never"}],
"comma-spacing": [2,{"after": true}],
"comma-style": ["error", "last"],
"computed-property-spacing": [2,"never"],
"consistent-this": [2,"self"],
"eol-last": 2,
"id-length": [1,{"min":3,"max":40,"exceptions":["i","j","k","x","y","z","to","Q","qs"]}],
"indent": 2,
"linebreak-style": [2,"unix"],
"max-nested-callbacks": [1,4],
"new-parens": 2,
"no-array-constructor": 1,
"no-continue": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multiple-empty-lines": [2,{"max":2}],
"no-nested-ternary": 2,
"no-new-object": 2,
"no-spaced-func": 2,
"no-trailing-spaces": [2,{"skipBlankLines":true}],
"no-underscore-dangle": 1,
"no-unneeded-ternary": 1,
"object-curly-spacing": [2,"never",{}],
"one-var": ["error","never"],
"operator-linebreak": [1,"after"],
"quotes": [2,"single"],
"semi-spacing": [2,{"after":true}],
"semi": [2,"always"],
"space-before-blocks": [2,"always"],
"space-before-function-paren": [2,"never"],
"space-in-parens": [2,"never"],
"space-infix-ops": [2,{"int32Hint":false}],
"space-unary-ops": [2,{"words":true}],
"wrap-regex": 2,
"arrow-parens": [2,"always"],
"arrow-spacing": [2,{"before":true,"after":true}],
"constructor-super": 2,
"generator-star-spacing": [2,{"before":false,"after":true}],
"no-class-assign": 2,
"no-const-assign": 2,
"no-this-before-super": 2,
"object-shorthand": [1,"never"],
"prefer-spread": 1,
"prefer-reflect": 1,
"require-yield": 2,
"max-depth": [1,4],
"max-len": [1,120,4,{"ignoreComments":false,"ignoreUrls":true}],
"max-params": [1,5],
"max-statements": [1,20],
"no-bitwise": 1,
"comma-dangle": [2,"never"],
"no-cond-assign": [2,"always"],
"no-console": 0,
"no-constant-condition": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-empty": 2,
"no-ex-assign": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": [2,"functions"],
"no-extra-semi": 2,
"no-func-assign": 2,
"no-inner-declarations": [1,"both"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-negated-in-lhs": 2,
"no-obj-calls": 2,
"no-regex-spaces": 2,
"no-sparse-arrays": 2,
"no-unreachable": 2,
"use-isnan": 2,
"valid-typeof": 2,
"no-unexpected-multiline": 2,
"block-scoped-var": 1,
"complexity": [2,5],
"curly": [2,"all"],
"default-case": 1,
"dot-notation": [1,{"allowKeywords":false}],
"dot-location": [1,"property"],
"eqeqeq": 2,
"no-alert": 2,
"no-caller": 2,
"no-else-return": 2,
"no-eq-null": 2,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-implicit-coercion": [2,{"boolean":true,"number":true,"string":true}],
"no-implied-eval": 2,
"no-invalid-this": 2,
"no-iterator": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 1,
"no-multi-spaces": ["error",{"exceptions": {"Property":true,"AssignmentExpression":true,"BinaryExpression":false,"VariableDeclarator":true,"ImportDeclaration":true}}],
"no-native-reassign": 2,
"no-new-func": 2,
"no-new-wrappers": 2,
"no-new": 2,
"no-octal-escape": 2,
"no-octal": 2,
"no-param-reassign": 1,
"no-proto": 2,
"no-redeclare": [2,{"builtinGlobals":true}],
"no-return-assign": [2,"always"],
"no-script-url": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-throw-literal": 2,
"no-unused-expressions": 2,
"no-useless-call": 1,
"no-void": 2,
"no-with": 2,
"yoda": [2,"never",{}],
"no-catch-shadow": 2,
"no-delete-var": 2,
"no-label-var": 2,
"no-shadow-restricted-names": 2,
"no-shadow": [2,{"builtinGlobals":true,"hoist":"all"}],
"no-undef": 2,
"no-undefined": 2,
"no-unused-vars": 2,
"no-use-before-define": 2,
"callback-return": 1,
"handle-callback-err": 1,
"no-new-require": 2,
"no-path-concat": 2,
"no-process-exit": 1,
"no-sync": 1,
"array-bracket-spacing": [2,"never",{}]
},

"globals": {
"$": false,
"angular": false,
"beforeEach": false,
"browser": false,
"browserify": false,
"Buffer": false,
"couch": false,
"define": true,
"devel": false,
"dojo": false,
"describe": false,
"expect": false,
"it": false,
"jasmine": false,
"jquery": false,
"mocha": false,
"mootools": false,
"node": false,
"module": false,
"nonstandard": false,
"proccess": false,
"prototypejs": false,
"qunit": false,
"require": false,
"requirejs": false,
"rhino": false,
"runs": false,
"shelljs": false,
"spyOn": false,
"typed": false,
"waits": false,
"waitsFor": false,
"worker": false,
"wsh": false,
"yui": false
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jspm_packages

# Optional REPL history
.node_repl_history
build/
27 changes: 27 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

'use latest';

const contact = require('./lib/contact');

function task(context, callback) {
return contact.validateParams(context)
.then(() => {
return contact.validateForm(context);
})
.then(() => {
return contact.sendMail(contact.createMessage(context), context);
})
.then((info) => {
console.info('Message sent: ' + info.response);
return callback(null, {
message: 'We\'ve received your message and we\'ll contact you very soon!'
});
})
.catch((err) => {
console.error(err);
return callback('There was a problem sending your message. Please try again in a few minutes');
});
}

module.exports = task;
118 changes: 118 additions & 0 deletions lib/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

'use latest';

const nodemailer = require('nodemailer');
const disposableEmail = require('temporary-email-address-validator');
const emailValidator = require('email-validator');
const request = require('request-promise');
const Q = require('q');

function isNotDefined(something) {
return typeof something === 'undefined' || something === null || something === '';
}

function isEmailValid(email) {
return emailValidator.validate(email) && disposableEmail.validate(email);
}

function validateParams(context) {
return Q.fcall(() => {
let secrets = ['SMTP_PORT', 'SMTP_HOST', 'SMTP_SECURE', 'SMTP_REQUIRE_TLS', 'SMTP_USER',
'SMTP_PASS', 'MAIL_TO', 'RECAPTCHA_URL', 'RECAPTCHA_SECRET'];

secrets.forEach((secret) => {
if (isNotDefined(context.data[secret])) {
throw new Error('Secret ' + secret + ' is missing');
}
});
});
}

function validateForm(context) {

return Q.fcall(() => {
if (isNotDefined(context.data['g-recaptcha-response'])) {
console.error('Captcha is invalid');
throw new Error('Sorry, there was a problem validating that you\'re a human!');
}
})
.then(function() {
return request({
method: 'POST',
url: context.data.RECAPTCHA_URL,
qs: {
secret: context.data.RECAPTCHA_SECRET,
response: context.data['g-recaptcha-response']
},
json: true
});
})
.catch((err) => {
console.error(err);
throw new Error('Sorry, there was a problem validating that you\'re a human!');
})
.then((response) => {
if (!response.success) {
console.error('Invalid Captcha');
throw new Error('Sorry, there was a problem validating that you\'re a human!');
}
})
.then(() => {
if (isNotDefined(context.data.name)) {
console.error('Name is undefined');
throw new Error('Sorry, you need to enter a name.');
}

if (isNotDefined(context.data.email)) {
console.error('Email is undefined');
throw new Error('Sorry, you need to enter a valid email.');
} else if (!isEmailValid(context.data.email)) {
console.error('Email is invalid');
throw new Error('Sorry, you need to enter a valid email.');
}
});
}

function createMessage(context) {
let text = 'Message received from the website\'s contact form:\n\n';

text = text.concat('Name: ' + context.data.name + '\n');
text = text.concat('Email: ' + context.data.email + '\n');
text = text.concat('Company: ' + context.data.company + '\n');
text = text.concat('Phone: ' + context.data.phone + '\n\n');
text = text.concat('Message: ' + context.data.message);

return text;
}

function sendMail(message, context) {
let transporter = nodemailer.createTransport({
port: parseInt(context.data.SMTP_PORT),
host: context.data.SMTP_HOST,
secure: Boolean(context.data.SMTP_SECURE),
requireTLS: Boolean(context.data.SMTP_REQUIRE_TLS),
auth: {
user: context.data.SMTP_USER,
pass: context.data.SMTP_PASS
}
});

let mailOptions = {
from: context.data.name + ' <' + context.data.email + '>',
to: context.data.MAIL_TO,
subject: 'Contact message from the web!',
text: message
};

return transporter.sendMail(mailOptions);
}

module.exports = {
isNotDefined: isNotDefined,
isEmailValid: isEmailValid,
validateParams: validateParams,
validateForm: validateForm,
createMessage: createMessage,
sendMail: sendMail
};
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "webtask-contact-form",
"version": "1.0.0",
"description": "A node.js server side form to receive emails from a contact-form in a web",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/systemdevs/webtask-contact-form.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/systemdevs/webtask-contact-form/issues"
},
"homepage": "https://github.com/systemdevs/webtask-contact-form#README",
"dependencies": {
"email-validator": "^1.0.7",
"nodemailer": "^2.6.4",
"request": "^2.78.0",
"request-promise": "^4.1.1",
"temporary-email-address-validator": "^1.0.3"
}
}

0 comments on commit 3f81315

Please sign in to comment.