sails-mandrill
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.
Create a connection to your Mandrill account
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 sails@v0.9.x as well, but you'll need to use the
adapters
config, inconfig/adapters.js
, and set theadapter:'mandrill'
key in your model (instead ofconnections
).
config/connections.js
moduleexportsconnections = // ... // Mandrill 'my favorite mandrill account': adapter: 'sails-mandrill' apiKey: processenvMANDRILL_KEY // the api key for your mandrill account from: name: 'Default sender to use ( can be overriden in options to .send() )' email: 'default@sender.com' ;
Hook up a Model to your new connection
config/models/Email.js
moduleexports = connections: 'my favorite mandrill account';
\
Usage
Model.send ( [options], [callback] )
Generally, 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.
Send an HTML email:
Email; // 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!
Send a template from your Mandrill dashboard:
TODO: document this
Model.listTemplates ( [options], [callback] )
Get your templates. The key property is required.
Email;
Model.addTemplate ( [options], [callback] )
Add a template. The key and name properties are required
Email;
Using the adapter directly
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) sailsadaptersmandrill;}; ;