Retrieving Emails From Remote Servers With fetchmail on Debian (2024)

On this page

  1. 1 Preliminary Note
  2. 2 Install fetchmail
  3. 3 Configure fetchmail
    1. 3.1 Run fetchmail As A Daemon With A Global Configuration File
    2. 3.2 Use Per-User Configuration Files And Run fetchmail Via Cron
  4. 4 Links

Fetchmail is a program for retrieving emails from remote servers. Imagine you have five email accounts on five different servers. Of course, you don't want to connect to each of them to get your emails. This is where fetchmail comes into play. If you have a user account on a Linux server, you can make fetchmail download emails from remote servers and put them into just one mailbox (the one of your Linux user), from where you can retrieve them with your email client (e.g. Thunderbird or Outlook).

Or imagine you have an email account at a provider that doesn't do spam- and virus filtering. In that case you could use fetchmail to download the mails to your own server and pipe them through spam- and virus filters (e.g. SpamAssassin and ClamAV) before you download the mails with your email client.

1 Preliminary Note

You need a Linux server with a system user that can receive emails, which means an MTA such as Postfix or Sendmail must be installed on the system. Otherwise, fetchmail won't work, because it tries to pass on the downloaded emails to an MTA (Postfix, Sendmail, ...), and the MTA delivers the mails to the user's mailbox (you can configure the system to include spam- and virus scanning in this process, e.g. with amavisd-new or procmail, but this isn't covered in this tutorial).

I use a Debian system in this tutorial where two users called falko and till exist.

2 Install fetchmail

In order to install fetchmail, all we have to do is run

apt install fetchmail

3 Configure fetchmail

There are two ways of configuring fetchmail. We can make it run as a daemon with a global configuration file, or we can create a cron job to run fetchmail together with per-user configuration files. I will describe both methods here.

3.1 Run fetchmail As A Daemon With A Global Configuration File

To make fetchmail run as a daemon, we have to edit /etc/default/fetchmail and set START_DAEMON to yes:

nano /etc/default/fetchmail
# This file will be used to declare some vars for fetchmail## Uncomment the following if you dont want localized log messages# export LC_ALL=C# Declare here if we want to start fetchmail. 'yes' or 'no'START_DAEMON=yes

Next we must create the configuration file /etc/fetchmailrc because the fetchmail daemon won't start if this file doesn't exist. In this file we can specify how the fetchmail daemon should behave as well as the details fetchmail needs to know to retrieve emails from foreign email accounts.

Let's assume falko has two email accounts from which we want to retrieve emails:

  • First account: server pop.someprovider.tld, protocol POP3, username [emailprotected] (yes, the username is an email address in this case), password secret.
  • Second account: server mail.otherprovider.tld, protocol POP3, username ftimme, password verysecurepassword.

till has one email account:

  • Server mailin.tillsprovider.tld, protocol POP3, username tbrehm, password iwonttellyou.

So our file /etc/fetchmailrc could look like this:

nano /etc/fetchmailrc
# /etc/fetchmailrc for system-wide daemon mode# This file must be chmod 0600, owner fetchmailset daemon 300 # Pool every 5 minutesset syslog # log through syslog facilityset postmaster rootset no bouncemail # avoid loss on 4xx errors # on the other hand, 5xx errors get # more dangerous...########################################################################### Hosts to pool########################################################################### Defaults ===============================================================# Set antispam to -1, since it is far safer to use that together with# no bouncemaildefaults:timeout 300antispam -1batchlimit 100poll pop.someprovider.tld protocol POP3 user "[emailprotected]" there with password "secret" is falko herepoll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" is falko here fetchallpoll mailin.tillsprovider.tld protocol POP3 user "tbrehm" there with password "iwonttellyou" is till here keep

At the beginning of the file we have some global options such as set daemon 300 (which means fetchmail should retrieve emails every 300 seconds) that control the operation of the program. The meanings of the above options are as follows:

  • set daemon: Set a background poll interval in seconds.
  • set syslog: Do error logging through syslog.
  • set postmaster: Give the name of the last-resort mail recipient (default: user running fetchmail, "postmaster" if run by the root user).
  • set no bouncemail: Direct error mail to the local postmaster (as per the "postmaster" global option above).

Then we have the server and the user options options. These go together into the lines beginning with poll; if there are options that are the same for each poll line, we can as well specify them before the poll lines in a section that begins with defaults: (such as timeout, antispam, and batchlimit in our example).

  • timeout: Server inactivity timeout in seconds (default 300).
  • antispam: Specify what SMTP returns are interpreted as spam-policy blocks.
  • batchlimit: Specify the maximum number of messages that will be shipped to an SMTP listener before the connection is deliberately torn down and rebuilt (defaults to 0, meaning no limit).

The poll lines are self-explanatory; as you see fetchmail retrieves emails from both of falko's external email accounts and puts them into one account.

You will notice that the poll lines have different endings (e.g. nofetchall (default), fetchall, keep, nokeep). The meanings are as follows:

  • nofetchall: Retrieve only new messages (default). If nothing else is specified (e.g. fetchall, keep), this means nofetchall.
  • fetchall: Fetch all messages whether seen or not.
  • keep: Don't delete seen messages from server.
  • nokeep: Delete seen messages from server.

To learn more about all available configuration settings, take a look at

man fetchmail

/etc/fetchmailrc must have 600 permissions and must be owned by the user fetchmail, so we do the following:

chmod 600 /etc/fetchmailrc
chown fetchmail /etc/fetchmailrc

Finally, we can start fetchmail:

/etc/init.d/fetchmail start

Fetchmail should now download emails and put them into falko's and till's mailboxes (using the MTA). It will repeat this every set daemon seconds.

3.2 Use Per-User Configuration Files And Run fetchmail Via Cron

Instead of using a global configuration file as shown in chapter 3.1, we can use per-user configuration files. These must have the name .fetchmailrc and must be located in the user's homedir.

We want to create such a file for the user falko now. Make sure you're logged in as falko, not root! Then we do this:

cd ~/
vi .fetchmailrc
set postmaster falkoset bouncemailpoll pop.someprovider.tld protocol POP3 user "[emailprotected]" there with password "secret"poll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" fetchall

The file looks very similar to the file /etc/fetchmailrc from chapter 3.1, however you will notice that I don't use the phrase is falko here anymore (as .fetchmailrc is in falko's homedir, fetchmail knows that the mails should be delivered to falko). Of course, you can still use is falko here, so the file could look like this as well:

set postmaster falkoset bouncemailpoll pop.someprovider.tld protocol POP3 user "[emailprotected]" there with password "secret" is falko herepoll mail.otherprovider.tld protocol POP3 user "ftimme" there with password "verysecurepassword" is falko here fetchall

To learn more about all available configuration settings, take a look at

man fetchmail

.fetchmailrc must have 600 permissions, so that only falko can read from/write to it:

chmod 600 ~/.fetchmailrc

That's it. Now falko can start the retrieval process by running

fetchmail

or

fetchmail -v

which shows what's going on.

Of course, falko doesn't want to start the retrieval manually every few minutes, so we create a cron job for him. Still as the user falko, we run

crontab -e

and create a cron job like this one (which would start fetchmail every five minutes):

*/5 * * * * /usr/bin/fetchmail &> /dev/null

4 Links

Retrieving Emails From Remote Servers With fetchmail on Debian (2024)

FAQs

Which provides a mechanism for retrieving emails from a remote server for a mail recipient? ›

POP3 (Post Office Protocol version 3)

POP3 is a protocol for retrieving emails from a remote mail server to the recipient's device. When you configure your email client with POP3 settings, it connects to the POP3 server to download your incoming emails.

How does Fetchmail work? ›

Fetchmail is a relatively simple program that downloads email from another server using the POP3 or IMAP protocol and delivers it to a mailbox on your system. It is most useful if you want to run your own mail server, but for some reason cannot have mail delivered directly.

Which email automation command we can directly get emails from the server without having interface like Gmail Outlook? ›

Yes, that is correct. Email automation commands allow you to interact with email servers directly without the need for an email client interface like Outlook. With email automation, you can send, receive, and manage emails programmatically using commands or scripts.

What protocol is used to retrieve email from remote server? ›

IMAP and POP3 are protocols used to retrieve email from a remote server to a local email client.

Which protocol is used to fetch e mail from a remote mailbox? ›

POP3 is the protocol used for fetching e-mail from a mailbox. (Option B) You may download email messages to your local computer and read them even if you aren't connected to the internet using POP3. POP3 is designed to delete mail from your server as soon as it is downloaded by the user.

Where does fetchmail store emails? ›

On the client side fetchmail probably stores mails in /var/spool/mail , unless you have set up your MTA. to deliver e.g. via procmail , in the later case your recipients ~/. procmailrc determines where things go. If you cannot seem to trace where things are going. Check your mail log (in fetched emails get bounced).

What port does fetchmail use for IMAP? ›

This is generally a different port than the port used by the base protocol. For IMAP, this is port 143 for the clear protocol and port 993 for the SSL secured protocol; for POP3, it is port 110 for the clear text and port 995 for the encrypted variant.

What is the keep option in fetchmail? ›

-k | --keep

Keep retrieved messages on the remote mail server. Normally, messages are deleted from the folder on the mail server after they have been retrieved. Specifying the keep option causes retrieved messages to remain in your folder on the mail server. This option does not work with ETRN or ODMR.

What is to receive emails from a remote server to a local email client? ›

Post Office Protocol 3, or POP3, is the most commonly used protocol for receiving email over the internet. This standard protocol, which most email servers and their clients support, is used to receive emails from a remote server and send to a local client.

How do I automate email responses? ›

How To Set Up an Automated Response in Gmail: 1. Log in to your Gmail, head to the upper right hand corner and click on the Settings cog. From there, you will click on “Advanced,” then, at the top look for “Advanced.” Click and scroll down until you find “Canned Responses,” which you will then want to enable.

How do I automate data extraction from email? ›

Step-by-step guide to data extraction from emails
  1. a. Sign up on the Docsumo platform. ...
  2. b. Upload and organize documents. ...
  3. c. Select data to extract from emails. ...
  4. d. Customize extraction settings. ...
  5. e. Review and export extracted data. ...
  6. f. Automate data extraction for large document sets. ...
  7. g. Integrate into the workflow. ...
  8. h.

What is POP3 and SMTP? ›

SMTP sends the email from the sender's device to the receiver's mailbox, and POP3 retrieves and organizes emails from the receiver's mail server to the receiver's computer. SMTP functions between the sender's and receiver's mail servers, and POP3 functions between the receiver and the receiver's mail server.

What is POP3 and IMAP? ›

POP3 downloads emails from a server to a single computer, making those emails only accessible on that specific computer. IMAP stores emails on a server and then syncs them across multiple devices.

What protocol was used to retrieve the email to the email server? ›

Email clients use Mail Access protocols like the POP/ IMAP protocols to retrieve/ sync emails from the server. Basically, mail access protocols are used to download or sync emails from the server. Email clients use transfer protocol - the SMTP protocol to transfer/ send emails through the server.

Which of these serves is used to retrieve email from a mail server? ›

POP3, or Post Office Protocol version 3, is a standard protocol used for retrieving email messages from a mail server. POP3 is commonly used by email clients to download messages from a server to a local computer.

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5981

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.