wildduck/examples/push-message.js

110 lines
3.5 KiB
JavaScript
Raw Normal View History

/* eslint no-console: 0 */
'use strict';
const recipients = process.argv.slice(2);
const total = 1;
if (!recipients || !recipients.length) {
console.error('Usage: node example.com recipient1@exmaple.com [recipient2@exmaple.com...]'); // eslint-disable-line no-console
return process.exit(1);
}
2017-07-16 19:37:33 +08:00
const config = require('wild-config');
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
2017-04-17 20:58:46 +08:00
lmtp: true,
host: 'localhost',
2017-04-17 20:58:46 +08:00
port: config.lmtp.port,
logger: false,
2017-04-17 20:58:46 +08:00
debug: false,
tls: {
rejectUnauthorized: false
}
});
2017-03-27 04:58:05 +08:00
let sent = 0;
let startTime = Date.now();
2017-03-27 04:58:05 +08:00
function send() {
transporter.sendMail(
{
envelope: {
from: 'andris@kreata.ee',
to: recipients
},
headers: {
/*
// uncomment to send the messge to Junk
2018-01-30 22:14:15 +08:00
'X-Rspamd-Bar': '/',
'X-Rspamd-Report': 'R_PARTS_DIFFER(0.5) MIME_GOOD(-0.1) R_DKIM_ALLOW(-0.2) R_SPF_ALLOW(-0.2)',
'X-Rspamd-Score': '22.6'
*/
2017-03-27 04:58:05 +08:00
},
from: 'Kärbes 🐧 <andris@kreata.ee>',
2018-01-18 18:58:31 +08:00
to: recipients
.map((rcpt, i) => ({ name: 'Recipient #' + (i + 1), address: rcpt }))
.concat('andris <andris.reinman@gmail.com>, andmekala <andmekala@hot.ee>'),
cc: '"Juulius Orro" muna@gmail.com, kixgraft@gmail.com',
subject: 'Test ööö message [' + Date.now() + ']',
text: 'Hello world! Current time is ' + new Date().toString(),
html:
'<p>Hello world! Current time is <em>' +
new Date().toString() +
'</em> <img src="cid:note@example.com"/> <img src="http://www.neti.ee/img/neti-logo-2015-1.png"></p>',
attachments: [
// attachment as plaintext
{
filename: 'notes.txt',
content: 'Some notes about this e-mail',
contentType: 'text/plain' // optional, would be detected from the filename
},
2017-03-27 04:58:05 +08:00
// Small Binary Buffer attachment, should be kept with message
{
filename: 'image.png',
2018-05-11 19:39:23 +08:00
content: Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'base64'
),
cid: 'note@example.com' // should be as unique as possible
},
// Large Binary Buffer attachment, should be kept separately
{
path: __dirname + '/swan.jpg',
filename: 'swän.jpg'
}
]
},
(err, info) => {
if (err && err.response) {
console.log('Message failed: %s', err.response);
} else if (err) {
console.log(err);
} else {
console.log(info);
}
sent++;
if (sent >= total) {
console.log('Sent %s messages in %s s', sent, (Date.now() - startTime) / 1000);
return transporter.close();
} else {
send();
2017-03-27 04:58:05 +08:00
}
}
);
2017-03-27 04:58:05 +08:00
}
2017-03-30 01:06:09 +08:00
send();
/*
2017-03-27 04:58:05 +08:00
for (let i = 0; i < total; i++) {
send();
}
2017-03-30 01:06:09 +08:00
*/