Build Your Own PHP Web Mailer | 2 | WebReference

Build Your Own PHP Web Mailer | 2


Build Your Own PHP Web Mailer [con't]

Receiving Email

When it comes to receiving emails, there are two mail protocols available: POP and IMAP. Each has its advantages and disadvantages. POP downloads all the messages to your PC and has only one directory, inbox. While it is useful in terms of mail storage, it causes problems if you are receiving mail on more than one machine. Some POP servers have the option to "leave message on the server," which simulates IMAP and overcomes this problem. Also with POP, because the messages are stored locally, you may lose all of your data if your PC breaks down. IMAP stores messages on the server and reads them only when you require it. In addition to that, IMAP's directory structure can be created on the server itself. The only challenge with IMAP is the storage space and Internet connection it requires. However, these shouldn't be a problem nowadays. Anyway, it's up to you to decide which one to use, as I will explain both.

POP Server

To connect to a POP server, we again use fsockopen() but issue slightly different commands. Notice that we don't have to say "HELO" now. Rather, we enter the username and password immediately:

The username and password are sent as plain text; there is no base64 encoding. After a successful login, you may issue some of the following commands:

  • STAT -- This command displays the number of messages in the inbox and their size in bytes. Example response:
  • LIST -- This command is the same as STAT, but it shows the details for each message in a new line. Example response:
    Notice the dot at the end. It signalizes the end of the listing.
  • TOP -- This command retrieves a part of the message (note: not all POP servers support this command). The number of lines to retrieve is specified. Headers are always returned, even if the number is 0. Example: