Retrieve mails of only one specific account with fetchmail

The situation

I have the following ~/.fetchmailrc

poll pop.gmail.com       protocol pop3 user "foo@example.com"      password "***"            ssl
mda "/usr/bin/procmail -d default -a foo@example-com"

poll pop.gmail.com       protocol pop3 user "bar@example.com"      password "***"            ssl
mda "/usr/bin/procmail -d default -a bar@example-com"

So, I retrieve all the mails with fetchmail -v -m '/usr/bin/procmail -d %T'.

The problem

But the problem is all the accounts are retrieved.

The question

Is it possible from fetchmail command to ask for retrieving just one specific addresses’ mails?

Asked By: fauve

||

You can give each account a different name and use the via option

eg

poll foo via pop.gmail.com ...

poll bar via pop.gmail.com ...

Now you can specify each one on the command line; fetchmail foo will only fetch mail for that one account.

I use this type of structure for Outlook:

poll outlook via imap-mail.outlook.com port 993
  proto IMAP
  user my_user_1@outlook.com is foo here
  fetchall
  password *****
  ssl

poll outlook2 via imap-mail.outlook.com port 993
  proto IMAP
  user my_user_2@outlook.com is bar here
  fetchall
  password *****
  ssl

This can be implemented as follows in zsh.

Alias and function to retrieve mail

alias retrieveallmails="fetchmail -v -m '/usr/bin/procmail -d %T'" # For all the accounts

function retrievemail()
{
    # For a specific account
    fetchmail $1 -v -m '/usr/bin/procmail -d %T'
}

Related completion

_retrievemail_complete() {
    local accounts
    accounts=($(fetchmail --configdump | sed -n 's/.*"pollname":"(.*)",/1/p'))


    _arguments 
        '1: :->account' 
        '*:: :->arguments'

    case $state in
        (account)
            _describe 'accounts' accounts
            ;;
        (arguments)
            _files
            ;;
    esac
}

compdef _retrievemail_complete retrievemail
Answered By: Stephen Harris
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.