Ubuntu之邮件服务器(Postfix, Dovecot, MySql)

前提

  • 系统为 Ubuntu , 版本为14.04, 其他版本问题应该不大
  • 系统 hostnamezjc - 详见设置linux主机名 ----- 此步非必须
    • $ sudo su
    • # echo zjc > /etc/hostname
    • # exit
  • 系统IP地址为10.1.6.49
  • 一些地址映射 要加入/etc/hosts文件或者DNS
    • $ sudo apt-get install vim
    • $ sudo vim /etc/hosts #自然,也可以用其他文本编辑器
    •  #新添加的项
      10.1.6.49      zjc zjc.com vzjc.com  mail.vzjc.com
  • 修改完毕后最好重启一下机器
    • $ sudo shutdown -r now

安装依赖包

  1. $ sudo apt-get install openssl telnet libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql

安装mysql

  1. $ sudo apt-get install mysql-client mysql-server

首次安装后会提示输入mysql密码,输入确认并记住,以后还需要用到

设置MySQL数据库

其中的mail_adminpassword-for-mail_admin为生成的数据库mail的用户名和密码,可以修改成自己的,但是一旦修改,后文中出现的地方都要修改。

  1. $ mysql -u root -p


Enter password: input your password
mysql> CREATE DATABASE mail;
mysql> USE mail;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mail. TO ‘mail_admin‘@‘localhost‘ IDENTIFIED BY ‘password-for-mail_admin‘;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mail. TO ‘mail_admin‘@‘127.0.0.1‘ IDENTIFIED BY ‘password-for-mail_admin‘;
mysql> FLUSH PRIVILEGES;
mysql> CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );
mysql> CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );
mysql> CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
mysql> CREATE TABLE transport ( domain varchar(128) NOT NULL default ‘‘, transport varchar(128) NOT NULL default ‘‘, UNIQUE KEY domain (domain) );
mysql> quit

安装Postfit及相关

$ sudo apt-get install postfix postfix-mysql postfix-doc mailutils

如果postfit弹出设置界面,那么

General type of mail configuration 选择 Internet Site
System Mail Name 选择 vzjc.com

配置Postfit

虚拟domain配置文件

$ sudo vim /etc/postfix/mysql-virtual_domains.cf

  1. user = mail_admin
  2. password = password-for-mail_admin
  3. dbname = mail
  4. query = SELECT domain AS virtual FROM domains WHERE domain=‘%s‘
  5. hosts = 127.0.0.1

虚拟forwarding配置文件

$ sudo vim /etc/postfix/mysql-virtual_forwardings.cf

  1. user = mail_admin
  2. password = password-for-mail_admin
  3. dbname = mail
  4. query = SELECT destination FROM forwardings WHERE source=‘%s‘
  5. hosts = 127.0.0.1

虚拟mailbox配置文件

$ sudo vim /etc/postfix/mysql-virtual_mailboxes.cf

  1. user = mail_admin
  2. password = password-for-mail_admin
  3. dbname = mail
  4. query = SELECT CONCAT(SUBSTRING_INDEX(email,‘@‘,-1),‘/‘,SUBSTRING_INDEX(email,‘@‘,1),‘/‘) FROM users WHERE email=‘%s‘
  5. hosts = 127.0.0.1

虚拟email2email配置文件

$ sudo vim /etc/postfix/mysql-virtual_email2email.cf

  1. user = mail_admin
  2. password = password-for-mail_admin
  3. dbname = mail
  4. query = SELECT email FROM users WHERE email=‘%s‘
  5. hosts = 127.0.0.1

设置权限

  1. $ sudo chmod o= /etc/postfix/mysql-virtual_*.cf
  2. $ sudo chgrp postfix /etc/postfix/mysql-virtual_*.cf

建立用户

  1. $ sudo groupadd -g 5000 vmail
  2. $ sudo useradd -c ‘VMail‘ -g vmail -u 5000 vmail -d /home/vmail -m

使用postconf配置

此处有个需要注意的地方mynetworks应该包含本机的所有IP地址,例如我这里只有一个10.1.6.197,不然会导致发给自己的域的邮件接收不到。如果还是接收不到,那么重启机器看看

  1. postconf -e ‘myhostname = vzjc.com‘
  2. postconf -e ‘mydestination = vzjc.com, localhost, localhost.localdomain‘
  3. postconf -e ‘mynetworks = 127.0.0.0/8, 10.1.6.197‘
  4. postconf -e ‘message_size_limit = 30720000‘
  5. postconf -e ‘virtual_alias_domains =‘
  6. postconf -e ‘virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf‘
  7. postconf -e ‘virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf‘
  8. postconf -e ‘virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf‘
  9. postconf -e ‘virtual_mailbox_base = /home/vmail‘
  10. postconf -e ‘virtual_uid_maps = static:5000‘
  11. postconf -e ‘virtual_gid_maps = static:5000‘
  12. postconf -e ‘smtpd_sasl_auth_enable = yes‘
  13. postconf -e ‘broken_sasl_auth_clients = yes‘
  14. postconf -e ‘smtpd_sasl_authenticated_header = yes‘
  15. postconf -e ‘smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination‘
  16. postconf -e ‘smtpd_use_tls = yes‘
  17. postconf -e ‘smtpd_tls_cert_file = /etc/postfix/smtpd.cert‘
  18. postconf -e ‘smtpd_tls_key_file = /etc/postfix/smtpd.key‘
  19. postconf -e ‘proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps‘
  20. postconf -e ‘virtual_transport = dovecot‘
  21. postconf -e ‘local_transport = dovecot‘

为postfix创建证书

  1. $ cd /etc/postfix
  2. $ sudo openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 3650 -x509

更改证书权限

$ sudo chmod o= /etc/postfix/smtpd.key

配置saslauthd

  1. $ sudo mkdir -p /var/spool/postfix/var/run/saslauthd
  2. $ sudo cp -a /etc/default/saslauthd /etc/default/saslauthd.bak

$ sudo vim /etc/default/saslauthd

  1. START=yes
  2. DESC="SASL Authentication Daemon"
  3. NAME="saslauthd"
  4. MECHANISMS="pam"
  5. MECH_OPTIONS=""
  6. THREADS=5
  7. OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd -r"

$ sudo vim /etc/pam.d/smtp

  1. auth required pam_mysql.so user=mail_admin passwd=password-for-mail_admin host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1
  2. account sufficient pam_mysql.so user=mail_admin passwd=password-for-mail_admin host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1

$ sudo vim /etc/postfix/sasl/smtpd.conf

  1. pwcheck_method: saslauthd
  2. mech_list: plain login
  3. allow_plaintext: true
  4. auxprop_plugin: sql
  5. sql_engine: mysql
  6. sql_hostnames: 127.0.0.1
  7. sql_user: mail_admin
  8. sql_passwd: password-for-mail_admin
  9. sql_database: mail
  10. sql_select: select password from users where email = ‘%[email protected]%r‘

设置权限

  1. $ sudo chmod o= /etc/pam.d/smtp
  2. $ sudo chmod o= /etc/postfix/sasl/smtpd.conf

重启服务

  1. $ sudo adduser postfix sasl
  2. $ sudo service postfix restart
  3. $ sudo service saslauthd restart

安装dovecot及相关

安装dovecot

$ sudo apt-get install dovecot-common dovecot-imapd dovecot-pop3d dovecot-mysql

弹出的选项中选择生成根证书,并且生成的证书dovecot.pem应该在/etc/dovecot/下面,key在/etc/dovecot/private下面。请确认这一点
hostname 填写 vzjc.com

配置dovecot

$ sudo vim /etc/postfix/master.cf

取消submission配置的注释:

  1. submission inet n - - - - smtpd
  2. -o syslog_name=postfix/submission
  3. -o smtpd_tls_security_level=encrypt
  4. -o smtpd_sasl_auth_enable=yes
  5. -o smtpd_client_restrictions=$mua_client_restrictions
  6. -o milter_macro_daemon_name=ORIGINATING

文件末尾增加

dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -d ${recipient}

$ sudo cp -a /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak

清空文件/etc/dovecot/dovecot.conf,加入如下

$ sudo vim /etc/dovecot/dovecot.conf

: 1,$d

注意ssl_cert, ssl_key指向的证书和私钥必须存在

  1. log_timestamp = "%Y-%m-%d %H:%M:%S "
  2. mail_location = maildir:/home/vmail/%d/%n/Maildir
  3. namespace {
  4. inbox = yes
  5. location =
  6. prefix = INBOX.
  7. separator = .
  8. type = private
  9. }
  10. passdb {
  11. args = /etc/dovecot/dovecot-sql.conf
  12. driver = sql
  13. }
  14. protocols = imap pop3
  15. disable_plaintext_auth = no
  16. auth_mechanisms = plain login
  17. service auth {
  18. unix_listener /var/spool/postfix/private/auth {
  19. group = postfix
  20. mode = 0660
  21. user = postfix
  22. }
  23. unix_listener auth-master {
  24. mode = 0600
  25. user = vmail
  26. }
  27. user = root
  28. }
  29. ssl = required
  30. ssl_cert = </etc/dovecot/dovecot.pem
  31. ssl_key = </etc/dovecot/private/dovecot.pem
  32. userdb {
  33. args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
  34. driver = static
  35. }
  36. protocol lda {
  37. auth_socket_path = /var/run/dovecot/auth-master
  38. log_path = /home/vmail/dovecot-deliver.log
  39. postmaster_address = [email protected].com
  40. }
  41. protocol pop3 {
  42. pop3_uidl_format = %08Xu%08Xv
  43. }

dovecot-sql.conf

$ sudo cp -a /etc/dovecot/dovecot-sql.conf /etc/dovecot/dovecot-sql.conf.bak

$ sudo vim /etc/dovecot/dovecot-sql.conf

  1. driver = mysql
  2. connect = host=127.0.0.1 dbname=mail user=mail_admin password=password-for-mail_admin
  3. default_pass_scheme = CRYPT
  4. password_query = SELECT email as user, password FROM users WHERE email=‘%u‘;

$ sudo service dovecot restart

$ sudo chgrp vmail /etc/dovecot/dovecot.conf

$ sudo chmod g+r /etc/dovecot/dovecot.conf

设置mail aliases - 详见aliases配置文件 -- 这个是给sendmail程序用的吧,反正我是没看出来为啥要配置它

$ sudo vim /etc/aliases

postmaster: root
root:[email protected]

测试

添加邮件账户

$ mysql -u root -p
Enter password: input your password
mysql> USE mail;
mysql> INSERT INTO domains (domain) VALUES (‘vzjc.com‘);
mysql> INSERT INTO users (email, password) VALUES (‘[email protected]‘, ENCRYPT(‘password-for-test1‘));
mysql> INSERT INTO users (email, password) VALUES (‘[email protected]‘, ENCRYPT(‘password-for-test2‘));
mysql> INSERT INTO users (email, password) VALUES (‘[email protected]‘, ENCRYPT(‘111111‘));
mysql> quit

本来以为用户名可以用 test1 形式, 而不用 [email protected] 形式呢,结果如果不加@时候收发邮件是时灵时不灵的。。。不测试了,用户名直接加域名得了

### 重启机器

> 做完上面操作后如果用telnet vzjc.com 587,则显示不能连接,机器重启后就可以了 -- 原因待查

$ sudo shutdown -r now

### 用telnet测试

$ telnet vzjc.com 587

Trying 10.1.6.197...
Connected to vzjc.com.
Escape character is ‘^]‘.
220 vzjc.com ESMTP Postfix (Ubuntu)
ehlo vzjc.com
250-vzjc.com
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

$ telnet vzjc.com smtp

$ telnet vzjc.com 110

Trying 10.1.6.197...
Connected to vzjc.com.
Escape character is ‘^]‘.
+OK Dovecot (Ubuntu) ready.
quit
+OK Logging out
Connection closed by foreign host.

用客户端测试

可以用ubuntu的thunderbird或者其他系统的outlook,foxmail等客户端测试,如果是其他机器上,注意一系列端口号一定要被服务器开放

FAQ:

解决Postfit不能群发的问题 解决方案

参考:

https://www.centos.bz/2013/06/ubuntu-12-04-deploy-postfix-dovecot-mysql-cyrus-sasl/
http://nonfu.me/p/2753.html
http://blog.163.com/[email protected]/blog/static/32111129201001911212786/

来自为知笔记(Wiz)

时间: 2024-10-21 17:23:21

Ubuntu之邮件服务器(Postfix, Dovecot, MySql)的相关文章

邮件服务器postfix+dovecot+mysql

