# [SOLVED] Local mail server

## Caffeine.Junkie

Amazed by the possibility of configuring everything in gentoo I came up with the idea of making my system as useful as I can. So instead of fetching my emails by Kmail/Thunderbird I want to have access to those mails from wherever I am. I've set up sshd so I can connect to my machine, emerged mutt.

The thing I'm trying to do is:

1) fetch emails from a mail server

2) move the mails to the local IMAP machine

3) read the mails by Kmail or mutt

I know I need:

- fetchmail to get mails from mail server to local machine fetchmailrc (that's because I got two accounts on the same server)

- procmail to move mail previously collected by fetchmail to designated places

- imap server (courier?)

- mail client (Kmail/mutt)

I got some issues with putting it all up.

First of all my ~/.fetchmailrc:

```
poll mail.server.com protocol pop3 user "foo" password "pass" is "local_user" here

poll mail.server.com protocol pop3 user "foo1" password "pass is "local_user" here
```

Seems alright to me but I want procmail to move emails to different folders, e.g.:

mail for foo@mail.server.com to ~/mail/account1 and mail for foo1@mail.server.com to ~/mail/account2.

Someone mentioned I need to set up X-Envelope header to every mail account by I don't know how.

Next thing is ~/.procmailrc but since I don't know how to set up fetchmail I'll wait with procmail.Last edited by Caffeine.Junkie on Sat Feb 23, 2008 2:30 pm; edited 1 time in total

----------

## seiichiro0185

I use the following fetchmail + procmail setup to collect all my email accounts into one and serve it with dovecot via imap:

~/.fetchmailrc (make sure this file is only readable by the user and no one else, or fetchmail will complain)

```

poll pop.backslash.ch

protocol pop3

username mail1@backslash.ch

password password1

mimedecode

mda "/usr/bin/procmail -d %s"

poll pop.backslash.ch

protocol pop3

username mail2@backslash.ch

password password2

mimedecode

mda "/usr/bin/procmail -d %s"

```

~/.procmailrc

```

MAILDIR=$HOME/Maildir

DEFAULT=$HOME/Maildir/

LOGFILE=$HOME/.procmaillog

:0

* ^TO mail1@backslash.ch

.mail2/

:0

* ^TO mail2@backslash.ch

.mail2/

```

with this running fetchmail as the user who has the 2 configs in his home dir will fetch the mails into the maildir folder ~/Maildir putting mail from mail1@backslash.ch to ~/Maildir/.mail1 and mail from mail2@backslash.ch into ~/Maildir/.mail2 

for fetching the mail on a regular basis one can use a cron job  starting a script looking similar to this:

```

#!/bin/bash

su myuser -c fetchmail >/dev/null 2>/dev/null

```

for serving the mail via imap I use dovecot, mostly using the default config except editing the mail_location line in /etc/dovecot/dovecot.conf to look like this: 

```

mail_location = maildir:~/Maildir

```

it should also possible to use the mail in ~/Maildir with an maildir-capable Email-program, but I didn't test this (this setup runs on my headless server, so no need for this)

hope this helps, for me this setup runs fine for over 1,5 years now (.procmailrc has some more filters for sorting mailing lists etc..)

seiichiro0185

----------

## Caffeine.Junkie

Thx for the reply but I am wondering how to force fetchmail to add some header to mails from every account. The thing is that not every email has a properly filled To: header so I was thinking about "tagging" emails from account1 with e.g. ACCOUNT1 or ACCOUNT2 and so on.

Is it possible?

----------

## seiichiro0185

I haven't seen an option so far to let fetchmail tag the mails (but I'm no fetchmail guru, so there may be one). 

The Envelope-To /X-Envelope-To thing is something that has to do with the smtp-server. From my understanding it is like this:

To:                    not really relevant for delivering, can be anything, set from your mail-program

Envelope-To:      This one is used for Delivering the mail

X-Envelope-To:   This one may be set by your accounts server from the Envelope-To, but is not neccesary

