1 、安装
[root@mail ~]# postconf -a
cyrus
dovecot
若postfix已安装好,则会支持这两个功能,若不支持,表示postfix未安装好;CentOS 7开始默认系统自带postfix
# 安装postfix
yum -y install postfix
# 安装完成还需要替换系统自带的sendmail:
rpm -e sendmail 或 yum remove sendmail
# 修改MTA(默认邮件传输代理)
alternatives --config mta # 然后直接回车即可。
# 检查一下是不是已经设置成功了。
alternatives --display mta #第一行可以看到mta的状态。 例如:mat - status is manual.
[root@mail ~]# systemctl restart postfix
[root@mail ~]# systemctl enable postfix
[root@mail ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-01-15 10:00:45 CST; 5s ago
Process: 2097 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 2110 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 2108 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 2106 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 2182 (master)
CGroup: /system.slice/postfix.service
├─2182 /usr/libexec/postfix/master -w
├─2183 pickup -l -t unix -u
└─2184 qmgr -l -t unix -u
Jan 15 10:00:45 mail systemd[1]: Stopped Postfix Mail Transport Agent.
Jan 15 10:00:45 mail systemd[1]: Starting Postfix Mail Transport Agent...
Jan 15 10:00:45 mail postfix/master[2182]: daemon started -- version 2.10.1, configuration /etc/postfix
Jan 15 10:00:45 mail systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
2、 配置/etc/postfix/main.cf(注意每次配置完需重启服务才能生效,新手易犯问题)
vim /etc/postfix/main.cf
# 修改内容:
# 75行: 取消注释,设置hostname 邮局系统的主机名
myhostname = mail.abc.com
# 83行: 取消注释,设置域名 邮局系统的域名
mydomain = abc.com
# 99行: 取消注释 从本机发出邮件的域名名称
myorigin = $mydomain
# 116行: 默认是localhost,我们需要修改成all 监听的网卡接口
inet_interfaces = all
# 119行: 推荐ipv4,如果支持ipv6,则可以为all
inet_protocols = ipv4
# 164行: 添加 可接收邮件的主机名或域名
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 264行: 取消注释,指定内网和本地的IP地址范围 设置可转发哪些主机的邮件
mynetworks = 127.0.0.0/8
# 419行: 取消注释,邮件保存目录
home_mailbox = Maildir/
# 571行: 添加
smtpd_banner = $myhostname ESMTP
# 添加到最后
# 规定邮件最大尺寸为10M
message_size_limit = 10485760
# 规定收件箱最大容量为1G
mailbox_size_limit = 1073741824
# SMTP认证
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject
postfix check
[root@mail ~]# postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
home_mailbox = Maildir/
html_directory = no
inet_protocols = all
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
mydestination = $myhostname, $mydomain
mydomain = auto.com
myhostname = mail.auto.com
mynetworks = 127.0.0.1
myorigin = $mydomain
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
sample_directory = /usr/share/doc/postfix-2.10.1/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
unknown_local_recipient_reject_code = 550
3、验证测试
-------------增加邮件测试帐号--------------
[root@mail ~]# groupadd mailusers //创建组给测试帐号
[root@mail ~]# useradd -g mailusers -s /sbin/nologin user01 // 创建user01帐户只属于mailusers组并不能登录系统,用于测试邮件
[root@mail ~]# passwd user01 //创建user01密码,使用test1234
[root@mail ~]# useradd -g mailusers -s /sbin/nologin user02
[root@mail ~]# passwd user02
[root@mail ~]# telnet mail.auto.com 25 // 连接邮件服务器的25端口
Trying 127.0.0.1...
Connected to mail.auto.com.
Escape character is '^]'.
220 mail.auto.com ESMTP Postfix
helo localhost // 宣告客户端的主机地址
250 mail.auto.com
mail from:user01@auto.com
250 2.1.0 Ok
rcpt to:user02@auto.com
250 2.1.5 Ok
data // 表示要开始写邮件内容了
354 End data with <CR><LF>.<CR><LF>
Subject: my test mail 1. // 指定邮件标题
my first mial document...
.
250 2.0.0 Ok: queued as 2D37565968E
quit // 断开telnet连接
221 2.0.0 Bye
Connection closed by foreign host.
[root@mail ~]# ls /home/user02/Maildir/new/
1579055447.V803Ie5d1f4M449562.mail
[root@mail ~]# cat /home/user02/Maildir/new/1579055447.V803Ie5d1f4M449562.mail
Return-Path: <user01@auto.com>
X-Original-To: user02@auto.com
Delivered-To: user02@auto.com
Received: from localhost (localhost [127.0.0.1])
by mail.auto.com (Postfix) with SMTP id 2D37565968E
for <user02@auto.com>; Wed, 15 Jan 2020 10:30:01 +0800 (CST)
Subject: my test mail 1.
my first mial document...
Message-Id: <20200115023012.2D37565968E@mail.auto.com>
Date: Wed, 15 Jan 2020 10:30:01 +0800 (CST)
From: user01@auto.com
本文由来源 jackaroo2020,由 javajgs_com 整理编辑,其版权均为 jackaroo2020 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。