1.前期准备 1.1设置hostname CentOS7,可以通过hostnamectl set-hostname hostname命令设置hostname,并且修改hosts文件.这里域名是sijibao.info. hostnamectl set-hostname mail.sijibao.info 为什么要设置hostname呢?因为一般情况下,Postfix在与其他的SMTP服务器进行通信的时候,会使用hostname来表名自己的身份.主机名有两种形式,单名字与FQDN(Fully Qu

简单邮件服务器postfix+dovecot搭建

Postfix 是一种电子邮件服务器,是 MTA(邮件传输代理)软件,Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,POP / IMAP 是 MUA 从邮件服务器中读取邮件时使用的协议. linux下postfix+Dovecot的搭建 安装前准备 系统默认安装sendmail,首先对其进行关闭或卸载,防止端口占用. 1 .关闭服务 service sendmail stop chkconfig  sendmail off 2.使用yum卸载 yum remove sendm

Postfix+Dovecot+MySQL搭建邮件服务器

说到邮件服务器,网上有许多不同解决方案.Window操作系统下常见的邮件服务器有hMailServer.MailEnable.EVOMailServer.Apache James(纯Java跨平台)等. 而在Linux/Unix系统中常用的邮件服务器也有不少选择,最经典也是最常见的莫过于Postfix.这篇文章将会讲述如何在Ubuntu系统之下,通过Postfix.Dovecot.MySQL整合实现普通意义上「邮件服务器」的大部分功能. 开始之前 配置邮件服务器并不是一项简单的工作,其过程不仅需

邮件服务器Postfix的管理 重启php-fpm

Postfix邮件系统安装与配置:Postfix,Cyrus-IMAP,Cyrus-sasl,Dovecot和SPFhttp://www.freehao123.com/postfix-cyrus/CentOS下安装postfix发送邮件教程http://www.wpmee.com/lnmp1-0-centos-postfix/ CentOS下安装Postfix替换Sendmail发送邮件http://www.linuxidc.com/Linux/2014-10/107947.htm今天有网友提醒

Centos 下搭建电子邮箱服务器postfix+dovecot

因为同事在做一个电子邮件服务器,准备进行邮件推广什么的,后来遇到些问题,于是我们两个小白一起研究. 网上有很多关于电子邮件服务器搭建的.完整的文档.因此,一些具体的细节和配置,这里就不做详细的说明和解释了.主要讲的是我们两个小白花费大量时间,所查找到的,遇到的问题. 一.背景 postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件. Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,支持 Linux/Unix 系统. 经典搭配: pos

centos7搭建邮件服务器postfix

一.服务端: 域名解析: 角色 记录类型 主机记录 记录值 主域名 A @ 104.225.233.249 邮件域名 A mail 104.225.233.249 邮件服务器 MX @ mail.dollarphp.com 反垃圾配置 TXT @ v=spf1 a mx ~al 卸载sendmail: yum -y remove sendmail 安装软件: yum -y install postfix dovecot cyrus-sasl-* mailx 配置postfix:vim /etc/

10分钟教你搭建邮件服务器Postfix(RPM包安装)

简介 postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Venema想要为使用最广泛的sendmail提供替代品的一个尝试.在Internet世界中,大部分的电子邮件都是通过sendmail来投递的,大约有100万用户使用sendmail,每天投递上亿封邮件.这真是一个让人吃惊的数字.Postfix试图更快.更容易管理.更安全,同时还与sendmail保持足够的兼容性. 工作原理 客户端通过Outlook软件,向邮件

教你如何架设linux邮件服务器postfix

检查linux是否有安装postfix和dovecot 检查命令如下: Rpm  -qa |grep  postfix; Rpm –qa |grep dovecot; 如果没有显示任何数据,表明没有安装,需要网上下载软件安装:   修改电脑名: # vi /etc/sysconfig/network 修改HOSTNAME=linuxmail.cn   修改发送服务器配置文件: 1.  # vim /etc/postfix/main.cf 2.  113 inet_interfaces = all

Using Postfix, Dovecot and MySql as Mailserver under CentOS 7

Here is the original guid: Postfix+Dovecot+MySQL搭建邮件服务器 The reference above was written for Ubuntu, I found some changes must be done to apply the techniques with my CentOS scenario. Another helpful guid: Email with Postfix, Dovecot and MariaDB on Ce