forked from nodemailer/nodemailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-stream.js
41 lines (35 loc) · 1.05 KB
/
plugin-stream.js
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
/* eslint no-console: 0 */
'use strict';
// This example demonstrates the 'stream' step with a plugin that converts all spaces to tabs
var nodemailer = require('../lib/nodemailer');
var transporter = nodemailer.createTransport({
transport: 'stub' // load dynamically nodemailer-stub-tranport
});
var plugin = new(require('stream').Transform)();
plugin._transform = function (chunk, encoding, done) {
// replace all spaces with tabs in the stream chunk
for (var i = 0; i < chunk.length; i++) {
if (chunk[i] === 0x20) {
chunk[i] = 0x09;
}
}
this.push(chunk);
done();
};
transporter.use('stream', function (mail, callback) {
// apply output transformer to the raw message stream
mail.message.transform(plugin);
callback();
});
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'hello',
text: 'hello world!'
}, function (err, info) {
if (err) {
console.log(err.message);
} else {
console.log(info.response.toString());
}
});