forked from fritx/gulp-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·54 lines (47 loc) · 1.28 KB
/
index.js
File metadata and controls
executable file
·54 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var colors = require('ansi-colors');
var flatOpts = require('flat-options');
var log = require('fancy-log');
var nodemailer = require('nodemailer');
var path = require('path');
var through2 = require('through2');
module.exports = function (options) {
options = flatOpts(options, {
to: null,
from: null,
subject: null,
html: null,
text: null,
smtp: null,
});
return through2.obj(function (file, enc, callback) {
var transporter = nodemailer.createTransport('SMTP', options.smtp);
if (file.isNull()) {
this.push(file);
return callback();
}
var to = options.to.constructor === Array ? options.to.join(',') : options.to;
var subject = options.subject || path.basename(file.path);
var html = options.html || file.contents.toString();
var text = options.text || null;
return transporter.sendMail(
{
from: options.from,
to: to,
subject: subject,
generateTextFromHTML: true,
html: html,
text: text,
},
function (error, info) {
if (error) {
log.error(error);
} else {
log.info('Sent email', colors.cyan(subject), 'to', colors.red(to));
}
transporter.close();
callback();
}
);
});
};