Central webmail server

From Nekochan
Jump to: navigation, search

Central mailserver with web access on IRIX

This article will guide you on your way to setup a central mailserver that fetches your mail from various accounts, filters it, sorts it, makes it available over IMAP and a sexy webmail interface. What you need:

1. neko_fetchmail
2. neko_procmail
3. neko_perl_mail_spamassassin
4. neko_cyrus_imapd
5. neko_cyrus_sasl
6. neko_apache2_prefork
7. neko_php5
8. neko_php5_openssl
9. neko_php5_mysql4
10. neko_mysql4
11. ... and all dependencies of the above that you might not have installed yet.

Step one: Cyrus

Open /usr/nekoware/etc/cyrus.conf and comment out the following line:

pop3        cmd="pop3d" listen="pop3" prefork=0 

You don't want to run pop3 anyway - that'd defeat the whole purpose of imap. Next, edit /usr/nekoware/etc/imapd.conf. It has to contain this:

configdirectory: /usr/nekoware/var/imap
partition-default: /usr/nekoware/var/spool/imap
admins: cyrus root
srvtab: /usr/nekoware/var/imap/srvtab
allowanonymouslogin: no
sasl_passwd_check: saslauthd
allowplaintext: yes
sasl_mech_list: PLAIN
altnamespace: 1

This will give you a simple authentication mechanism. Next, you'll want to start the saslauthd daemon.

# saslauthd -a shadow

Contrary to what you might think, this does not just allow regular /etc/shadow (unix) users. First, add a sasl password for root:

# saslpasswd2 root

To add more users to imap, you just add them to the sasl database:

# saslpasswd2 newuser

Then you can create their mailboxes.

$ cyradm --user newuser localhost
localhost> cm Sent
localhost> cm Drafts
localhost> cm Spam

... and so on. You can check what mailboxes you have as a user:

$ cyradm --user newuser localhost
localhost> lm
INBOX (\HasChildren)             Sent (\HasNoChildren)
Drafts (\HasNoChildren)          Spam (\HasNoChildren)

You'll need to setup the mailboxes in a way that'll allow external apps to post mail in the folders:

localhost> setacl Sent anyone p

... and so on for every map. Start the mailserver:

# chkconfig neko_cyrus_imapd on

You might want to use a rather dirty hack: edit /etc/init.d/neko_cyrus_imapd and add the saslauthd startup stuff to it. That's about it for this part: you already have a working imap server setup now. You can import the account in your client.

Step two: Webmail

IMAP is fun, but you haven't always got a client handy to check your mail. Webmail is the way to go; we'll configure this on top of the running imap server. First, edit /etc/init.d/neko_apache2 and enable SSL and PHP5 by uncommenting the following lines:

MODULES="-DSSL $MODULES" 
(...)
MODULES="-DPHP5 $MODULES" 

After that, start neko_mysql4:

# chkconfig neko_mysql4 on

Now, visit the RoundCube website and download the latest tarball. Unpack it in /usr/nekoware/apache2/htdocs and read the INSTALL file. It'll give you a few commands to run in mysql:

# /usr/nekoware/mysql4/bin/mysql
mysql> CREATE DATABASE roundcubemail DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
mysql> GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcube@localhost IDENTIFIED BY 'mypassword' ;
mysql> quit

# /usr/nekoware/mysql4/bin/mysql roundcubemail < SQL/mysql5.initial.sql

There. You can verify this worked by trying

# /usr/nekoware/mysql4/bin/mysql -u roundcube -p

Edit the config/db.inc.php.dist and make sure your DB line looks like this:

$rcmail_config['db_dsnw'] = 'mysql://roundcube:mypassword@localhost/roundcubemail'; 

Then save it to db.inc.php. Edit the config/main.inc.php.dist; the following lines are interesting:

# We don't do caching since imap is on localhost 
$rcmail_config['enable_caching'] = FALSE;  
# We're not going to manually create all users again
$rcmail_config['auto_create_user'] = TRUE;    
# Outgoing mail server
$rcmail_config['smtp_server'] = 'smtp.your.isp'; 
# ... etcetera - make it match your folder structure
$rcmail_config['drafts_mbox'] = 'Drafts';       
# Everyone needs these folders. They'll be created if they don't exist. 
$rcmail_config['default_imap_folders'] = array('INBOX', 'Drafts', 'Sent', 'Spam', 'Trash');
# Spellchecking is for retards.
$rcmail_config['enable_spellcheck'] = FALSE;

Good, now start apache.

# chkconfig neko_apache2 on

Your webmail should be available on https://yourserver now.

Step three: fetching and filtering

All that remains to be done is fetching your mail and actually storing it on that nifty server you have. First you'll want to start spamassassin.

# chkconfig neko_spamd on

Next, create a ~/.fetchmailrc. Here's an example for one POP3 and one IMAP account:

poll pop3.crappy-isp.com proto pop3 user "foo" pass "bar" fetchall mda "/usr/nekoware/bin/procmail"
poll imap.sexy-isp.com proto imap user "your" pass "momma" fetchall mda "/usr/nekoware/bin/procmail"

When fetchmail is invoked, it'll download all mail from the accounts and pass it on to procmail. This brings us to the next step. Open /usr/nekoware/etc/procmailrc and insert the following:

:0fw: spamassassin.lock
* < 128000
| /usr/nekoware/bin/spamc

This pipes every mail smaller than 128KB through spamassassin, and locks it so only one spamassassin instance works at the same time. This isn't necessary but can reduce server load. Next, create a ~/.procmail/ and ~/.procmailrc, the latter having your own personal mail filters.

PROCMAILDIR=$HOME/.procmail
LOG=$PROCMAILDIR/pmlog
:0
* ^X-Spam-Status: Yes
| /usr/nekoware/cyrus/bin/deliver -m spam newuser
:0
* ^TO_.*anothername
| /usr/nekoware/cyrus/bin/deliver -m anothermap newuser
:0
| /usr/nekoware/cyrus/bin/deliver newuser

And that's it. All you have to do now is add an entry in your crontab that runs fetchmail at your desired interval. IRIX + cron + fetchmail + procmail + cyrus_imapd + roundcube = a killer combo!