npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sails-mandrill

v0.10.2

Published

Mandrill adapter for sails/waterline >= v0.10

Downloads

12

Readme

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 [email protected] as well, but you'll need to use the adapters config, in config/adapters.js, and set the adapter:'mandrill' key in your model (instead of connections).

config/connections.js
module.exports.connections = {

  // ...
  
  // Mandrill
  'my favorite mandrill account': {
    adapter: 'sails-mandrill',
    apiKey: process.env.MANDRILL_KEY, // the api key for your mandrill account
    from: {
      name: 'Default sender to use ( can be overriden in options to .send() )',
      email: '[email protected]'
    }
  }
};

Hook up a Model to your new connection

config/models/Email.js
module.exports = {
  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.send({
  to: [{
    name: 'Alvin',
    email: '[email protected]'
  }, {
    name: 'Chipettes',
    email: '[email protected]'
  }],
  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'
}, function optionalCallback (err) {
  // 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!

Send a template from your Mandrill dashboard:

TODO: document this

Model.listTemplates ( [options], [callback] )

Get your templates. The key property is required.

Email.listTemplates({
  key: API_KEY,
  label: "example-label"
}, function optionalCallback (err, templates) {
    console.log(templates);
});

Model.addTemplate ( [options], [callback] )

Add a template. The key and name properties are required

Email.listTemplates({
  key: API_KEY,
  name: "Example Template",
  from_email: "[email protected]",
  from_name: "Example Name",
  subject: "example subject",
  code: "<h1>example code</h1>",
  text: "Example text content",
  publish: false,
  labels: [
    "example-label"
  ]
}, function optionalCallback (err, template) {
    console.log(template);
});

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 sendEmail = function (options, cb) {
  // 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)
  sails.adapters.mandrill.send(null, options, cb);
};

sendEmail({
  /* all the same stuff, just make sure you include everyting 
   * you'd include in your connections configuration as well
   */
}, function optionalCallback (err) {
  // 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!
});