mirror of
https://github.com/eleith/emailjs.git
synced 2024-07-05 20:10:37 +00:00
allow overriding log
This commit is contained in:
parent
07b456ad9f
commit
1dad23e285
35
smtp/smtp.js
35
smtp/smtp.js
@ -57,12 +57,20 @@ const AUTH_METHODS = {
|
|||||||
let DEBUG = 0;
|
let DEBUG = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {...string} args the message(s) to log
|
* @param {...any} args the message(s) to log
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const log = (...args) => {
|
const log = (...args) => {
|
||||||
if (DEBUG === 1) {
|
if (DEBUG === 1) {
|
||||||
args.forEach(d => console.log(d));
|
args.forEach(d =>
|
||||||
|
console.log(
|
||||||
|
typeof d === 'object'
|
||||||
|
? d instanceof Error
|
||||||
|
? d.message
|
||||||
|
: JSON.stringify(d)
|
||||||
|
: d
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,6 +108,7 @@ class SMTP extends EventEmitter {
|
|||||||
* @property {boolean|SMTPSocketOptions} [ssl]
|
* @property {boolean|SMTPSocketOptions} [ssl]
|
||||||
* @property {boolean|SMTPSocketOptions} [tls]
|
* @property {boolean|SMTPSocketOptions} [tls]
|
||||||
* @property {string[]} [authentication]
|
* @property {string[]} [authentication]
|
||||||
|
* @property {function(...any): void} [logger]
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {SMTPOptions} [options] instance options
|
* @param {SMTPOptions} [options] instance options
|
||||||
@ -113,6 +122,7 @@ class SMTP extends EventEmitter {
|
|||||||
port,
|
port,
|
||||||
ssl,
|
ssl,
|
||||||
tls,
|
tls,
|
||||||
|
logger,
|
||||||
authentication,
|
authentication,
|
||||||
} = {}) {
|
} = {}) {
|
||||||
super();
|
super();
|
||||||
@ -204,6 +214,8 @@ class SMTP extends EventEmitter {
|
|||||||
// keep these strings hidden when quicky debugging/logging
|
// keep these strings hidden when quicky debugging/logging
|
||||||
this.user = /** @returns {string} */ () => user;
|
this.user = /** @returns {string} */ () => user;
|
||||||
this.password = /** @returns {string} */ () => password;
|
this.password = /** @returns {string} */ () => password;
|
||||||
|
|
||||||
|
this.log = typeof logger === 'function' ? logger : log;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -258,7 +270,7 @@ class SMTP extends EventEmitter {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const connected = () => {
|
const connected = () => {
|
||||||
log(`connected: ${this.host}:${this.port}`);
|
this.log(`connected: ${this.host}:${this.port}`);
|
||||||
|
|
||||||
if (this.ssl && !this.tls) {
|
if (this.ssl && !this.tls) {
|
||||||
// if key/ca/cert was passed in, check if connection is authorized
|
// if key/ca/cert was passed in, check if connection is authorized
|
||||||
@ -290,6 +302,7 @@ class SMTP extends EventEmitter {
|
|||||||
connected();
|
connected();
|
||||||
} else {
|
} else {
|
||||||
this.close(true);
|
this.close(true);
|
||||||
|
this.log(err);
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
SMTPError('could not connect', SMTPError.COULDNOTCONNECT, err)
|
SMTPError('could not connect', SMTPError.COULDNOTCONNECT, err)
|
||||||
@ -305,13 +318,13 @@ class SMTP extends EventEmitter {
|
|||||||
this.close(true);
|
this.close(true);
|
||||||
caller(callback, err);
|
caller(callback, err);
|
||||||
} else if (msg.code == '220') {
|
} else if (msg.code == '220') {
|
||||||
log(msg.data);
|
this.log(msg.data);
|
||||||
|
|
||||||
// might happen first, so no need to wait on connected()
|
// might happen first, so no need to wait on connected()
|
||||||
this._state = SMTPState.CONNECTED;
|
this._state = SMTPState.CONNECTED;
|
||||||
caller(callback, null, msg.data);
|
caller(callback, null, msg.data);
|
||||||
} else {
|
} else {
|
||||||
log(`response (data): ${msg.data}`);
|
this.log(`response (data): ${msg.data}`);
|
||||||
this.quit(() => {
|
this.quit(() => {
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
@ -327,7 +340,7 @@ class SMTP extends EventEmitter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this._state = SMTPState.CONNECTING;
|
this._state = SMTPState.CONNECTING;
|
||||||
log(`connecting: ${this.host}:${this.port}`);
|
this.log(`connecting: ${this.host}:${this.port}`);
|
||||||
|
|
||||||
if (this.ssl) {
|
if (this.ssl) {
|
||||||
this.sock = connect(
|
this.sock = connect(
|
||||||
@ -359,13 +372,13 @@ class SMTP extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
send(str, callback) {
|
send(str, callback) {
|
||||||
if (this.sock && this._state === SMTPState.CONNECTED) {
|
if (this.sock && this._state === SMTPState.CONNECTED) {
|
||||||
log(str);
|
this.log(str);
|
||||||
|
|
||||||
this.sock.once('response', (err, msg) => {
|
this.sock.once('response', (err, msg) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
caller(callback, err);
|
caller(callback, err);
|
||||||
} else {
|
} else {
|
||||||
log(msg.data);
|
this.log(msg.data);
|
||||||
caller(callback, null, msg);
|
caller(callback, null, msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -590,7 +603,7 @@ class SMTP extends EventEmitter {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
message(data) {
|
message(data) {
|
||||||
log(data);
|
this.log(data);
|
||||||
this.sock.write(data);
|
this.sock.write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -830,10 +843,10 @@ class SMTP extends EventEmitter {
|
|||||||
close(force = false) {
|
close(force = false) {
|
||||||
if (this.sock) {
|
if (this.sock) {
|
||||||
if (force) {
|
if (force) {
|
||||||
log('smtp connection destroyed!');
|
this.log('smtp connection destroyed!');
|
||||||
this.sock.destroy();
|
this.sock.destroy();
|
||||||
} else {
|
} else {
|
||||||
log('smtp connection closed.');
|
this.log('smtp connection closed.');
|
||||||
this.sock.end();
|
this.sock.end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user