rss logo

How To : Synchronize Courier and Zimbra server with OfflineIMAP

Configuration

Intro

Offlineimap

We will see how to synchronize two mails servers with the help of offlineimap.

Offlineimap software will be installed on the courier debian server.

Network diagram

Installing

offlineimap

root@host:~# apt-get install offlineimap

Settings

.offlineimaprc

vmail@host:~$ touch ~/.offlineimaprc
vmail@host:~$ cat ~/.offlineimaprc
[general]
pythonfile=/var/vmail/.function.py
# List of accounts to be synced, separated by a comma. Ex : user01, user02
accounts = user01
#default directory
metadata = ~/.offlineimap
#ui = quiet

######################
#######USER01
######################
[Account user01]
# Minutes between syncs, if we use daemon, I personally prefer launch it via cron job
autorefresh = 5
# Identifier for the local repository; e.g. the maildir to be synced via IMAP.
localrepository = user01-courier
# Identifier for the remote repository; i.e. the actual IMAP, usually non-local.
remoterepository = user01-zimbra
# Status cache. Default is plain, which eventually becomes huge and slow.
status_backend = plain

# courier
[Repository user01-courier]
# Where should the mail be placed? 
#localfolders = ~/shebangthedolphins.net/user01/Maildir
folderfilter = lambda foldername: foldername not in ['Chats','Contacts','Emailed Contacts']
type = IMAP
remoteport = 993
remotehost = 192.168.0.84
remoteuser = user01@shebangthedolphins.net
remotepass = mypassword
ssl = yes
# to get certificate fingerprint : openssl x509 -in /etc/courier/imapd.pem -noout -sha1 -fingerprint or just check the offlineimap output ERROR: Server SSL fingerprint 
cert_fingerprint = bcb93b4b21e9e7a1e10fa8b2768e9046530e1fae
nametrans = courier_function
folderfilter = lambda foldername: not re.search('(Chats|Contacts|Emailed|Contacts)', foldername)

# zimbra
[Repository user01-zimbra]
type = IMAP
remoteport = 993
remotehost = 192.168.0.85
remoteuser = user01@shebangthedolphins.net
remotepass = mypassword
ssl = yes
# to get certificate fingerprint : openssl x509 -in /etc/courier/imapd.pem -noout -sha1 -fingerprint or just check the offlineimap output ERROR: Server SSL fingerprint 
cert_fingerprint = bcb93b4b21e9e7a1e10fa8b2768e9046530e1fae
nametrans = zimbra_function
#folderfilter = lambda foldername: foldername not in ['Chats','Contacts','Emailed Contacts']
folderfilter = lambda foldername: not re.search('(Chats|Contacts|Emailed|Contacts)', foldername)

.function.py

vmail@host:~$ cat /var/vmail/.function.py
import re
def zimbra_function(foldername):
        print("outside loop : " + foldername)
        """zimbra to courier"""
        match01 = re.search(r'^INBOX$',foldername)
        if match01:
                retval = foldername
                print("IF Z to C outside foldername : " + foldername)
        else:
                retval = re.sub("^", "INBOX.", foldername)
                print("ELSE Z to C loop retval : " + retval)
        match02 = re.search(r'^INBOX/',foldername)
        if match02:
                retval = re.sub("^", "INBOX.", foldername)

        retval = re.sub("/", ".", retval)
        print("retval Z to C : " + retval + "\n")
        return retval

def courier_function(foldername):
        print("outside loop : " + foldername)
        """courier to zimbra"""
        match = re.search(r'^INBOX$',foldername)
        if match:
                print("IF C to Z loop foldername : " + foldername)
                retval = foldername
        else:
                print("ELSE C to Z loop foldername : " + foldername)

        match02 = re.search(r'^INBOX\.',foldername)
        if match02:
                retval = re.sub('^INBOX\.', '', foldername)

        retval = re.sub("\.", "/", retval)
        print("retval C to Z : " + retval + "\n")
        return retval

script job

vmail@host:~$ cat /var/vmail/script.sh
#! /bin/bash
/usr/bin/offlineimap -q -c /var/vmail/.offlineimaprc -o 2>&1 | tee /tmp/sync.log

cron job

root@host:~# cat /etc/cron.d/offlineimap 
*/5 * * * * vmail /var/vmail/script.sh >dev/null 2>&1

Note

In his current Debian version, offlineimap doesn't seems to be able to create folders, so we have to create manually the same folders on each servers side.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address