Skip to content

Instantly share code, notes, and snippets.

@qalqi
Last active August 29, 2015 14:16
Show Gist options
  • Save qalqi/4015da906e9cedad6e43 to your computer and use it in GitHub Desktop.
Save qalqi/4015da906e9cedad6e43 to your computer and use it in GitHub Desktop.
SailsJs Mandrill Controller to send emails from SailJs
/**
* EmailController
* @require :: npm install machinepack-mandrill --save
* @model :: sails generate email
* @description :: SailsJs Server-side logic for serving Emails using Mandrill https://mandrill.com/ [12000 Free Emails]
* @help :: See http://links.sailsjs.org/docs/controllers
*/
var Mandrill = require('machinepack-mandrill');
module.exports = {
sendmail: function(req, res) {
// Send a plaintext email to the specified recipient.
Mandrill.sendPlaintextEmail({
apiKey: 'qq_qqqqqqqqqqqqq-qq',
toEmail: req.param('toEmail'),
toName: req.param('toName'),
subject: 'Greetings!',
message: "Hello World, Regards" + req.param('fromName'),
fromEmail: req.param('fromEmail'),
fromName: req.param('fromName')
}).exec({
// An unexpected error occurred.
error: function (err){
return res.negotiate(err);
},
// OK.
success: function (){
Email.create({
toEmail: req.param('toEmail'),
toName: req.param('toName'),
fromEmail: req.param('fromEmail'),
fromName: req.param('fromName')
}, function emailCreated(err, newEmail) {
if (err) {
// Otherwise, send back something reasonable as our error response.
return res.negotiate(err);
}
// Send back the id of the new user
return res.json({
id: newEmail.id
});
});
},
});
}
};
/*
Copyright (C) <2015> @qalqi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment