How to access an email server using POP in Javascript

5th Jul 2023

Share this article:

Accessing an email server from Javascript

The majority of email servers enable access using either IMAP or POP.

POP preceded IMAP as an email server protocol. While IMAP provides a lot of functionality for essentially constructing thin clients over the top of the email server (you leave the emails on the server), POP (post office protocol) is a simple protocol that enables users to quickly download and then delete the contents of their mailbox.

When using an email client, you should prefer IMAP but if all you want to do is programmatically access a mailbox then POP is nice and simple.

Brief overview of Post Office Protocol

The protocol is pretty simple. It's implemented over the top of TCP. You start by opening a TCP connection to the email server and then executing commands that the server responds to. Once you've connected, and the server has issued a greeting, you are in the AUTHORIZATION state. In this state, you must identify yourself (the client) to the server. This can be done using USER/PASS or APOP. Once you've successfully done this you're now in the TRANSACTION state and can issue commands to inspect the mailbox, retrieve and delete messages. Finally, you call QUIT to disconnect from the mail server.

An example session looks like:

S: <wait for connection on TCP port 110>
C: <open connection>
S:    +OK [pop.imitate.email] POP3 Service Ready.
C:    APOP mjerz c4c9334bac560ecc979e58001b3e22fb
S:    +OK mjerz's maildrop has 2 messages (320 octets)
C:    STAT
S:    +OK 2 320
C:    LIST
S:    +OK 2 messages (320 octets)
S:    1 120
S:    2 200
S:    .
C:    RETR 1
S:    +OK 120 octets
S:    <the POP3 server sends message 1>
S:    .
C:    DELE 1
S:    +OK message 1 deleted
C:    RETR 2
S:    +OK 200 octets
S:    <the POP3 server sends message 2>
S:    .
C:    DELE 2
S:    +OK message 2 deleted
C:    QUIT
S:    +OK POP3 server signing off (maildrop empty)
C:  <close connection>
S:  <wait for next connection>

In the TRANSACTION state you issue the following commands:

  • STAT - tells you how many and how big the emails are in the mailbox
  • LIST - returns a line for each message containing the message number and size
  • RETR - returns the content of a particular message (identified by the number)
  • DELE - marks a message as deleted (only actually deleted after QUIT)
  • NOOP - does nothing, used to keep the connection alive
  • RSET - unmarks all messages marked as deleted

Using Javascript to access a mailbox

There are several well-used libraries available through npm for connecting to a mail server over POP. For this example, we're going to make use of node-pop3 which provides a very simple API that mimics the protocol as opposed to providing an abstraction over the top.

Once you have your emails retrieved over POP you normally want to inspect the emails and extract information from them. POP3 simply provides back an email in raw form which is difficult to process. To help with this we use the mailparser library that is part of Nodemailer. It handles parsing the raw email content and provides back an object containing the properties that you would expect (subject, from, to, html, attachments etc)

As an example, we're going to connect to the Imitate Email sandbox email server over POP:

const Pop3Command = require('node-pop3');
const simpleParser = require('mailparser').simpleParser;

const pop3 = new Pop3Command({
  user: 'xxx-...',
  password: 'xxx-...',
  host: 'pop.imitate.email',
  servername: 'pop.imitate.email',
  port: 995,
  tls: true // the connection is encrypted over SSL
});

const msgNum = 1;

(async() => {
    // get the list of messages - this also connects and does USER/PASS as needed
    const list = await pop3.LIST();
    console.log(list);

    // execute the STAT command
    const [statInfo] = await pop3.command('STAT');
    console.log(statInfo);

    // get the raw content of the first message
    const str = await pop3.RETR(msgNum);

    // parse the email using mailparser
    let email = await simpleParser(str);
    console.log(email.subject);

    // end the session
    await pop3.QUIT();
})();

Next steps

It's easy to access email over POP using Javascript. Are you looking for somewhere to try it out?...

Imitate Email provides a sandbox email server which makes building, testing and demoing email functionality very easy.

Getting started with Imitate Email is easy (and free): You can sign up using just email or a social account.