node-poplib
node-poplib offers an MIT-licensed client library for the POP3 protocol. It is currently RFC1939 compliant and offers the following capabilities:
- USER, PASS, APOP
- LIST, TOP, RETR, DELE
- UIDL, NOOP, CAPA
- RSET, QUIT
- Plaintext and TLS support
Installation
You have two installation options:
-
Via npm:
npm install node-pop3-client
-
Download the source and install it yourself
Usage
node-pop3-client is event based. It is best to illustrate via examples:
Here we initialize the client (for plain text transmission):
var POP3Client = ;var client = port host tls;
If the tls parameter is true, the library will use a TLS connection. Note that you will have to set the correct port (generally 995).
Next, we trap several common states:
client; client; client; client;
The error event is emitted when there is a network error. The Node.js error object is passed back to user-code.
The connect event is emitted when the connection to the remote server is successful.
The invalid-state event is emitted when you try to carry out an action not allowed within your current state (eg, attempting to RETR-ieve a message when authentication has not been completed).
The locked event is emitted when you try to execute another command while the current command has not finished executing successfully (eg, attempting to RETR-ieve a message while the remote server has not finished sending LIST data).
On a successful connect, we try authenticating:
client;
Note that on successful auth, we try listing. For all events, the first received argument is always a boolean indicating whether the command succeeded. The last received argument is always the raw unparsed data received from the remote server. The intermediate arguments contain parsed data.
client; // Data is a 1-based index of messages, if there are any messagesclient; client; client; client;
See tests and demos for more examples.
Tests & Demos
For test purposes, you can use the following sendmail.sh script to pump email into your SMTP server for retrieval via POP3:
./sendmail.sh 10 "user@example.com" "this is my subject" "this is my body"
There is a full-featured POP3 client example in tests/demo.js
.
There is also a simple example of downloading all emails in a POP3 server and saving it locally in an mbox formatted file in tests/retrieve-all.js
.
There is a TLS example in tests/tls.js
.
If you want to try APOP support, see tests/apop.js
.