Install Dovecot

#!/bin/bash

# ensure that this script is run by root
if [ $(id -u) -ne 0 ]; then
  sudo $0
  exit
fi

# install dovecot
apt-get install -y dovecot-imapd

# enable secure imap only (Port 993)
sed -i 's|#port = 143|port = 0|g' /etc/dovecot/conf.d/10-master.conf

# configure mailbox path
sed -i 's|mbox:~/mail:INBOX=/var/mail/%u|maildir:~/Maildir|g' /etc/dovecot/conf.d/10-mail.conf

# configure ssl
sed -i 's|^#ssl = yes|ssl = yes|g' /etc/dovecot/conf.d/10-ssl.conf
sed -i 's|^#disable_plaintext_auth = yes|disable_plaintext_auth = yes|g' /etc/dovecot/conf.d/10-auth.conf

# restart
/etc/init.d/dovecot restart
>
/etc/dovecot/conf.d/10-master.conf
  inet_listener imaps {
    port = 993
    ssl = yes
  }
 
openssl req -new -x509 -nodes -out /etc/dovecot/dovecot.pem -keyout /etc/dovecot/private/dovecot.pem -days 1095 -newkey rsa:4096

Links
http://wiki2.dovecot.org/SSL/DovecotConfiguration
http://wiki2.dovecot.org/SSL/CertificateCreation
http://ebalaskas.gr/wiki/dovecot/DovecotSSL
http://wiki.ubuntuusers.de/Dovecot
https://help.ubuntu.com/11.04/serverguide/C/dovecot-server.html
http://wiki.dovecot.org/MainConfig
http://www.howtoforge.com/dovecot_mail_server_sieve_virtual_users