Something I need to make absolutely clear from the start. This post is NOT about setting up an actual email server. This server sits on our local network, behind our router/firewall. It does not have direct access to the Internet. As such I was not focusing on security and will not vouch for it (this setup) being secure.
Ok, now that that is done, I need to give a little background. Here at BMCR we have satellite Internet. This means we have very limited bandwidth. In order to stay under our cap, I limit bandwidth per day by IP address. Once you hit your daily limit you can’t get to the Internet till the next day. That is all well and good, but if you hit your limit you can’t check your email. This was starting to be a problem.
Enter in these three programs postfix, dovecot, and offlineimap. With this setup, our server syncs itself with our Google Apps email accounts, and then we check and send our email on/from the local server. This way, even if you hit your bandwidth limit for the day, you can still do email (it also means that if for some strange reason all of Google’s servers die you have a local backup copy of all your emails). I installed this on Ubuntu server 10.04 (the 32bit version).
I offer no guarantees that any of this will work for you, nor do I guarantee that it wont completely destroy your system if you try it.
Sources:
Postfix install and configuration: Techienote.com
Postfix setup for multiple smtp servers: Cyberciti.biz (I don’t actually talk about this in this post, but I did set it up.)
Dovecot install and configuration: Webmonkey.com
Offlineimap: Offlineimap.org
Postfix
Install
sudo apt-get install postfix -y
The “-y” matters, but I’m not sure why. At the configuration screen select “No Configuration”
sudo cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf
Configuration
sudo vi /etc/postfix/main.cf
This is my main.cf:
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/sll/private/ssl-cert-snakeoil.key
smtpd_use_tls=no
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client
myhostname = repository.bmcr
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =
relayhost = [smtp.gmail.com]:submission
mynetworks = 192.168.1.0/24, 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
default_transport = smtp
relay_transport = smtp
inet_protocols = all
home_mailbox = email/
mailbox_command =
# SASL Settings
smtp_use_tls=yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map
smtp_generic_maps = hash:/etc/postfix/generic
Just a note, the “home_mailbox” parameter. On our server every ones email is in a folder called “email” in their home directory. You can call that folder whatever you want.
sudo echo "myfully.qualifieddomain.name" > /etc/mailname
sudo vi /etc/postfix/sasl_passwd
This opens the postfix password file. One username and password per line.
For example:
#Per-sender authentication
username@Repository.BMCR username@bmcrministries.org:password
#Default login
[smtp.gmail.com]:587 username@bmcrministries.org:password
I’ll try and explain the following commands, but the sources links will have better explanations (owing to the fact they know what they are doing).
This secures the password file (the passwords are stored in it plain text):
sudo chmod 400 /etc/postfix/sasl_passwd
Postfix keeps some kind of a database hash of the postfix password file and the mailname file. These update the databases after a change (these must be run whenever you modify there respective files):
sudo postmap /etc/postfix/sasl_passwd
sudo newaliases (this is for the /etc/mailname file… I think)
Setup the CA certificate. This is a self-signed certificate, so most email clients/browsers will throw up a warning. Remember this is running on our local network and isn’t accessible from outside.
sudo touch /etc/postfix/cacert.pem
sudo cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem >> /etc/postfix/cacert.pem
Now restart postfix.
sudo /etc/init.d/postfix restart
To test postfix:
sudo apt-get install mailutils This will let you send an email from the command line.
echo “We rolling, yo” | mail -s “Sent from Repository” someone@somewhere.com
If you’ve got everything configured properly this should send an email. The subject line will be the text following the “-s” and the body will be the text following “echo”. The address is that of the recipient.
A couple of notes:
I had ‘inet_interfaces’ explicitly stated in my main.cf. For some reason that didn’t work. I removed the line and it defaulted properly. Once that was done I started getting alias.db errors, which were fixed by ‘sudo newaliases’. That was some how tied to ‘mynetworks’. That should default to the local subnet and local host, however I had to explicitly put that in. Once these things were figured out I could send email using Thunderbird.
Dovecot (IMAP server)
Install
sudo apt-get install dovecot-imapd
Configuration
sudo nano /etc/dovecot/dovecot.conf
Find “protocols” and set it to “protocols = imap imaps”
Find “mail_location” and set that to “mail_location = maildir:/home/%u/email:LAYOUT=fs” (if you are using a different folder name make sure to use that instead)
Offlineimap
Install
For whatever reason I couldn’t get the Offlineimap in the Ubuntu repositories to work, so I installed from a different repository.
In order to add a ppa run this command:
sudo apt-get install python-software-properties
Then add the ppa:
sudo add-apt-repository ppa:pomeo/offlineimap
Then of course:
sudo apt-get update
sudo apt-get install offlineimap
Offlineimap is configured on a per user basis. Each user will have an .offlineimaprc file in their home directory. OfflineIMAP is very picky about the configuration file. If you are getting errors open the file up again and double check your settings. For example if you have a space before “ui = TTY.TTYUI” it will error out.
My offlineimaprc:
[general]
accounts = BMCR
ui = TTY.TTYUI
[Account BMCR]
#Profile for local mail
localrepository = localrep
#Profile for remote mail
remoterepository = remoterep
[Repository localrep]
type = Maildir
localfolders = ~/email/
[Repository remoterep]
type = IMAP
remotehost = imap.gmail.com
ssl = yes
remoteuser = someone@somewhere.com
remotepass = password
nametrans = lambda folder: re.sub('.*Drafts$', 'Drafts', re.sub('.*Sent Mail$', 'Sent_Mail', re.sub('.*Trash$', 'Trash', folder)))
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail','[Gmail]/Spam','[Gmail]/Starred','[Gmail]/Important']
maxconnections = 2
I would also chmod this to 400 when your done as it also contains a plain text password. The documentation at offlineimap.org should walk you through all the options I’ve used.
I hope this was useful. If something is un-clear, or simply wrong please let me know and I’ll try and fix it.