使用postfix搭建邮箱系统

基础设置

  • 更新系统

    1
    
    yum update -y
    
  • 安装pop3/imap邮件检索代理

    1
    
    yum install -y  dovecot dovecot-devel dovecot-mysql
    
  • postfixadmin邮箱管理界面

    1
    
    wget http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.91/postfixadmin-2.91.tar.gz -O /usr/local/src/postfixadmin-2.91.tar.gz
    
  • 客户管理界面

    1
    
    wget http://downloads.sourceforge.net/project/roundcubemail/roundcubemail/0.9.5/roundcubemail-0.9.5.tar.gz -O /usr/local/src/roundcubemail-0.9.5.tar.gz
    
  • 修改主机名

    1
    2
    
    vim /etc/sysconfig/network
    HOSTNAME=mail.targetlux.cn
    
  • 添加用户

    1
    2
    
    groupadd pmail
    useradd pmail -g pmail -s /sbin/nologin -d /data/www/wwwroot
    

LAMP/LANP环境部署

yum部署

1
yum -y install httpd mysql mysql-devel mysql-server php php-pecl-Fileinfo php-mcrypt php-devel php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc pcre pcre-devel

编译安装

1

注意php要启用IMAP支持

postfixadmin安装配置

  • 安装包部署

    1
    2
    3
    
    tar zxvf postfixadmin-2.91.tar.gz -C /data/www/wwwroot/
    mv postfixadmin-2.91 postfixadmin
    chown pmail.pmail -R /data/www/wwwroot/postfixadmin
    
  • 修改配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    cd /data/www/wwwroot/postfixadmin
    vim config.inc.php  
    $CONF['configured'] = true;
    $CONF['database_type'] = 'mysql';
    $CONF['database_host'] = 'localhost';
    $CONF['database_user'] = 'postfix';
    $CONF['database_password'] = 'postfix';
    $CONF['database_name'] = 'postfix';
    $CONF['admin_email'] = 'postmaster@free.com';
    $CONF['encrypt'] = 'dovecot:CRAM-MD5';
    $CONF['dovecotpw'] = "/usr/bin/doveadm pw";
    $CONF['domain_path'] = 'YES';
    $CONF['domain_in_mailbox'] = 'NO';
    $CONF['aliases'] = '1000';
    $CONF['mailboxes'] = '1000';
    $CONF['maxquota'] = '1000';
    $CONF['fetchmail'] = 'NO';
    $CONF['quota'] = 'YES';
    $CONF['used_quotas'] = 'YES';
    $CONF['new_quota_table'] = 'YES';
    
  • 数据库配置

    1
    2
    3
    
    create database postfix;
    grant all on postfix.* to postfix@'localhost' identified by 'postfix';
    flush privileges;
    
  • 通过web界面安装
    登录http://localhost/postfixadmin/setup.ph
    按照提示一步一步进行,其中涉及setup密码,管理员邮箱密码等步骤

  • postfix邮件发送代理配置

    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
    
    vim /etc/postfix/main.cf
    ################# Main configure ####################
    myhostname = mail.targetlux.cn
    mydomain = targetlux.cn
    myorigin = $mydomain
    inet_interfaces = all
    mydestination = localhost
    mynetworks = 127.0.0.0/8
    disable_dns_lookups = yes
    default_destination_concurrency_limit = 20
    default_process_limit = 50
    net_interfaces = ipv4
    ################## Virtual mail configure #######################
    virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql_virtual_domains_maps.cf
    virtual_alias_maps = proxy:mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
    virtual_mailbox_base = /data/mail
    #注意uid 与 gid 与之前的建立的pmail账号是一致的,可以用# id pmail查询
    virtual_uid_maps = static:2001
    virtual_gid_maps = static:2001
    virtual_transport = dovecot
    dovecot_destination_recipient_limit = 1
    proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mail
    box_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $tr
    ansport_maps $mynetworks $virtual_mailbox_limit_maps
    ################ SASL SUPPORT FOR CLIENTS #######################
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_type = dovecot
    smtpd_sasl_security_options = noanonymous
    #以下的两个选项可以任选一个,不过必须要与dovecot 一致 
    #smtpd_sasl_path = /var/run/dovecot/auth-client
    smtpd_sasl_path = /var/run/dovecot/auth-client
    broken_sasl_auth_clients = yes
    smtpd_recipient_restrictions = permit_mynetworks,
                                   permit_sasl_authenticated,
                                   reject_invalid_hostname,
                                   reject_non_fqdn_hostname,
                                   reject_unknown_sender_domain,
                                   reject_non_fqdn_sender,
                                   reject_non_fqdn_recipient,
                                   reject_unknown_recipient_domain,
                                   reject_unauth_pipelining,
                                   reject_unauth_destination
    ################## Mail Quota configure #######################
    #mail transport max size:20M
    message_size_limit = 20480000
    # 虚拟邮件用户使用的最大容量,需在postfixadmin 中启用quota($CONF['used_quotas'] = 'YES';)才生效。
    virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf
    # Queue lifetime control
    maximal_queue_lifetime = 1d
    bounce_queue_lifetime = 1d
    
  • 检查配置文件是否有错误

    1
    
    postconf
    
  • 创建mysql脚本
    创建/etc/postfix/mysql_virtual_domains_maps.cf文件

    1
    2
    3
    4
    5
    6
    7
    8
    
    vim /etc/postfix/mysql_virtual_domains_maps.cf
    user = postfix
    password = postfix
    hosts = localhost
    dbname = postfix
    query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
    #optional query to use when relaying for backup MX
    #query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1'
    

    创建/etc/postfix/mysql_virtual_alias_maps.cf文件

    1
    2
    3
    4
    5
    6
    
    vim /etc/postfix/mysql_virtual_alias_maps.cf
    user = postfix
    password = postfix
    hosts = localhost
    dbname = postfix
    query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
    

    创建/etc/postfix/mysql_virtual_mailbox_maps.cf文件

    1
    2
    3
    4
    5
    6
    
    vim /etc/postfix/mysql_virtual_mailbox_maps.cf
    user = postfix
    password = postfix
    hosts = localhost
    dbname = postfix
    query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1'
    

    创建/etc/postfix/mysql_virtual_mailbox_limit_maps.cf文件

    1
    2
    3
    4
    5
    6
    
    vim /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
    user = postfix
    password = postfix
    hosts = localhost
    dbname = postfix
    query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'
    
  • SMTP认证设置
    查看postfix支持的认证

    1
    
    postconf -a
    

    修改/etc/postfix/main.cf配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    vim /etc/postfix/main.cf
    # SASL SUPPORT FOR CLIENTS
    # Turns on sasl authorization
    smtpd_sasl_auth_enable = yes
    # Use dovecot for authentication
    smtpd_sasl_type = dovecot
    # Path to UNIX socket for SASL
    smtpd_sasl_path = /var/run/dovecot/auth-client
    # Disable anonymous login. We don't want to run an open relay for spammers.
    smtpd_sasl_security_options = noanonymous
    # Adds support for email software that doesn't follow RFC 4954.
    # This includes most versions of Microsoft Outlook before 2007.
    broken_sasl_auth_clients = yes
    #
    smtpd_recipient_restrictions =  permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
    
  • 使用Dovecot做为投递

    1
    2
    3
    4
    
    vim /etc/postfix/main.cf
    # TRANSPORT MAP
    virtual_transport = dovecot
    dovecot_destination_recipient_limit = 1
    

    修改master.cf文件

    1
    2
    3
    4
    
    vim /etc/postfix/master.cf
    #在最后增加这两行,注意flags前面有两个空格,不然会报错
    dovecot   unix  -       n       n       -       -       pipe,
      flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
    

