2017-03-19 21:57:53 +08:00
/* eslint no-console: 0 */
2017-03-07 00:27:04 +08:00
'use strict' ;
const recipient = process . argv [ 2 ] ;
2017-04-02 00:22:47 +08:00
const total = Number ( process . argv [ 3 ] ) || 1 ;
2017-03-07 00:27:04 +08:00
if ( ! recipient ) {
console . error ( 'Usage: node example.com username@exmaple.com' ) ; // eslint-disable-line no-console
return process . exit ( 1 ) ;
}
const config = require ( 'config' ) ;
const nodemailer = require ( 'nodemailer' ) ;
const transporter = nodemailer . createTransport ( {
2017-03-27 04:58:05 +08:00
pool : true ,
maxConnections : 10 ,
2017-03-07 00:27:04 +08:00
host : 'localhost' ,
port : config . smtp . port ,
2017-03-21 06:07:23 +08:00
logger : false ,
debug : false
2017-03-07 00:27:04 +08:00
} ) ;
2017-03-27 04:58:05 +08:00
let sent = 0 ;
let startTime = Date . now ( ) ;
2017-03-21 17:35:59 +08:00
2017-03-27 04:58:05 +08:00
function send ( ) {
2017-03-21 17:35:59 +08:00
2017-03-27 04:58:05 +08:00
transporter . sendMail ( {
envelope : {
from : 'andris@kreata.ee' ,
to : [ recipient ]
2017-03-21 17:35:59 +08:00
} ,
2017-04-03 18:39:39 +08:00
from : 'Kärbes 🐧 <andris@kreata.ee>' ,
2017-04-03 21:59:04 +08:00
to : 'Ämblik 🦉 <' + recipient + '>, andmekala@hot.ee, Müriaad Polüteism <müriaad@müriaad-polüteism.org>' ,
2017-03-27 04:58:05 +08:00
subject : 'Test ööö message [' + Date . now ( ) + ']' ,
2017-04-03 18:39:39 +08:00
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>' ,
2017-03-27 04:58:05 +08:00
attachments : [
// attachment as plaintext
{
filename : 'notes.txt' ,
content : 'Some notes about this e-mail' ,
contentType : 'text/plain' // optional, would be detected from the filename
} ,
// Small Binary Buffer attachment, should be kept with message
{
filename : 'image.png' ,
content : new Buffer ( 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC' , 'base64' ) ,
cid : 'note@example.com' // should be as unique as possible
} ,
2017-03-21 17:35:59 +08:00
2017-03-27 04:58:05 +08:00
// 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 ) ;
2017-03-21 17:35:59 +08:00
}
2017-03-27 04:58:05 +08:00
sent ++ ;
if ( sent >= total ) {
console . log ( 'Sent %s messages in %s s' , sent , ( Date . now ( ) - startTime ) / 1000 ) ;
return transporter . close ( ) ;
2017-04-02 00:22:47 +08:00
} else {
2017-03-30 01:06:09 +08:00
send ( ) ;
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
* /