Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
smtp-tester
Advanced tools
Readme
smtp-tester is a simple smtp server that accepts connections, receives mail, and then calls callbacks that are bound to a particular address.
Installation is fairly straightforward, just install the npm module:
npm install smtp-tester
First, require smtp-tester:
var ms = require('smtp-tester');
Next, initialize a server with a port on which it should listen.
var mailServer = ms.init(port);
Done. Your SMTP server is now listening on port 'port'.
Send mail using any SMTP client you want. For node work, I personally use nodemailer
npm install nodemailer
To receive mail, bind a handler to the mailServer you created earlier.
var ms, mailServer, handler, port = 4000;
ms = require('smtp-tester');
mailServer = ms.init(port);
handler = function(addr,id,email) {
// do something interesting
};
mailServer.bind("foo@bar.com",handler);
Done. Every mail sent to foo@bar.com (and every one sent before binding) will call the handler exactly once.
You can have as many handlers as you want, they are all executed, even for the same address. However, execution order, while usually in the order in which they were added, is not guaranteed.
If you intend to capture a single message using promises, you can do:
mailServer.captureOne('foo@bar.com')
.then(function({ address, id, email }) {
// Do something interesting
});
Most likely you'll want to use the wait
option as well, so if no message is received
in the given time frame, captureOne()
rejects the promise:
mailServer.captureOne('foo@bar.com', { wait: 1000 })
.then(function({ address, id, email }) {
// Do something interesting
})
.catch(function(error) {
// No message delivered to foo@bar.com in 1 second.
});
Now using async/await:
try {
const { email } = await mailServer.captureOne('foo@bar.com', { wait: 1000 });
} catch (error) {
// No message delivered to foo@bar.com in 1 second.
}
This is useful for testing that no message was delivered, too.
If you want a handler to catch every email that is sent through the system, just bind with no address at all.
handler = function(addr,id,email) {
// do something interesting
// because this is a catch-all, the addr will be null
};
mailServer.bind(handler);
Catch-All handlers are always run before specific handlers.
To stop receiving mail at a particular handler, just unbind.
mailServer.unbind("foo@bar.com",handler);
If you want to remove a catch-all handler that catches every email that is sent through the system, just unbind with no address at all.
handler = function(addr,id,email) {
// do something interesting
// because this is a catch-all, the addr will be null
};
mailServer.bind(handler); // this adds it
mailServer.unbind(handler); // this removes it
To remove messages from the mail server, you can remove an individual message or all of them:
mailServer.remove(id);
mailServer.removeAll();
Surprisingly, the method is just called "stop".
mailServer.stop();
Handlers that receive mail are passed three parameters.
Sample email object is as follows, taken from the test.js included with the package.
{
sender: 'mailtest@bar.com',
receivers: {
'foo@bar.com': true
},
data: 'X-Mailer: Nodemailer (0.2.3; +http://www.nodemailer.org)\r\nDate: Thu, 01 Dec 2011 10:24:01 GMT\r\nFrom: mailtest@bar.com\r\nTo: foo@bar.com\r\nSubject: email test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nThis is a test mail',
body: 'This is a test mail',
headers: {
'x-mailer': 'Nodemailer (0.2.3; +http://www.nodemailer.org)',
date: 'Thu, 01 Dec 2011 10:24:01 GMT',
from: 'mailtest@bar.com',
to: 'foo@bar.com',
subject: 'email test',
'mime-version': '1.0',
'content-type': { value: 'text/plain' },
'content-transfer-encoding': 'quoted-printable'
}
}
smtp-tester supports pre-shipped modules. They are named and can be run by calling
var success;
// to load a module
success = mailServer.module(name);
// to unload a module
mailServer.unmodule(name);
If the module successfully loads, it will return success, else it will return false.
The following modules are currently available.
More are expected to follow.
Just run
npm test
Note that each build triggers a Travis CI build
FAQs
Quick and dirty smtp server, that accepts handlers to process messages
The npm package smtp-tester receives a total of 10,068 weekly downloads. As such, smtp-tester popularity was classified as popular.
We found that smtp-tester demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.