the  ^TO (all uppercase) I use in my procmailrc is a special predefined procmail expression that will serach all possible To: Fields (including Envelope and X-Envelope) and also CC/BCC for the word/address put behind it. So my setup will also catch mail even with wrong To: as long as one of the envelopes is in place (which should be the case or the mail would not have arrived at your account...) [for more info see man 5 procmailrc section MISCELLANEOUS]

If for some reason there are mails that are not catched by this expression (which sould be really rare) you may put a "catch all" rule at the end of your procmailrc that looks like this (which is in general a good idea):

```

:0

.undefined/

```

I hope I didn't tell you something totally wrong here, if thats the case I would be glad if someone will tell me ^_^

----------

## Caffeine.Junkie

I think I figured it out. In my situation I have two email accounts on the same server belonging to the same local user e.g.:

local user: foo@mail.server.com and foo1@mail.server.com.

If you want to put all the emails from these accounts into separate folders you could you the "To:" header but that does not apply to mailing lists, spam etc. In this case using the "tracepolls" fetchmail option is the easiest way.

This is how ~/.fetchmailrc should look like:

```
poll mail.server.com protocol pop3 tracepolls user "foo" password "pass" is "local_user" here

poll mail.server.com protocol pop3 tracepolls user "foo1" password "pass" is "local_user" here
```

The tracepolls option adds something like this into Received header:

```
Received: from mail.server.com [mail.server.com IP]

   by LOCALHOSTNAME with POP3 (fetchmail-6.3.8 polling mail.server.com account foo)

   for <local_user@localhost> (single-drop); Wed, 20 Feb 2008 01:06:11 +0100 (CET)
```

Once you got that set you need to edit ~/.procmailrc to move the fetched emails into different folders. My .procmailrc looks like this:

```
MAILDIR=$HOME/.maildir

## for foo@mail.server.com

:0

* ^Received:.*account.*foo

$MAILDIR/account1

## for foo1@mail.server.com

:0

* ^Received:.*account.*foo1

$MAILDIR/account2
```

That would be it. I've spent lots of time searching for this. Hope this helps somebody.

----------

## seiichiro0185

Hey, thats an elegant solution I didn't know, nice!  :Smile: 

----------

## mimosinnet

 *Caffeine.Junkie wrote:*   

> I think I figured it out.

 

This is a great tip! I had the same issue some time ago and I did not come across the tracepolls option in fethcmail. In my case used the -a option in procmail to pass an argument:

 *procmail man page wrote:*   

> -a argument
> 
>             This will set $1 to be equal to argument.  Each succeeding -a argument will set the next number variable ($2,  $3,
> 
>             etc).  It can be used to pass meta information along to procmail.  This is typically done by passing along the $@x
> ...

 

This is my .fetchmailrc. It gets the mail from four different accounts and learns the spam form the dovecot server. Each account is given a certain argument (YAHOO/MENTA/GMAIL/UAB):

```
$ cat .fetchmailrc

set logfile /var/log/fetchmail.log

set no bouncemail

poll pop.correo.yahoo.es protocol POP3 service 110  

   user "user" password "password" fetchall 

   mda "/usr/bin/procmail -a YAHOO -d %T"

poll pop3.auna.com protocol POP3 service 110  

   user "user" password "password" fetchall 

   mda "/usr/bin/procmail -a MENTA -d %T"

poll pop.gmail.com protocol POP3 service 995 

   user "user" password "password" fetchall 

   ssl

   mda "/usr/bin/procmail -a GMAIL -d %T"

poll imap.uab.es protocol IMAP service 143 

   user "user" password "password" fetchall 

   mda "/usr/bin/procmail -a UAB -d %T"

poll localhost protocol IMAP

   user user password password

   fetchall no rewrite 

   folder IN.Spam_Fallat

   mda "/usr/bin/sa-learn --spam"
```

Procmail classifies the mail based in these arguments:

```
$ cat rc.comptes 

LLISTA="$1"

:0

* LLISTA ?? ^^MENTA^^

.0IN.Menta/

:0

* LLISTA ?? ^^YAHOO^^

.0IN.Yahoo/

:0

* LLISTA ?? ^^GMAIL^^

.0IN.Gmail/

:0

* LLISTA ?? ^^UAB^^

.0IN.UAB/
```

Cheers!

----------

