smtp/client: invoke callback when send is successful

allows awaiting message result when send is promisified
This commit is contained in:
Zack Schuster 2020-10-30 13:47:53 -07:00 committed by Zack Schuster
parent c13356b51e
commit d799559261
2 changed files with 42 additions and 1 deletions

View File

@ -63,6 +63,9 @@ export class SMTPClient {
}
this.queue.push(stack);
this._poll();
if (this.queue.every((x) => x !== stack)) {
callback(null, message);
}
} else {
callback(new Error(why), msg);
}

View File

@ -4,7 +4,13 @@ import test from 'ava';
import { simpleParser, ParsedMail } from 'mailparser';
import { SMTPServer } from 'smtp-server';
import { DEFAULT_TIMEOUT, SMTPClient, Message, MessageHeaders } from '../email';
import {
DEFAULT_TIMEOUT,
SMTPClient,
Message,
MessageHeaders,
isRFC2822Date,
} from '../email';
const parseMap = new Map<string, ParsedMail>();
const port = 3000;
@ -309,3 +315,35 @@ test('client only responds once to greylisting', async (t) => {
);
t.is(error, "bad response on command 'RCPT': greylist");
});
test('client send can have result awaited when promisified', async (t) => {
// bind necessary to retain internal access to client prototype
const sendAsync = promisify(client.send.bind(client));
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'piglet@gmail.com',
bcc: 'pooh@gmail.com',
text: "It is hard to be brave when you're only a Very Small Animal.",
};
try {
const message = await sendAsync(new Message(msg));
t.true(message instanceof Message);
t.like(message, {
alternative: null,
content: 'text/plain; charset=utf-8',
text: "It is hard to be brave when you're only a Very Small Animal.",
header: {
bcc: 'pooh@gmail.com',
from: 'piglet@gmail.com',
subject: '=?UTF-8?Q?this_is_a_test_TEXT_message_from_emailjs?=',
},
});
t.deepEqual(message.attachments, []);
t.true(isRFC2822Date(message.header.date as string));
t.regex(message.header['message-id'] as string, /^<.*[@]{1}.*>$/);
} catch (err) {
t.fail(err);
}
});