使用 Ubuntu 安裝郵件伺服器 (Mail Server):Postfix + Dovecot + Openwebmail
在 DNS Server 的文章提到二個域名 (domain name):
    www.example.com.tw    mail.example.com.tw這篇文章的內容與 mail.example.com.tw 有關;至於 www.example.com.tw 上一篇已經說明。
架設郵件
伺服器 (mail server) 需要 Postfix 和 Dovecot 二個套件。
    Postfix 負責 SMTP
    Dovecot 負責 POP3 和 IMAP
選擇 Dovecot 的理由是 Dovecot 同時支援 Maildir 和 mbox;而 Openwebmail 只支援 mbox。
Ubuntu Server 從 9.10 版開始提供一支套件 Dovecot-postfix,這支套件整合了 Postfix 與 Dovecot 的設定環境,使設定更簡單。
環境
我們希望郵件伺服器 Outlook Express 可以收發郵件,同時也提供網頁郵件服務。以下是我們的假設環境:
    郵件伺服器:Postfix
    POP3 伺服器:Dovecot-pop3d
    IMAP 伺服器:Dovecot-imapd
    信箱格式:mbox
    認證:使用 Ubuntu Server 的帳號及密碼
    網頁郵件服務:Openwebmail
Postfix
Ubuntu Server 常用的郵件伺服器是 Postfix。
安裝
安裝 Postfix 的指令如下:
    sudo apt-get install postfix
安裝過程中,畫面會要求你設定你的環境。
        Internet Site
        example.com.tw
啟動停止 Postfix 的指令如下:
    sudo /etc/init.d/postfix start #啟動 Postfix
    sudo /etc/init.d/postfix stop #停止 Postfix
    sudo /etc/init.d/postfix restart #重新啟動 Postfix
組態
後面我們要安裝 dovecot-postfix 套件,安裝 dovecot-postfix 會修改 Postfix 的 main.cf 檔案,這裡的 main.cf 檔案是被 dovecot-postfix 更改後的檔案。
Postfix 主要的組態設定檔是 /etc/postfix/main.cf。vi 編輯 main.cf 的指令如下:
    sudo vi /etc/postfix/main.cf
main.cf 的內容如下:
    第 9行 將 (Ubuntu) 拿掉
    第21行到第25行 #註記
    第34行 新增 mail.example.com.tw
    第41行 #註記,因為這裡使用 mbox
    第54行 #註記
    第56行到第61行 #註記
    # 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
    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-mail.pem
    #smtpd_tls_key_file = /etc/ssl/private/ssl-mail.key
    #smtpd_use_tls = yes
    #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 = dns.example.com.tw
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = mail.example.com.tw, example.com.tw, dns.example.com.tw, localhost.example.com.tw, localhost
    relayhost =
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all     
    #home_mailbox = Maildir/     
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/dovecot-auth
    smtpd_sasl_authenticated_header = yes
    smtpd_sasl_security_options = noanonymous
    smtpd_sasl_local_domain = $myhostname
    broken_sasl_auth_clients = yes     
    smtpd_recipient_restrictions = reject_unknown_sender_domain, reject_unknown_recipient_domain, reject_unauth_pipelining, permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
    smtpd_sender_restrictions = reject_unknown_sender_domain     
    #mailbox_command = /usr/lib/dovecot/deliver -c /etc/dovecot/conf.d/01-dovecot-postfix.conf -n -m "${EXTENSION}"     
    #smtp_use_tls = yes
    #smtpd_tls_received_header = yes
    #smtpd_tls_mandatory_protocols = SSLv3, TLSv1
    #smtpd_tls_mandatory_ciphers = medium
    #smtpd_tls_auth_only = yes
    #tls_random_source = dev:/dev/urandom
重新啟動 Postfix。
Dovecot
Ubuntu Server 常用的 POP3 Server, IMAP Server 是 dovecot。
安裝
安裝 Dovecot POP3, IMAP Server 的指令如下:
    sudo apt-get install dovecot-pop3d dovecot-imapd
