https://linuxhint.com/bash_script_send_email/
https://www.linuxtechi.com/install-configure-postfix-mailserver-centos-8/
https://support.itrsgroup.com/hc/en-us/articles/360020253193-Sending-outgoing-notifications-via-SMTP-relay
https://www.mail-tester.com/
https://www.linuxtechi.com/log-email-subject-maillog/
https://community.spiceworks.com/linux?source=navbar-subnav
By default postfix only capture ‘From’ and ‘To’ details in the log file (/var/log/maillog).
https://www.linuxtechi.com/install-configure-postfix-mailserver-centos-8/
https://support.itrsgroup.com/hc/en-us/articles/360020253193-Sending-outgoing-notifications-via-SMTP-relay
https://www.mail-tester.com/
https://www.linuxtechi.com/log-email-subject-maillog/
https://community.spiceworks.com/linux?source=navbar-subnav
By default postfix only capture ‘From’ and ‘To’ details in the log file (/var/log/maillog).
如何在CentOS 上安装和配置Postfix邮件服务器
Postfix是免费的开源MTA(邮件传输代理),用于在Linux系统上路由或传递电子邮件
设置:
操作系统:CentOS 7服务器
IP地址:192.168.43.128
主机名:server1.admin.info(确保域名指向服务器的IP)
在继续进行操作之前,还请确保不存在其他MTA(例如Sendmail),因为这将导致与Postfix配置冲突。例如,要删除Sendmail,请运行以下命令
# yum remove sendmail
设置主机名并更新/etc/hosts文件
使用下面的hostnamectl命令在系统上设置主机名
# hostnamectl set-hostname server1.admin.info
# exec bash
此外,您需要在/etc/hosts文件中添加系统的主机名和IP条目
# vi /etc/hosts
192.168.43.128 server1.admin.info
保存并退出文件
安装Postfix邮件服务器
在验证系统上没有其他MTA正在运行之后,通过执行以下命令安装Postfix
# yum install postfix
启动并启用Postfix服务
成功安装Postfix后,通过运行以下命令启动并启用Postfix服务
# systemctl start postfix
# systemctl enable postfix
要检查Postfix状态,请运行以下systemctl命令
# systemctl status postfix
我们已经验证了Postfix已启动并正在运行。接下来,我们将配置Postfix以将电子邮件本地发送到我们的服务器。
安装mailx电子邮件客户端
在配置Postfix服务器之前,我们需要安装mailx功能,要安装mailx,请运行以下命令
# yum install mailx
配置Postfix邮件服务器
Postfix的配置文件位于/etc/postfix/main.cf中。我们需要对配置文件进行一些更改,因此请使用您喜欢的文本编辑器将其打开。
# sudo vi /etc/postfix/main.cf
更改以下几行
myhostname = server1.admin.info
mydomain = admin.info
myorigin = $mydomain
## Uncomment comment ##
inet_interfaces = all
## Comment ##
#inet_interfaces = localhost
## Change to all ##
inet_protocols = all
## Comment ##
#mydestination = $myhostname, localhost.$mydomain, localhost
## Uncomment ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Uncomment and add IP range ##
mynetworks = 192.168.43.0/24, 127.0.0.0/8
## Uncomment ##
home_mailbox = Maildir/
校验更改
# cat /etc/postfix/main.cf | grep -Env '^$|#'
完成后,保存并退出配置文件。重新启动后缀服务以使更改生效
# systemctl restart postfix
测试Postfix邮件服务器
测试我们的配置是否有效,首先,创建一个测试用户
# useradd postfixuser
# passwd postfixuser
接下来,运行以下命令以将电子邮件从beijing本地用户发送给另一个用户“postfixuser ”
# telnet localhost smtp
or
# telnet localhost 25
如果未安装telnet服务,则可以使用以下命令进行安装
# dnf install telnet -y
如前所述运行命令时,应获得如下所示的输出
[root@server1 ~]# telnet localhost smtp
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 server1.admin.info ESMTP Postfix
以上确认与postfix邮件服务器的连接正常。接下来,键入命令:
# ehlo localhost
输出将是这样的
250-server1.admin.info
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
接下来,运行以橙色突出显示的命令,例如“mail from”,“rcpt to”,data,最后键入quit
mail from:<beijing>
250 2.1.0 Ok
rcpt to:<postfixuser>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello,welcom to my mailserver(Postfix)
.
250 2.0.0 Ok: queued as D1DCA813CE6
quit
221 2.0.0 Bye
Connection closed by foreign host.
完整的telnet命令可以将电子邮件从本地用户“beijing”发送到另一个本地用户“postfixuser”
如果一切都按计划进行,则您应该能够查看在新用户的主目录中发送的电子邮件
[root@server1 new]# ls /home/postfixuser/Maildir/new
1576549565.Vfd04I200a0adM774839.server1.admin.info
要阅读电子邮件,只需使用cat命令,如下所示
# cat /home/postfixuser/Maildir/new/1576549565.Vfd04I200a0adM774839.server1.admin.info
Postfix邮件服务器日志
Postfix邮件服务器邮件日志存储在文件“/var/log/maillog”中,使用以下命令查看实时日志
# tail -f /var/log/maillog
退出用Ctrl+C
保护Postfix邮件服务器
始终建议确保客户端和后缀服务器之间的通信安全,这可以使用SSL证书来实现,这些证书可以来自受信任的权威机构或“自签名”证书。在本教程中,我们将使用openssl命令生成用于后缀的自签名证书
我假设openssl已经安装在您的系统上,如果未安装,请使用以下yum命令
# yum install openssl -y
使用下面的openssl命令生成私钥和CSR(证书签名请求)
# openssl req -nodes -newkey rsa:2048 -keyout mail.key -out mail.csr
Generating a 2048 bit RSA private key
.....................................+++
.....................................................................+++
writing new private key to 'mail.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shang Hai
Locality Name (eg, city) [Default City]:Shang Hai
Organization Name (eg, company) [Default Company Ltd]:IT
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:server1.admin.info
Email Address []:info@admin.info
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
现在,使用以下openssl命令生成自签名证书,
# openssl x509 -req -days 365 -in mail.csr -signkey mail.key -out mail.crt
Signature ok
subject=/C=CN/ST=Shang Hai/L=Shang Hai/O=IT/OU=IT/CN=server1.admin.info/emailAddress=info@admin.info
Getting Private key
现在将私钥和证书文件复制到/etc/postfix目录
# cp mail.key mail.crt /etc/postfix
在后缀配置文件中添加私钥和证书文件的路径
# vi /etc/postfix/main.cf
………
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/mail.crt
smtpd_tls_key_file = /etc/postfix/mail.key
smtpd_tls_security_level = may
………
重新启动postfix服务以使上述更改生效
systemctl restart postfix
让我们尝试使用mailx客户端将电子邮件发送到内部本地域和外部域
发送本地内部电子邮件
# echo "test email" | mailx -s "Test email from Postfix MailServer" -r shanghai@admin.info postfixuser@admin.info
使用以下方法检查并阅读电子邮件
[root@server1 new]# cd /home/postfixuser/Maildir/new/
[root@server1 new]# ll
总用量 20
-rw-------. 1 postfixuser postfixuser 434 12月 17 10:26 1576549565.Vfd04I200a0adM774839.server1.admin.info
-rw-------. 1 postfixuser postfixuser 561 12月 17 10:48 1576550892.Vfd04I200a0aeM465920.server1.admin.info
-rw-r--r--. 1 root root 1306 12月 17 10:39 mail.crt
-rw-r--r--. 1 root root 1054 12月 17 10:38 mail.csr
-rw-r--r--. 1 root root 1704 12月 17 10:38 mail.key
[root@server1 new]# cat 1576550892.Vfd04I200a0aeM465920.server1.admin.info
Return-Path: <shanghai@admin.info>
X-Original-To: postfixuser@admin.info
Delivered-To: postfixuser@admin.info
Received: by server1.admin.info (Postfix, from userid 0)
id 60BFC813CE6; Tue, 17 Dec 2019 10:48:12 +0800 (CST)
Date: Tue, 17 Dec 2019 10:48:12 +0800
From: shanghai@admin.info
To: postfixuser@admin.info
Subject: Test email from Postfix MailServer
Message-ID: <5df841ec.OIQwHU4o+XUCrKk0%shanghai@admin.info>
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
test email
发送电子邮件到外部域
# echo "External Test email" | mailx -s "Postfix MailServer" -r info@admin.info jk.d@aisino.sh.cn
# echo "External Test email" | mailx -s "Postfix MailServer" -r infomaster@admin.info jk.d@aisino.sh.cn
echo "External Test email" | mailx -s "Postfix MailServer" jk.d@aisino.sh.cn
注册发QQ邮箱要加白名单,发件人不填则默认为root@admin.info
检查Postfix邮件队列
使用mailq命令列出队列中的邮件
# mailq
Mail queue is empty
# 查看邮件日志
cat /var/log/maillog
# 查看邮件日志
cat /var/log/maillog
评论
发表评论