Skip to content

Commit

Permalink
#92 Add plain text email for confirm contact
Browse files Browse the repository at this point in the history
Requires npm update!
  • Loading branch information
simison committed Dec 26, 2014
1 parent 056da39 commit 5550676
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
46 changes: 35 additions & 11 deletions app/controllers/contacts.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
sanitizeHtml = require('sanitize-html'),
htmlToText = require('html-to-text'),
nodemailer = require('nodemailer'),
userHandler = require('./users'),
messageHandler = require('./messages'),
Expand All @@ -24,8 +25,14 @@ exports.add = function(req, res) {

// Sanitize contact
function(done) {

// Catch message separately
var message = (req.body.message && req.body.message !== '') ? sanitizeHtml(req.body.message, messageHandler.messageSanitizeOptions) : false;
var messageHTML = false;
var messagePlain = false;
if(req.body.message && req.body.message !== '') {
messageHTML = sanitizeHtml(req.body.message, messageHandler.messageSanitizeOptions);
messagePlain = htmlToText.fromString(req.body.message, {wordwrap: 80});
}
delete req.body.message;

var contact = new Contact(req.body);
Expand All @@ -34,49 +41,66 @@ exports.add = function(req, res) {
contact.users.push(req.body.friendUserId);
contact.users.push(req.user._id);

done(null, contact, message);
done(null, contact, messageHTML, messagePlain);
},

// Find friend
function(contact, message, done) {
function(contact, messageHTML, messagePlain, done) {
User.findById(req.body.friendUserId, 'email').exec(function(err, friend) {
if (!friend) done(new Error('Failed to load user ' + req.body.friendUserId));

done(err, contact, message, friend);
done(err, contact, messageHTML, messagePlain, friend);
});
},

// Save contact
function(contact, message, friend, done) {
function(contact, messageHTML, messagePlain, friend, done) {
contact.save(function(err) {
done(err, contact, message, friend);
done(err, contact, messageHTML, messagePlain, friend);
});
},

// Prepare email for friend
function(contact, message, friend, done) {
// Prepare HTML email for friend
function(contact, messageHTML, messagePlain, friend, done) {

var url = (config.https ? 'https' : 'http') + '://' + req.headers.host;

res.render('email-templates/confirm-contact', {
name: friend.displayName,
message: message,
message: messageHTML,
meName: req.user.displayName,
meURL: url + '/#!/profile/' + req.user.username,
urlConfirm: url + '/#!/contact-confirm/' + contact._id,
}, function(err, emailHTML) {
done(err, emailHTML, friend);
done(err, contact, emailHTML, messagePlain, friend);
});
},

// Prepare TEXT email for friend
function(contact, emailHTML, messagePlain, friend, done) {

var url = (config.https ? 'https' : 'http') + '://' + req.headers.host;

res.render('email-templates-text/confirm-contact', {
name: friend.displayName,
message: messagePlain,
meName: req.user.displayName,
meURL: url + '/#!/profile/' + req.user.username,
urlConfirm: url + '/#!/contact-confirm/' + contact._id,
}, function(err, emailPlain) {
done(err, emailHTML, emailPlain, friend);
});
},

// If valid email, send reset email using service
function(emailHTML, friend, done) {
function(emailHTML, emailPlain, friend, done) {

var smtpTransport = nodemailer.createTransport(config.mailer.options);
var mailOptions = {
to: friend.email,
from: config.mailer.from,
subject: 'Confirm contact',
text: emailPlain,
html: emailHTML
};
smtpTransport.sendMail(mailOptions, function(err) {
Expand Down
17 changes: 17 additions & 0 deletions app/views/email-templates-text/confirm-contact.server.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'email.server.view.html' %}

{% block content %}
Hey {{name}}!

{{meName}} would like to connect with you on Trustroots.

{% if message %}
{{message}}
{% endif %}

To confirm or discard this connection, please click the link below.

<{{urlConfirm}}>

{{meName}}: {{meURL}}
{% endblock %}
38 changes: 38 additions & 0 deletions app/views/email-templates-text/email.server.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{#<!--
# Mail base template
#
# Based on:
# @link https://raw.githubusercontent.com/TomFrankly/mailchimp-responsive-template/master/Responsive%20Basic%20Chopped%20and%20Screwed.html
# License: @link http://creativecommons.org/licenses/by-sa/3.0/
#
#
#
#
# This email is optimized for mobile email clients, and even
# works relatively well in the Android Gmail App, which does
# not support Media Queries, but does have limited mobile-
# friendly functionality.
#
# While this coding method is very flexible, it can be more
# brittle than traditionally-coded emails, particularly in
# Microsoft Outlook 2007-2010. Outlook-specific conditional
# CSS is included to counteract the inconsistencies that
# crop up.
#-->
#}
{% if mailTitle %}{{ mailTitle }}
{% else %}{{title}}
{% endif %}
----------------------------------------------------------------------

{% block content %}{% endblock %}

--
Trustroots
{% if url %}{{url}}{% endif %}

Remember, I'm just a little mail robot so don't reply this email. Contact us if you want to talk to a human: {{url}}/#!/contact
{% if ourMail %}

Please add '{{ourMail}}' to your contact list to avoid our mails going to spam folder.
{% endif %}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"glob": "~4.0.5",
"grunt-cli": "~0.1.13",
"helmet": "~0.5.0",
"html-to-text": "^1.1.1",
"lodash": "~2.4.1",
"method-override": "~2.3.0",
"mongoose": "~3.8.19",
Expand Down

0 comments on commit 5550676

Please sign in to comment.