#########################单元四 配置电子邮件传输
######################################基本电子邮件发送
电子邮件发送
**服务器使用SMTP协议将电子邮件提交至TCP端口25,或由本地客户端通过 /usr/bin/sendmail 程序进行提交。如果该MTA是最终目标位置,邮件将传递至MDA。否则,将使用MX记录在DNS中查找下一个MTA,并使用SMTP进行转发。
**MDA:“邮件发送代理”。MDA将邮件发送至收件人的本地邮件存储位置(默认情况下是/var/spool/mail/user)。Postfix提供自己的MDA,以发送至基于文件的本地默认邮件存储位置
/usr/libexec/postfix/local.
**转发:电子邮件服务器(MTA)将提交的邮件转发至另一个服务器,以进行发送
**排队:失败的发送或转发尝试排队等待,并由MTA定义重试。(默认情况下,Postfix每小时执行此操作一次)
**拒绝:在首次提交期间,电子邮件被电子邮件服务器拒绝
**退回:远程服务器接受电子邮件以进行发送以后,又将该电子邮件退回给始发电子邮件服务器和/或用户
**电子邮件以进行发送以后,又将该电子邮件退回给始发电子邮件服务器和/或用户
Postfix由postfix RPM包提供,并通过postfix服务脚本控制。它是一个由多个协同操作程序构成的模块化程序,它的组件由master进程控制。
**Postfix的主配置文件是/etc/postfix/main.cf,可以使用文本编辑器或postconf命令进行编辑。postconf命令还可用于确定Postfix的所有当前和默认配置设置或逐项确定这些设置。
**默认情况下,Postfix仅侦听来自本地主机的传入电子邮件。若要重新配置postfix以接收从远程主机发送的本地邮件,必须在/etc/postfix/main.cf中设置inet_interfaces = all
**对电子邮件进行故障排除时,将在/var/log/maillog中保留所有与邮件相关的操作日志,其中包括关于被事件和成功事件的信息。mailq命令(或postqueue -p)显示已排队的所有传出邮件的列表。若要尝试再次立即发送所有已排队的邮件,可以运行postfix flush命令(或postqueue -f);否则,postfix将大约每小时尝试重新发送一次,直至邮件被接受或过期。
###重要的Postfix配置指令
可以在/etc/postfix/main.cf文件中找到以下所有指令。
myorigin
重写本地发布的电子邮件,使其显示为来自该域。这样有助于确保响应返回入站邮件服务器默认:myorigin = $myhostname
inet_interfaces
控制Postfix侦听传入电子邮件的网络接口。如果设置为loopback-only,仅侦听127.0.0.1和::1,
如果设置为all,则侦听所有网络接口。还可以指定特定地址。默认:inet_interfaces = localhost
mydestination
收到地址为这些域的电子邮件将传递至MDA,以进行本地发送。默认:mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks
IP地址和网络的逗号分隔列表(采用CIDR表示法)。这些地址和网络可以通过此MTA转发至任何位置,无需进一步身份验证。默认:mynetworks = 127.0.0.0/8
relayhost
relayhost转发所有通过的出站邮件。通常用方括号指定,以阻止MX记录查找。默认:relayhost =
local_transport
收件地址为$mydestination的邮件的发送方式。默认情况下,设置为local:$myhostname(使用Local MDA将传入电子邮件发送到/var/spool/mail中的本地邮件存储位置)
默认:local_transport = local:$myhostname
postconf -d 显示默认值。
postconf -n显示不同于默认值的更改。
##########################################空壳邮件客户端
实际上,大多数组织不再只用一个邮件服务器来处理所有入站和出战电子邮件。相反,出于安全方面的考虑邮件服务器专门针对特定角色进行了设置,以便可以面向其具体针对的应用程序更好标准角色包括:
**null客户端:运行本地MTA的客户端计算机,使所有电子邮件都可以转发至中央邮件服务器以进行发送,null客户端不接受任何电子邮件的本地发送。
**仅入站邮件服务器:在站点处理用户的所有传入电子邮件,并将之传递给MDA以发送至用户邮件存储位置的邮件服务器。在实际情况中,通常会在仅入站邮件服务器前端安装反垃圾邮件服务器或设备,以
过滤垃圾邮件并且仅将 正常邮件转发至入站邮件服务器。
**出站邮件转发:出站邮件转发(或“smarthost”)接收所有出站邮件,并使用MX记录和SMTP协议将邮件转发至目标位置 。
eg: 搭建dns
[root@mail-qq ~]# yum install bind -y
[root@mail-qq ~]# vim /etc/named.conf
// listen-on port 53 { 127.0.0.1; }; ##注释掉相当允许所有连接
// listen-on-v6 port 53 { ::1; };
// allow-query { localhost; };
dnssec-validation no;
[root@mail-qq ~]# vim /etc/named.rfc1912.zones
zone "qq.com" IN {
type master;
file "qq.com.zone";
allow-update { none; };
};
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { none; };
};
[root@mail-qq ~]# cd /var/named/
[root@mail-qq named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@mail-qq named]# cp named.localhost westos.com.zone -p
[root@mail-qq named]# vim westos.com.zone
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.141
westos.com. MX 1 172.25.254.241.
[root@mail-qq named]# cp -p westos.com.zone qq.com.zone
[root@mail-qq named]# vim qq.com.zone
$TTL 1D
@ IN SOA dns.qq.com. root.qq.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.qq.com.
dns A 172.25.254.141
qq.com. MX 1 172.25.254.141.
[root@mail-qq named]# systemctl start named
[root@mail-qq named]# systemctl stop firewalld.service
[root@mail-qq named]# systemctl disable firewalld.service
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@mail-qq named]# vim /etc/resolv.conf
nameserver 172.25.254.141
[root@mail-qq named]# dig -t mx qq.com
[root@mail-qq named]# dig -t mx westos.com
[root@mail-westos ~]# systemctl stop firewalld.service
[root@mail-westos ~]# systemctl disable firewalld.service
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@mail-westos ~]# vim /etc/resolv.conf
nameserver 172.25.254.141
[root@mail-westos ~]# dig -t mx qq.com
[root@mail-westos ~]# dig -t mx westos.com
eg:发送邮件
[root@mail-qq named]# vim /etc/postfix/main.cf
76 myhostname = mail-qq.qq.com
83 mydomain = qq.com
99 myorigin = $mydomain
116 inet_interfaces = all
164 mydestination = $myhostname, $mydomain, localhost
[root@mail-qq named]# systemctl restart postfix.service
[root@mail-qq named]# mail [email protected]
Subject: efe
vr
.
EOT
[root@mail-qq named]# mailq ##空表示发送出去
Mail queue is empty
[root@mail-westos ~]# vim /etc/postfix/main.cf
76 myhostname = mail-westos.westos.com
83 mydomain = westos.com
99 myorigin = $mydomain
116 inet_interfaces = all
164 mydestination = $myhostname, $mydomain, localhost
[root@mail-westos ~]# systemctl restart postfix.service
[root@mail-westos ~]# mail ##查看接收的邮件
eg:用另一测试
[root@foundation41 ~]# yum install telnet
[root@foundation41 ~]# telnet 172.25.254.141 25
Trying 172.25.254.141...
Connected to 172.25.254.141.
Escape character is '^]'.
220 mail-qq.qq.com ESMTP Postfix
mail from:[email protected]
250 2.1.0 Ok
rcpt to:[email protected]
250 2.1.5 Ok
data
354 End data with
rthgrthtrhytehhy
htrhtr
.
250 2.0.0 Ok: queued as 75104246208
quit
221 2.0.0 Bye
Connection closed by foreign host.
eg:另一台主机接收
[root@mail-qq named]# mail [email protected]
Subject: fegver
vgrebvgreb
.
EOT30 mail_location = mbox:~/mail:INBOX=/var/mail/%u
[root@mail-qq named]# mailq
Mail queue is empty
[root@mail-westos ~]# mail -u student ##查看student用户发送的邮件
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/mail/student": 1 message 1 new
>N 1 root Thu Apr 20 23:25 21/732 "fegve"
& q
Held 1 message in /var/mail/student
[root@mail-westos ~]# yum install dovecot -y
[root@mail-westos ~]# systemctl start dovecot
[root@mail-westos ~]# netstat -antlpe |grep dovecot
[root@mail-westos ~]# vim /etc/dovecot/dovecot.conf
24 protocols = imap pop3 lmtp
disable_plaintext_auth = no
[root@mail-westos ~]# vim /etc/dovecot/conf.d/10-mail.conf
30 mail_location = mbox:~/mail:INBOX=/var/mail/%u
[root@mail-westos ~]# cd /var/mail/
[root@mail-westos mail]# ls
root rpc student
[root@mail-westos mail]# systemctl restart dovecot.service
[root@mail-westos ~]# su - student
[student@mail-westos ~]$ mkdir /home/student/mail/.imap
[student@mail-westos ~]$ touch /home/student/mail/.imap/INBOX
[root@foundation41 ~]# yum install mutt -y
[root@foundation41 ~]# mutt -f pop://[email protected]
[root@foundation41 ~]# mutt -f pop://[email protected]
[root@foundation41 ~]# mutt -f pop://[email protected]
[root@foundation41 ~]# mutt -f pop://[email protected]
eg:远程图形界面收发邮件
lftp 172.25.254.250:/pub/docs/software> get thunderbird-31.2.0-1.el7.x86_64.rpm
52387876 bytes
transferred
[root@foundation41 ~]# thunderbird ##安装客户端
[root@mail-westos mysqladmin]# vim /etc/dovecot/dovecot.conf
48 login_trusted_networks = 0.0.0.0/0
[root@mail-qq named]# vim /etc/named.rfc1912.zones
37 zone "eastos.com" IN {
38 type master;
39 file "eastos.com.zone";
40 allow-update { none; };
41 };
[root@mail-qq named]# cd /var/named/
[root@mail-qq named]# cp -p westos.com.zone eastos.com.zone
[root@mail-qq named]# vim eastos.com.zone
1 $TTL 1D
2 @ IN SOA dns.eastos.com. root.eastos.com. (
3 0 ; serial
4 1D ; refresh
5 1H ; retry
6 1W ; expire
7 3H ) ; minimum
8 NS dns.eastos.com.
9 dns A 172.25.254.141
10
11 eastos.com. MX 1 172.25.254.116.
eg:安装数据库客户端
[root@mail-westos mail]# yum install mariadb-server.x86_64 -y
[root@mail-westos mail]# vim /etc/my.cnf
[root@mail-westos mail]# systemctl start mariadb
[root@mail-westos mail]# mysql_secure_installation
[root@mail-westos mail]# yum install httpd php php-mysql.x86_64 -y
[root@mail-westos mail]# systemctl restart httpd
[root@mail-westos mail]# cd /var/www/html/
[root@mail-westos html]# ls
[root@mail-westos html]# lftp 172.25.254.250
lftp 172.25.254.250:/pub/docs/software> get phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@mail-westos html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@mail-westos html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@mail-westos html]# rm -fr *.bz2
[root@mail-westos html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin
[root@mail-westos html]# ls
mysqladmin
[root@mail-westos html]# cd mysqladmin/
[root@mail-westos mysqladmin]# cp config.sample.inc.php config.inc.php
[root@mail-westos mysqladmin]# vim config.inc.php
[root@mail-westos mysqladmin]# systemctl restart httpd.service
[root@mail-westos mysqladmin]# mysql -uroot -predhat
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE USER postfix@localhost identified by "postfix";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT INSERT,SELECT,UPDATE ON email.*to postfix@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
eg:和数据库连接,查找用户
[root@mail-westos mysqladmin]# groupadd -g 888 vmail
[root@mail-westos mysqladmin]# useradd -g 888 -u 666 vmail
[root@mail-westos mysqladmin]# id vmail
uid=666(vmail) gid=888(vmail) 组=888(vmail)
[root@mail-westos mysqladmin]# cd /home/vmail/
[root@mail-westos vmail]# cd /etc/postfix/
[root@mail-westos postfix]# ls
access generic main.cf relocated virtual
canonical header_checks master.cf transport
[root@mail-westos postfix]# vim mailuser.cf
1 hosts = localhost
2 user = postfix
3 password = postfix
4 dbname = email
5 table = emailuser
6 select_field = username
7 where_field = username
[root@mail-westos postfix]# postmap -q "[email protected]" mysql:/etc/postfix/mailuser.cf
[root@mail-westos postfix]# postmap -q "[email protected]" mysql:/etc/postfix/mailuser.cf
[root@mail-westos postfix]# cp mailuser.cf maildomain.cf
[root@mail-westos postfix]# cp mailuser.cf mailbox.cf
[root@mail-westos postfix]# vim maildomain.cf
1 hosts = localhost
2 user = postfix
3 password = postfix
4 dbname = email
5 table = emailuser
6 select_field = domain
7 where_field = domain
[root@mail-westos postfix]# vim mailbox.cf
1 hosts = localhost
2 user = postfix
3 password = postfix
4 dbname = email
5 table = emailuser
6 select_field = maildir
7 where_field = username
[root@mail-westos postfix]# postmap -q "[email protected]" mysql:/etc/postfix/mailbox.cf
/var/spool/redhat.com/
[root@mail-westos postfix]# postmap -q "redhat.com" mysql:/etc/postfix/maildomain.cf
redhat.com
[root@mail-westos postfix]# postconf -d | grep virtual
[root@mail-westos postfix]# postconf -e "virtual_mailbox_base =/home/vmail" ##-e编辑文件
[root@mail-westos postfix]# postconf -e "virtual_uid_maps =static:666"
[root@mail-westos postfix]# postconf -e "virtual_gid_maps =static:888"
[root@mail-westos postfix]# postconf -e "virtual_alias_maps =mysql:/etc/postfix/mailuser.cf"
[root@mail-westos postfix]# postconf -e "virtual_mailbox_maps =m[root@mail-westos postfix]# postconf -e "virtual_mailbox_maps =mysql:/etc/postfix/mailbox.cf"
[root@mail-westos postfix]# postmap -q "redhat.com" mysql:/etc/postfix/maildomain.cf
redhat.com
[root@mail-westos postfix]# postmap -q "[email protected]" mysql:/etc/postfix/mailbox.cf
/var/spool/redhat.com/
[root@mail-westos postfix]# cd /home/vmail/
[root@mail-westos vmail]# ls
[root@mail-westos vmail]# mail [email protected]
Subject: test
vegfvrebv
.
EOT
[root@mail-westos vmail]# ls
[root@mail-westos vmail]# mail [email protected]
Subject: test
vebvtrtrnbtr
.
EOT
[root@mail-westos vmail]# mailq
Mail queue is empty
[root@mail-westos vmail]# ls
redhat.com
[root@mail-westos vmail]# cd redhat.com/
[root@mail-westos redhat.com]# ls
cur new tmp
[root@mail-westos redhat.com]# cat new/1492763756.Vfd01I1357547M524953.mail-westos.westos.com
Return-Path:
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: by mail-westos.westos.com (Postfix, from userid 0)
id 6AE3211673F; Fri, 21 Apr 2017 04:35:56 -0400 (EDT)
Date: Fri, 21 Apr 2017 04:35:56 -0400
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (root)
vebvtrtrnbtr
[root@mail-westos redhat.com]# cd /home/vmail/
[root@mail-westos vmail]# mail [email protected]
Subject: test
2333
.
EOT
[root@mail-westos vmail]# mailq
Mail queue is empty
[root@mail-westos vmail]# ls
163.com redhat.com
[root@mail-westos vmail]# cd 163.com/
[root@mail-westos 163.com]# ls
westos
[root@mail-westos 163.com]# cd westos/
[root@mail-westos westos]# ls
cur new tmp
[root@mail-westos westos]# cat new/1492764131.Vfd01I268b0aM763968.mail-westos.westos.com
Return-Path:
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: by mail-westos.westos.com (Postfix, from userid 0)
id A74B511673F; Fri, 21 Apr 2017 04:42:11 -0400 (EDT)
Date: Fri, 21 Apr 2017 04:42:11 -0400
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (root)