Saturday, March 15, 2008

Setup a Mail Server in FreeBSD with Postfix and Dovecot

This tutorial show you how to configure a FreeBSD Mail Server using Postfix as a MTA (Mail Transfer Agent) and Dovecot as IMAP and POP3 Server.


Step 1. Compile and install Postfix from ports
--------------------------------------------------------------

cd /usr/ports/mail/postfix
make install


Step 2. Configure Postfix
----------------------------------

Create or modify file: /usr/local/etc/postfix/main.cf, to have the following content:

# -------------------- main.cf -----------------------------------------
queue_directory = /var/spool/postfix
command_directory = /usr/local/sbin
daemon_directory = /usr/local/libexec/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
mynetworks_style = host

debug_peer_level = 2

debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         xxgdb $daemon_directory/$process_name $process_id & sleep 5

sendmail_path = /usr/local/sbin/sendmail
mailq_path = /usr/local/bin/mailq
setgid_group = maildrop
html_directory = no
manpage_directory = /usr/local/man
sample_directory = /usr/local/etc/postfix
readme_directory = no

mydomain = example.com
myhostname = firewall.$mydomain
myorigin = $myhostname
mydestination = $myhostname, localhost,$mydomain

mynetworks = 10.0.0.5/32
mynetworks_style = subnet
# --------- end of file main.cf --------------------


Add the following line to /etc/rc.conf
postfix_enable="YES"


Edit /usr/local/etc/rc.d/postfix and change variable postfix_enable to "YES"

Notes:
Note1
--------
Don't forget to setup in main.cf the variable: mydestination = $myhostname, localhost,$mydomain
If you forget to do that, you will get rely denied error, and emails that comes to your domain name will be rejected.
If you have many domains you can use transport_maps variable:

mydestination = $mydomain, $myhostname, $transport_maps, localhost

Transport maps can be also used from within mysql (for large mail servers with postfix compiled and configured with virtual users and MySQL support. If you are interested there is a tutorial on that topic on our website.

Note2
--------
Make sure you either add your IP in main.cf with your mynetworks variable or setup smtp server with authentication and allow your username to use SMTP with authentication.

mynetworks = 10.0.0.2/32


Step 3. Start postfix
---------------------------
Run command:

/usr/local/etc/rc.d/postfix start

If you have a problem when starting postfix or trying to send email, postfix is not working and you get a message in /var/log/messages like this:

postfix/smtpd[6877]: fatal: open database /etc/aliases.db: No such file or directory

create an empty file /etc/aliases and then recreate aliases DB with command:

postmap /etc/aliases

At this point you should have a full working mail transport agent (MTA).
The next step is to configure a POP3/IMAP client so we (mail clients) could get emails from mail server.


Step 4. Install Dovecot POP3/IMAP Server
-------------------------------------------------------

cd /usr/ports/mail/dovecot
make install

This will start fetch/compilation/installation process of Dovecot from Ports.

Edit /etc/rc.conf and add:
dovecot_enable="YES"
to start dovecot at boot.

Edit /usr/local/etc/rc.d/dovecot, and change dovecot_enable variable to "YES".

Create a file /usr/local/etc/dovecot.conf with the following content:

# ----------------- /usr/local/etc/dovecot.conf ---------------------
protocols = imap pop3
disable_plaintext_auth = no
ssl_disable = yes

mail_location = mbox:~/mail/:INBOX=/var/mail/%u
mail_privileged_group = mail
verbose_proctitle = yes
first_valid_gid = 0

protocol imap {
  imap_client_workarounds = delay-newmail outlook-idle netscape-eoh tb-extra-mailbox-sep
}

protocol pop3 {
  pop3_uidl_format = %08Xu%08Xv
  pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
}

protocol lda {
  postmaster_address = postmaster@example.com This email address is being protected from spam bots, you need Javascript enabled to view it
  sendmail_path = /usr/sbin/sendmail
}

auth default {
  mechanisms = plain

  passdb pam {
  }

  userdb passwd {
  }

  user = root
}
dict {
  #quota = mysql:/usr/local/etc/dovecot-dict-quota.conf
}

plugin {
}
# ----------------- end /usr/local/etc/dovecot.conf ----------------

Save file and start dovecot:

/usr/local/etc/rc.d/dovecot start

You should now have a full working mail server. If you've configured an email client and you are not able to connect to your mail server check /var/log/messages and /var/log/maillog files for errors.

After configuring your mail server you should be able to:
- get emails from your mail server using an email client, configured to use an account from mail server using POP3 or IMAP protocols (for this tutorial it will work with FreeBSD system accounts, if you want to configure an email server to work with virtual users see other tutorials on our website).
- send emails using SMTP server from the mail server you've configured;
- the mail server will be able to receive emails sent from other mails server/Internet.
 

No comments: