Building Your Own Mail Server
Requirments:
- Postfix : Mail Server
- Dovecot : IMAP/POP3 Server
- AfterLogic WebMail Lite or Roundcube : WebMail Interface
- PostfixAdmin : Mail Account/Domain Management (myself)
Installation
1 |
apt-get install postfix postfix-mysql dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql postfixadmin |
Postfix Settings
/etc/postfix/main.cf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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/ssl/private/ssl-cert-snakeoil.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 smtpd_tls_cert_file=/etc/dovecot/dovecot.pem smtpd_tls_key_file=/etc/dovecot/private/dovecot.pem smtpd_use_tls=yes # comment this line if you want to use SMTP AUTH LOGIN smtpd_tls_auth_only = yes #Enabling SMTP for authenticated users, and handing off authentication to Dovecot smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes broken_sasl_auth_clients = yes smtpd_sasl_security_options = noanonymous smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. myhostname = host.example.com alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname #mydestination = example.com, hostname.example.com, localhost.example.com, localhost mydestination = 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 #Handing off local delivery to Dovecot's LMTP, and telling it where to store mail virtual_transport = lmtp:unix:private/dovecot-lmtp #Virtual domains, users, and aliases virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf |
/etc/postfix/mysql-virtual-mailbox-domains.cf:
1 2 3 4 5 |
user = postfixadmin password = #dbpassowrd hosts = 127.0.0.1 dbname = postfixadmin query = SELECT 1 FROM domain WHERE domain='%s' |
/etc/postfix/mysql-virtual-mailbox-maps.cf:
1 2 3 4 5 |
user = postfixadmin password = #dbpassowrd hosts = 127.0.0.1 dbname = postfixadmin query = SELECT 1 FROM mailbox WHERE username='%s' |
/etc/postfix/mysql-virtual-alias-maps.cf:
1 2 3 4 5 |
user = postfixadmin password = #dbpassowrd hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias WHERE address='%s' |
測試:
1 2 3 4 |
service postfix restart postmap -q {domain} mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf postmap -q {email} mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf postmap -q {alias} mysql:/etc/postfix/mysql-virtual-alias-maps.cf |
Dovecot Settings
In this section, you’ll configure Dovecot to:
- Set the IMAP, POP3, and LMTP protocols
- Define the mail location
- Use MySQL for username/password lookups for authentication
- Configure needed sockets for authentication and LMTP
- Require SSL encryption
You’ll modify a total of 7 Dovecot configuration files. Here’s the list:
/etc/dovecot/dovecot.conf
: Dovecot’s main configuration file/etc/dovecot/conf.d/10-mail.conf
: Deals with the server’s file system/etc/dovecot/conf.d/10-auth.conf
: Defines how user authentication is handled/etc/dovecot/conf.d/auth-sql.conf.ext
: New authentication file for SQL-type authentication/etc/dovecot/dovecot-sql.conf.ext
: An included authentication file with the MySQL connection parameters/etc/dovecot/conf.d/10-master.conf
: Where sockets are configured/etc/dovecot/conf.d/10-ssl.conf
: Where SSL-related parameters are specified
/etc/dovecot/dovecot-sql.conf.ext:
1 2 3 |
connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password={dbpassword} ... password_query = SELECT username as user, password FROM mailbox WHERE username='%u'; |
如果要開啟 pop3 110 port, /etc/dovecot/conf.d/10-master.conf:
1 2 3 4 5 6 7 8 9 |
service pop3-login { inet_listener pop3 { port = 110 } inet_listener pop3s { port = 995 #ssl = yes } } |
PostfixAdmin permission issue (myself)
1 |
usermod -G dovecot www-data |
/etc/dovecot/conf.d/10-master.conf:
1 2 3 4 5 6 7 8 9 |
service auth { ... unix_listener auth-client { mode = 0666 user = vmail group = www-data } ... } |