Skip to content

使用Postfix搭建本地邮件服务器

  • by

由于项目需要,需要搭建一个本地邮件服务器进行测试,本文是对搭建过程的记录,搭建过程参考了1

搭建使用两台虚机,分配私网IP地址并在同一个网段内:

  • CentOS 7:安装Postfix和Dovecot作为邮件服务器
  • Ubuntu:安装Thunderbird作为邮件客户端

设置邮件服务器

安装设置Postfix

使用CentOS7,系统已经默认安装了Postfix,只需要进行设置。编辑/etc/postfix/main.cf文件进行设置:

# INTERNET HOST AND DOMAIN NAMES
# 
# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). myhostname is used as a default value for many
# other configuration parameters.
# 设置本地主机名
myhostname = localhost.localdomain.com

# 设置域名,由于只是本地邮件服务器,不需要真实域名,设置一个本地域名即可
mydomain = localdomain.com

# SENDING MAIL
# 
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to appendmyhostname,
# which is fine for small sites.  
# 设置myorigin
myorigin = mydomain

# RECEIVING MAIL

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
# 设置监听所有网络接口来的邮件
inet_interfaces = all

# The mydestination parameter specifies the list of domains that this
# machine considers itself the final destination for.
# 设置mydestination包括之前设置的本地域名
mydestination =myhostname, localhost.mydomain, localhost,mydomain

# REJECTING MAIL FOR UNKNOWN LOCAL USERS
#
# If this parameter is defined, then the SMTP server will reject
# mail for unknown local users. This parameter is defined by default.
# 关闭拒绝未知的本地用户
local_recipient_maps =

# DELIVERY TO MAILBOX
# 设置邮件保存路径,这样邮件会被保存在user home路径的Maildir/下
home_mailbox = Maildir/

重启postfix服务,完成postfix的设置。

$ sudo systemctl restart postfix.service

新增两个用户作为邮件用户进行测试:

$ sudo useradd zhangsan
$ sudo passwd zhangsan 

$ sudo useradd lisi
$ sudo passwd lisi

使用telnet测试postfix发邮件:

$ telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 localhost.localdomain.com ESMTP Postfix
HELO localhost
250 localhost.localdomain.com
mail from:lisi@localdomain.com
250 2.1.0 Ok
rcpt to:zhangsan@localdomain.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Hello, this is a test mail.
.
250 2.0.0 Ok: queued as A254EA335B
quit
221 2.0.0 Bye
Connection closed by foreign host.

查看zhangsan用户的Maildir目录,可以看到刚才发的邮件:

$ cat /home/zhangsan/Maildir/new/1615084956.V801I300015M607162.localhost.localdomain 
Return-Path: <lisi@localdomain.com>
X-Original-To: zhangsan@localdomain.com
Delivered-To: zhangsan@localdomain.com
Received: from localhost (localhost [IPv6:::1])
    by localhost.localdomain.com (Postfix) with SMTP id A254EA335B
    for <zhangsan@localdomain.com>; Sat,  6 Mar 2021 21:41:59 -0500 (EST)
Message-Id: <20210307024212.A254EA335B@localhost.localdomain.com>
Date: Sat,  6 Mar 2021 21:41:59 -0500 (EST)
From: lisi@localdomain.com

Hello, this is a test mail.

安装设置Dovecot

Dovecot使用pop3和imap协议收取邮件,配置文件在/etc/dovecot/目录下。

设置dovecot.conf

# Protocols we want to be serving.
protocols = imap pop3 lmtp

# Space separated list of trusted network ranges. Connections from these
# IPs are allowed to override their IP addresses and ports (for logging and
# for authentication checks). disable_plaintext_auth is also ignored for
# these networks. Typically you'd specify your IMAP proxy servers here.
# 设置为私网地址
login_trusted_networks = 192.168.0.0/24

设置conf.d/10-mail.conf

# 设置邮件存储目录,就是在postfix中设置的路径
mail_location = maildir:~/Maildir

设置conf.d/10-auth.conf

# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). 
# 允许明文auth
disable_plaintext_auth = no

设置conf.d/10-ssl.conf

# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
# 关闭ssl
ssl = no

运行dovecot:

$ sudo systemctl enable dovecot.service 
$ sudo systemctl start dovecot.service

设置Thunderbird客户端

首先设置/etc/hosts文件,绑定邮件服务器IP和之前在Postfix设置的localdomain.com域名。

192.168.153.19  localdomain.com

安装Thunderbird并设置邮件用户:

邮件用户设置成功并可相互收发邮件:

至此搭建完毕,并可以进一步设置例如使用加密收发邮件等。

Leave a Reply

Your email address will not be published. Required fields are marked *