安裝 dovecot-postfix 的指令如下:
    sudo apt-get install dovecot-postfix
啟動停止 Dovecot 的指令如下:
    sudo /etc/init.d/dovecot start #啟動 Dovecot
    sudo /etc/init.d/dovecot stop #停止 Dovecot
    sudo /etc/init.d/dovecot restart #重新啟動 Dovecot
組態
安裝 dovecot-postfix 後,/etc/dovecot 目錄的 dovecot.conf 被 # 註記了。新的組態檔的位置在:
    Ubuntu Server 9.10 在 /etc/dovecot 目錄裡,多了 dovecot-postfix.conf 檔案。
    Ubuntu server 10.04 LTS 在 /etc/dovecot/conf.d 目錄裡,多了 01-dovecot-postfix.conf 檔案。
編輯組態檔案的指令如下:
    sudo vi /etc/dovecot/dovecot-postfix.conf # Ubuntu Server 9.10
    # or
    sudo vi /etc/dovecot/conf.d/01-dovecot-postfix.conf # Ubuntu Server 10.04 LTS
dovecot-postfix.conf 或 01-dovecot-postfix.conf 檔案內容如下:
    第 2行 將 imaps pop3s managesieve 用 # 註記
    第 3行 disable_plaintext_auth = no,使用 Ubuntu 帳號、密碼
    第 4行 ssl = no,不使用 SSL 認證
    第 5行到第 7行 #註記,不使用 SSL 認證
    第 8行 mail_location = maildir:~/Maildir #註記,不使用 Maildir
    第 9行 新增 mail_location = mbox:~/mail:INBOX=/var/spool/mail/%u # (for mbox),使用 mbox
    # Some general options
    protocols = imap pop3 # imaps pop3s managesieve
    disable_plaintext_auth = no
    ssl = no
    #ssl_cert_file = /etc/ssl/certs/ssl-mail.pem
    #ssl_key_file = /etc/ssl/private/ssl-mail.key
    #ssl_cipher_list = ALL:!LOW:!SSLv2:ALL:!aNULL:!ADH:!eNULL:!EXP:RC4+RSA:+HIGH:+MEDIUM
    #mail_location = maildir:~/Maildir
    mail_location = mbox:~/mail:INBOX=/var/spool/mail/%u # (for mbox)
    auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@     
    # IMAP configuration
    protocol imap {
    mail_max_userip_connections = 10
    imap_client_workarounds = outlook-idle delay-newmail
    }     
    # POP3 configuration
    protocol pop3 {
    mail_max_userip_connections = 10
    pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
    }     
    # LDA configuration
    protocol lda {
    postmaster_address = postmaster
    mail_plugins = sieve
    quota_full_tempfail = yes
    deliver_log_format = msgid=%m: %$
    rejection_reason = Your message to <%t> was automatically rejected:%n%r
    }     
    # Plugins configuration
    plugin {
    sieve=~/.dovecot.sieve
    sieve_dir=~/sieve
    }
重新啟動 Dovecot。
測試
測試 SMTP port 25,輸入下列指令
    sudo telnet mail.example.com.tw 25
Postfix 回應顯示如下:
    Trying 192.168.0.10...
    Connected to dns.example.com.tw.
    Escape character is '^]'.
    220 dns.example.com.tw ESMTP Postfix
接著輸入下列指令
    ehlo mail.example.com.tw
Postfix 回應顯示如下:
第 5行 250-AUTH PLAIN LOGIN
第 6行 250-AUTH=PLAIN LOGIN
    250-dns.example.com.tw
    250-PIPELINING
    250-SIZE 10240000
    250-ETRN
    250-AUTH PLAIN LOGIN
    250-AUTH=PLAIN LOGIN
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250 DSN
完成收發信的認證設定。
Openwebmail
網頁郵件套件我們選擇 Openwebmail。
Ubuntu 沒有提供 Openwebmail 套件,B2D 中文 Linux 計劃1有提供。
修改 /etc/apt/sources.list
    sudo vi /etc/apt/sources.list
