Mandrill adapter for sails/waterline >= v0.10
Mandrill adapter for sails.js
You can use this adapter directly, or from a model. I recommend you set up a connection, link it to a model, and access the functionality that way. It makes for cleaner and more reusable code. For instructions on using the adapter directly, see the bottom of this file.
Note: these instructions are for sails v0.10.x (currently unreleased, but available as the #associations branch on github). You'll probably be able to use this adapter with email@example.com as well, but you'll need to use the
config/adapters.js, and set the
adapter:'mandrill'key in your model (instead of
moduleexportsconnections =// ...// Mandrill'my favorite mandrill account':adapter: 'sails-mandrill'apiKey: processenvMANDRILL_KEY // the api key for your mandrill accountfrom:name: 'Default sender to use ( can be overriden in options to .send() )'email: 'firstname.lastname@example.org';
moduleexports =connections: 'my favorite mandrill account';
sails-mandrill accepts the same inputs as the official mandrill SDK for Node.js. It does, however, simplify the usage a bit for the most basic options.
You can override any of your connection defaults (including your API key!!!) in the options.
Emailsendto:name: 'Alvin'email: 'email@example.com'name: 'Chipettes'email: 'firstname.lastname@example.org'subject: 'omg i love you guys!!1'html:'I can\'t wait to see you all in Chicago<br/>' +'I loe you so much!!!! 'text: 'text fallback goes here-- in case some recipients (let\'s say the Chipettes) can\'t receive HTML emails'// If you need to wait to find out if the email was sent successfully,// or run some code once you know one way or the other, here's where you can do that.// If `err` is set, the send failed. Otherwise, we're good!;// A lot of times, in your controller, you may not necessarily want to wait until the email sends// In that case, you can go ahead and send your response down here.// Just keep in mind that delivery of an email is never 100% guaranteed, no matter what SMTP cloud you're using!
TODO: document this
Get your templates. The key property is required.
EmaillistTemplateskey: API_KEYlabel: "example-label"console.logtemplates;;
Add a template. The key and name properties are required
EmaillistTemplateskey: API_KEYname: "Example Template"from_email: "email@example.com"from_name: "Example Name"subject: "example subject"code: "<h1>example code</h1>"text: "Example text content"publish: falselabels:"example-label"console.logtemplate;;
As mentioned above, you can use the adapter directly. It's pretty much the same, except you won't have any default connection config mixed into your options, so you'll have to include your API key and
from config (ie. sender) each time you send an email:
var// Pass null as the first argument, since you aren't using the adapter from the context of a model.// (normally, the model name is passed implicitly as the first argument)sailsadaptersmandrillsendnull options cb;;sendEmail/* all the same stuff, just make sure you include everyting* you'd include in your connections configuration as well*/// If you need to wait to find out if the email was sent successfully,// or run some code once you know one way or the other, here's where you can do that.// If `err` is set, the send failed. Otherwise, we're good!;