dovecot邮件检索代理安装配置

  • 修改dovecot配置文件
    修改/etc/dovecot/dovecot.conf#主配置文件

    1
    2
    3
    4
    5
    6
    7
    
    vim /etc/dovecot/dovecot.conf
    protocols = imap pop3
    listen = *
    dict {
      quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
    }
    !include conf.d/*.conf
    

    修改/etc/dovecot/conf.d/10-auth.conf

    1
    2
    3
    4
    
    vim /etc/dovecot/conf.d/10-auth.conf
    disable_plaintext_auth = no
    auth_mechanisms = plain login cram-md5
    !include auth-sql.conf.ext
    

    修改/etc/dovecot/conf.d/10-mail.conf

    1
    2
    3
    
    vim /etc/dovecot/conf.d/10-mail.conf
    mail_location = maildir:%hMaildir
    mbox_write_locks = fcntl
    

    修改/etc/dovecot/conf.d/10-master.conf

    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
    
    vim /etc/dovecot/conf.d/10-master.conf
    service imap-login {
      inet_listener imap {
      }
      inet_listener imaps {
      }
    }
    service pop3-login {
      inet_listener pop3 {
      }
      inet_listener pop3s {
      }
    }
    service lmtp {
      unix_listener lmtp {
      }
    }
    service imap {
    }
    service pop3 {
    }
    service auth {
      unix_listener auth-userdb {
        mode = 0600
        user = vmail
        group = vmail
      }
    #新加下面一段,为smtp做认证
      unix_listener auth-client {
        mode = 0600
        user = postfix
        group = postfix
      }
    }
    service auth-worker {
    }
    service dict {
      unix_listener dict {
        mode = 0600
        user = vmail
        group = vmail
      }
    }
    

    修改/etc/dovecot/conf.d/15-lda.conf

    1
    2
    3
    4
    5
    
    vim /etc/dovecot/conf.d/15-lda.conf
    protocol lda {
      mail_plugins = quota
      postmaster_address = postmaster@free.com #管理员邮箱
    }
    

    修改/etc/dovecot/conf.d/20-imap.conf

    1
    2
    3
    4
    
    vim /etc/dovecot/conf.d/20-imap.conf
    protocol imap {
            mail_plugins = quota imap_quota
    }
    

    修改/etc/dovecot/conf.d/20-pop3.conf

    1
    2
    3
    4
    5
    
    vim /etc/dovecot/conf.d/20-pop3.conf
    protocol pop3 {
      pop3_uidl_format = %08Xu%08Xv
      mail_plugins = quota
    }
    

    修改/etc/dovecot/conf.d/90-quota.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    vim /etc/dovecot/conf.d/90-quota.conf
    plugin {
      quota_rule = *:storage=1G
    }
    plugin {
    }
    plugin {
      quota = dict:User quota::proxy::quota
    }
    plugin {
    }
    

    增加/etc/dovecot/dovecot-sql.conf.ext

    1
    2
    3
    4
    5
    6
    
    vim /etc/dovecot/dovecot-sql.conf.ext
    driver = mysql
    connect = host=localhost dbname=postfix user=postfix password=postfix
    default_pass_scheme = CRAM-MD5
    user_query = SELECT CONCAT('/var/vmail/', maildir) AS home, 2000 AS uid, 2000 AS gid, CONCAT('*:bytes=', quota) as quota_rule FROM mailbox WHERE username = '%u' AND active='1'
    password_query = SELECT username AS user, password, CONCAT('/var/vmail/', maildir) AS userdb_home, 2000 AS userdb_uid, 2000 AS userdb_gid, CONCAT('*:bytes=', quota) as userdb_quota_rule FROM mailbox WHERE username = '%u' AND active='1'
    

    增加/etc/dovecot/dovecot-dict-sql.conf.ext

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    vim /etc/dovecot/dovecot-dict-sql.conf.ext
    connect = host=localhost dbname=postfix user=postfix password=postfix
    map {
      pattern = priv/quota/storage
      table = quota2
      username_field = username
      value_field = bytes
    }
    map {
      pattern = priv/quota/messages
      table = quota2
      username_field = username
      value_field = messages
    }
    
  • 重新启动服务

    1
    
    service postfix restart
    

测试smtp与pop3

  • 登录管理界面新建邮箱与帐户.

  • 测试25端口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    telnet localhost 25
    Trying ::1...
    Connected to localhost.
    Escape character is '^]'.
    220 mail.free.com ESMTP Postfix
    ehlo targetlux.cn
    250-mail.targetlux.cn
    250-PIPELINING
    250-SIZE 10240000
    250-VRFY
    250-ETRN
    250-AUTH PLAIN LOGIN CRAM-MD5
    250-AUTH=PLAIN LOGIN CRAM-MD5
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250 DSN
    quit
    221 2.0.0 Bye
    Connection closed by foreign host.
    
  • 测试110端口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    telnet localhost 110
    Trying ::1...
    telnet: connect to address ::1: Connection refused
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    +OK Dovecot ready.
    user liuyun@targetlux.cn
    +OK
    pass 123456
    +OK Logged in.
    quit
    +OK Logging out.
    Connection closed by foreign host.
    
  • outlook/foxmail客户端测试

    1
    

Roundcubemail安装配置

  • 解压安装包

    1
    2
    3
    4
    
    tar -xf roundcubemail-0.9.2.tar.gz -C /var/www/html/
    cd /var/www/html/
    mv roundcubemail-0.9.2 webmail
    chown pmail.pmail -R webmail
    
  • 配置Roundcubemail
    登录http://localhost/webmail/installer/
    按照提示一步一步进行,下载生成的main.inc.phpdb.inc.php文件,上传至webmail目录。

  • 创建Roundcubemail所用数据库

    1
    2
    3
    
    CREATE DATABASE roundcubemail;
    GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcubemail@localhost IDENTIFIED BY 'roundcubemail';
    FLUSH PRIVILEGES;
    
  • 返回安装界面继续安装

  • 测试Roundcubemail
    登录http://localhost/webmail/
    进行邮件收发等操作。