Jaxer POP3 Class
by Steven Brown on Nov.20, 2008, under Jaxer
Hi guys I thought I’d release this code to the public, it’s a POP3 class written in Jaxer. It’s only very basic for now (is missing commands like TOP, APOP and NOOP) and doesn’t do any error checking (it assumes your commands are successful).
It also isn’t in the regular Jaxer coding format, though I plan to convert it soon.
var pop3 = function pop3() { var socket; var writeSocket = function writeSocket(command) { socket.writeString(command + "\n"); socket.flush(); }; var readSocket = function readSocket() { return socket.readLine(); }; return { // Connect to the server, if no port is specified the default 110 is used connect: function connect(host, port) { socket = new Jaxer.Socket(); if (!port) { port = 110; } socket.open(host, port); readSocket(); }, // Log into the server login: function login(username, password) { writeSocket("USER " + username); readSocket(); writeSocket("PASS " + password); readSocket(); }, // Performs the stat command and returns the raw result stat: function stat() { writeSocket("STAT"); return readSocket(); }, // Returns the number of emails in the account count: function count() { return this.stat().split(" ")[1]; }, // Gets the size of an email (if MailNum passed) // Otherwise gets an array of all emails and their sizes // Note the array index is the number of the email and starts at 1 list: function list(mailNum) { if (mailNum) { writeSocket("LIST " + mailNum); return readSocket().split(" ")[1]; } writeSocket("LIST"); readSocket(); var mailList = []; while ((inLine = readSocket()) !== ".") { var parts = inLine.split(" "); mailList[parts[0]] = parts[1]; } return mailList; }, // Gets the unique identifier of an email (if mailNum passed) // Otherwise gets an array of all emails and their unique identifiers // Note the array index is the number of the email and starts at 1 identify: function identify(mailNum) { if (mailNum) { writeSocket("UIDL " + mailNum); return readSocket().split(" ")[2]; } writeSocket("UIDL"); readSocket(); var mailList = []; while ((inLine = readSocket()) !== ".") { var parts = inLine.split(" "); mailList[parts[0]] = parts[1]; } return mailList; }, // Return the size of an email in bytes size: function count(mailNum) { return this.list(mailNum).split(" ")[2]; }, // Get an email retrieve: function retrieve(mailNum) { writeSocket("RETR " + mailNum); readSocket(); var mailContent = ""; while ((inLine = readSocket()) !== ".") { mailContent += inLine + "\r\n"; } return mailContent; }, // Deletes an email, we can't call it delete because that's a reserved word // You will need to disconnect in order for the email to be deleted remove: function remove(mailNum) { writeSocket("DELE " + mailNum); return readSocket(); }, // "Undelete" any emails marked for deletion reset: function reset() { writeSocket("RSET"); return readSocket(); }, // Send the "QUIT" command (to delete any emails marked for deletion) and disconnect disconnect: function disconnect() { writeSocket("QUIT"); readSocket(); socket.close(); } }; };
Basic usage goes something like this:
var pop = pop3(); pop.connect("yourhost"); pop.login("youruser", "yourpass"); var mailCount = pop.count(); var uniqueIds = pop.identify(); for (var i = 1; i <= mailCount; i++) { var myFile = new Jaxer.File(Jaxer.Dir.resolve(Jaxer.request.currentFolder + "/mail/" + uniqueIds[i] + ".txt")); if (!myFile.exists) { var mail = pop.retrieve(i); myFile.open('w'); myFile.write(mail); myFile.close(); } } pop.disconnect();
This code will connect and get a list of mail, downloading them into the “mail” folder. If you refresh the page it will check again and download any emails it doesn’t already have.
Please feel free to offer feedback.