Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-pop3

lianxh8kMIT0.9.1TypeScript support: included

POP3 client for node

pop3, node, Promise, Stream

readme

node-pop3

pop3 command support for node. Supports Promise and stream.

CLI

e.g. Test the API about TOP

pop -u example@gmail.com -p pwd -h example.pop.com -m TOP 100 10

or pass in some or all of the config in a designated config file (JSON or JS):

pop -c pop.config.js -m TOP 100 10

For more detail, please input

pop --help

Usage

In CommonJS, you can get the Pop3Command as follows:

const Pop3Command = require('node-pop3');

The examples below, however, use the ESM Modules format instead (i.e., import).

Example

  • Fetch mail by msgNum:
import Pop3Command from 'node-pop3';

const pop3 = new Pop3Command({
  user: 'example@example.com',
  password: 'example',
  host: 'pop3.example.com'
});

const msgNum = 1;

const str = await pop3.RETR(msgNum);
// deal with mail string
await pop3.QUIT();
  • List msgNum to uid in Server
const list = await pop3.UIDL();
console.dir(list);
/*
 * [
 *  ['1', 'ZC0113-H8wi_YChVab4F0QTbwP4B6i'],
 *  ['2', 'ZC0114-3A9gAn8M2Sp1RhVCGTIII6i'],
 *  ...
 * ]
*/

API

  • constructor(options)
params optional comment
options.user no String
options.password no String
options.host no String
options.port yes Number. Defaults to 110
options.servername yes String. Defaults to host value. Same as servername for Node TLS option.
options.tls yes Boolean. Defaults to false
options.timeout yes Number. Defaults to undefined
options.tlsOptions yes Defaults to {}

tlsOptions takes the options documented for your Node version and tls.connect function.

  • basic
method params return
connect |{Promise} resolve to undefined
command {String*} command messages to Server {Promise} resolve to {Array[String, Stream]}, which are messages of response and stream of response (if the response has multiple lines) from Server
listify Splits lines by CRLF, filters out empty lines, and converts each line to a an array based on splitting by spaces
const pop3 = new Pop3Command({host: 'pop3.example.com'});

// These must be in order
await pop3.connect();
await pop3.command('USER', 'example@example.com');
await pop3.command('PASS', 'example');

const [statInfo] = await pop3.command('STAT');
const [retrInfo, retrStream] = await pop3.command('RETR', 1);

console.log(statInfo); // 100 102400
console.log(retrInfo); // 1024 octets

const [quitInfo] = await pop3.command('QUIT');
console.log(quitInfo); // Logging out.

const streamString = await Pop3Command.stream2String(retrStream);
console.log(streamString); // <message details...>

console.log(
  await Pop3Command.listify(streamString)
); // [ ['Return-Path:', 'brett@...'], ...]
  • common
method params return comment
UIDL {String|Number} msgNum {Promise} resolve to {Array} list of responsed msgNum is optional
RETR {String|Number} msgNum {Promise} resolve to {String} of mail stream
TOP {String|Number} msgNum, {Number} n {Promise} resolve to {String} message of responsed n is default to 0
QUIT |{Promise} resolve to {String} message of response message

ERROR

pop3 will throw new Error's with an error message from Server. Beyond that, Error may have two properties attached by pop3.

property comment
err.eventName event name comes from socket.on. Includes error, close, timeout, end, and bad-server-response. command may also throw no-socket.
err.command which command causes the error. For example, PASS example

To-dos

  1. Testing:
    1. Set up CI with hidden pop.config.json credentials
    2. Avoid skipping some tests
  2. Edge cases
    1. After timeout, ensure any stream is ended (so other commands can continue)
    2. Ensure command will reject if socket is ended.
    3. Ensure in fixing the above that can QUIT and reuse same instance

changelog

CHANGES to node-pop3

0.9.1 (2025-04-15)

  • fix: avoid trailing spaces for parameterless commands (#34)
  • fix: destroy stream after timeout

0.9.0 (2023-05-24)

  • feat: ESM modules (BREAKING)
  • feat: TypeScript types
  • feat: added LAST command (@metafloor)
  • fix: LIST and UIDL with msg-nbr (@metafloor)
  • fix: add '.' to TERMINATOR_BUFFER_ARRAY constant (@Irykson)
  • docs: link to spec
  • docs: document listify

0.8.0 (2022-01-16)

  • Enhancement: Add servername option

0.7.0 (2021-10-22)

  • Enhancement: Add tlsOptions option
  • Fix: Drop TLS socket if erring (so subsequent connect can detect)
  • Fix: Report if no socket
  • Docs: Add sample pop.config-sample.json file; Clarify values
  • Chore: update devDeps. , add lint script, and switch to pnpm

0.6.0 (2020-08-10)

  • Breaking change: Change named exports listify and stream2String to attach to the default export class to enable require to avoid default.
  • Docs: Document CommonJS usage

0.5.1 (2020-07-19)

  • npm: Update devDeps (and build)

0.5.0 (2020-07-19)

  • Breaking enhancement: Add genuine Node exports

0.4.0 (2020-07-19)

  • Breaking change: Have public RETR stringify stream
  • Breaking change: Cause public UIDL to return single item if a message number was specified
  • Enhancement: Add public methods for LIST, RSET, DELE, STAT, NOOP

0.3.0 (2020-07-19)

  • Breaking change: Requires Node >= 6
  • Enhancement: Added timeout option (also to binary)
  • Enhancement: Re-export listify and streamtoString
  • Enhancement: If tls is set, default port to 995
  • Enhancement: Add source maps
  • Enhancement: Throw no-socket Error in command if no socket left
  • Enhancement (binary): Change test file to a genuine binary (as pop)
  • Enhancement (binary): Support passing in --config file (used also in tests)
  • Fix: Add bad-server-response error (handle obscure case where server doesn't follow spec or gives unknown response)
  • Fix: Reject command early upon error
  • Fix (CLI): For other commands, use _connect instead of connect to allow necessary authentication before practical use
  • Fix (CLI): Allow boolean args at any posiiton
  • Fix (CLI): Report bad alias (though ignore empty -- arg)
  • Fix (CLI) Avoid including reference to promise being uncaught in reported errors; catch any errors ourselves but log error to stderr
  • Fix (CLI): Ensure QUIT in CLI usage after every other command
  • Update: Avoid deprecated new Buffer in favor of Buffer.from
  • Refactoring: Misc. improvements/linting
  • Testing: Add Mocha + NYC testing