Fetching Mail
- 0
- Add a Comment
Fetching Mail
I’ve already expressed the fact that I’m partial to text email editors - Pine in particular. I also like being able to configure my mail retrieval program in my own way. For retrieving mail in Linux, I use fetchmail.
fetchmail is light and efficient in retrieving mail from most mail servers. It supports the POP2, POP3, IMAPbis, and IMAP4 protocols. fetchmail runs out to the servers and delivers the mail to the local mail delivery system, usually found at /var/spool/mail/username. fetchmail also delivers messages via SMTP through port 25 on the local machine. You can run fetchmail either via the command line or with configurations contained in the ~/.fetchmailrc file.
Here are some of the options for using fetchmail from the command line:
| Options | Actions |
| a | Retrieve all messages from the server, even those that have been seen previously, but left on the server. Fetchmail normally only retrieves new messages. |
| B n | The maximum number [n] of message to retrieve from the server. |
| b n | Sets the maximum number of messages sent [n] to an SMTP server in a single connection. |
| c | Checks for mail on a server without retrieving it. This option only works on IMAP servers. |
| F | Flushes previously retrieved messages from a server prior to fetching new ones. |
| K | Delete all retrieved messages from the mail server. |
| k | Keep copies of all retrieved messages on the mail server. |
| p | Specifies the mail protocol in use by the mail server from which you’re retrieving mail. |
| u name | Specifies the username to use on the mail server. |
| v | Display all status messages generated during a mail fetch. |
The flexibility of the command line is impressive. But, frankly, running this command with several options every time I want to check mail would be a pain. Instead, I’ve written a ~/.fetchmailrc file with all the pertinent information included. When this file exists in the user’s /home/ directory, fetchmail can be invoked with the fetchmail command, and will use the parameters of the ~/.fetchmailrc file.
Here’s a quick look at a typical ~/.fetchmailrc file:
poll mail.mymail.com proto POP3 user username pass password
poll mail.mysecondmail.com proto POP3 user username pass password
Notice that ~/.fetchmailrc has two important characteristics: a) it’s in my /home/ directory, as denoted by ~/, and b) it’s a hidden file, invisible to a normal ls command. This is evident from the ‘.’ before the file name. Before invoking fetchmail, you’ll also need to change the permissions on ~/.fetchmailrc to 710. Do this with the following command:
chmod 710 .fetchmailrc
As you own this file, you can, as a normal user, change the permissions. You don’t need to be root to do so.
Now, with the configuration and permissions set, I can start fetchmail with the following command:
fetchmail -d 600
This command puts the fetchmail program in daemon mode [-d] to check the servers listed in ~/.fetchmailrc every 600 seconds, or ten minutes. Mail is retrieved and stored in /var/mail/spool/tony, which is also defined in Pine as the inbox. Whenever I’m in a console window, fetchmail will alert me to new mail:
You have new mail in /var/spool/mail/tony
I simply open Pine, and there it is.
fetchmail is a great example of how you can set up any program in Linux to do what you want it to do, how you want it done.
