UNPKG

1.33 kBJavaScriptView Raw
1/*
2 * grunt-mailgun
3 * http://github.com/markhuge/grunt-mailgun
4 *
5 * Copyright (c) 2014 Mark Wilkerson
6 * Licensed under the MIT license.
7 */
8
9/*jslint node: true */
10'use strict';
11
12var mailer = require('mailgun-send'),
13 _ = require('lodash');
14
15
16module.exports = function (grunt) {
17
18 // Please see the Grunt documentation for more information regarding task
19 // creation: http://gruntjs.com/creating-tasks
20
21 grunt.registerMultiTask('mailgun', 'Send emails through mailgun as part of your build.', function () {
22 var done = this.async();
23 var opts = _.pick(this.data.options,['sender','recipient','subject','body']);
24
25 // Register our mailer instance with out API key
26 mailer.config({key: this.data.options.key});
27
28 if (this.files.length > 0) {
29 var i = this.files.length;
30 this.filesSrc.forEach(function (filePath) {
31 var _opts = _.clone(opts);
32 _opts.body = grunt.file.read(filePath);
33 mailer.send(_opts, function () {
34 grunt.log.writeln('Sent' + filePath + ' to ' + _opts.recipient);
35 if (i < 1) { done(); } else { i--; } // This seems dirty
36 });
37
38 });
39
40 } else {
41 mailer.send(opts, function () {
42 grunt.log.writeln('Sent mailgun msg to ' + opts.recipient);
43 done();
44 });
45 }
46
47 });
48
49};