在 sources.list 檔案最後面加入以下這行
    deb ftp://debian.tnc.edu.tw/pub1 b2d/
Ubuntu Server 10.04 LTS 比較麻煩,套件庫裡的 libmd5-perl 有問題,無法直接安裝。手動安裝 libmd5-perl 的指令如下:
第 1行 下載 libmd5-perl_2.03-1_all.deb 套件。
第 2行 安裝 libmd5-perl_2.03-1_all.deb 套件。
    sudo wget http://mirrors.kernel.org/ubuntu/pool/universe/libm/libmd5-perl/libmd5-perl_2.03-1_all.deb
    sudo dpkg -i libmd5-perl_2.03-1_all.deb
安裝
安裝 Openwebmail 的指令如下:
    sudo apt-get update
    sudo apt-get install openwebmail
安裝後不要忘記將 /etc/apt/sources.list 檔案裡新增的 deb ftp://debian.tnc.edu.tw/pub1 b2d/ 註記起來。
    # deb ftp://debian.tnc.edu.tw/pub1 b2d/
更新 auth_unix.pl
B2D 版本已經是很舊的版本 (2006/10/20 以前的版本,但不包括 1020 版),因為當時尚未改成支援 blowfish,所以必須下載 auth_unix.pl,把它拷貝到 /usr/lib/cgi-bin/openwebmail/auth,覆蓋掉原有的 auth_unix.pl,否則將無法正常登入 Openwebmail。會出現下面的錯誤訊息。
    --------------------------------------------------------------------------------------------------------------
    Can't locate Crypt/Eksblowfish/Bcrypt.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl /usr/lib/cgi-bin/openwebmail) at /usr/lib/cgi-bin/openwebmail/auth/auth_unix.pl line 15.
    BEGIN failed--compilation aborted at /usr/lib/cgi-bin/openwebmail/auth/auth_unix.pl line 15.
    --------------------------------------------------------------------------------------------------------------
下載 auth_unix.pl 指令如下:
    sudo wget ftp://ftp3.tnc.edu.tw/b2d/openwebmail/auth_unix.pl
覆蓋掉原有的 auth_unix.pl 指令如下:
    sudo cp auth_unix.pl /usr/lib/cgi-bin/openwebmail/auth/
組態
登入 Openwebmail 前,先修改 openwebmail.conf 檔案,編輯 openwebmail.conf 指令如下:
    sudo vi /usr/lib/cgi-bin/openwebmail/etc/openwebmail.conf
openwebmail.conf 檔案內容如下:
    第13行 設定域名,這裡是 example.com.tw
    第15行 病毒檢查
    第16行 垃圾郵件檢查
    第17行 垃圾郵件學習
    第18行 網頁硬碟服務
    第19行 終端機功能
    第21行 郵件語系
    第22行 ICON 設定
    第25行到第29行 郵件簽名
    #
    # Open WebMail configuration file
    #
    # This file contains just the overrides from defaults/openwebmail.conf,
    # please make all changes to this file.
    #
    # This file sets options for all domains and all users.
    # To set options on per domain basis, please put them in sites.conf/domainname
    # To set options on per user basis, please put them in users.conf/username
    #
    # Please refer to openwebmail.conf.help for the description of each option
    #
    domainnames example.com.tw     
    enable_viruscheck yes
    enable_spamcheck yes
    enable_learnspam yes
    enable_webdisk yes
    enable_sshterm no     
    default_locale zh_TW.Big5
    default_iconset Cool3D.Chinese.Traditional
    default_fscharset none     
    <default_signature>
    --
    Open WebMail Project (http://openwebmail.org)     
    </default_signature>
記得在修改 openwebmail.conf 之後,別忘了執行
    sudo /usr/lib/cgi-bin/openwebmail/openwebmail-tool.pl --init
登入 Openwebmail
開啟瀏覽器,網址列輸入
    http://192.168.0.10/openwebmail
使用者第一次登入帳戶要設定基本資料。