## Linux中无法发送邮件
>[success]出现这种情况,一般是因为服务器上设置了防火墙。
在这里,默认使用如下防火墙设置
~~~
#!/bin/bash
#配置服务器防火墙
#author yifeng (E-mail:576617109@qq.com)
iptables -F #先清空规则
iptables -P INPUT DROP #禁止所有访问
iptables -P OUTPUT DROP #禁止所有响应
iptables -P FORWARD DROP #禁止所有转发
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许web访问
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许ssh访问
iptables -A INPUT -p icmp -j ACCEPT #开启ping
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT #响应web访问
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT #响应ssh访问
iptables -A OUTPUT -p icmp -j ACCEPT #响应ping 访问
~~~
按如上方式配置后,执行下面语句进行邮件发送:
~~~
echo '测试'|mailx -s "易风课堂" 576617109@qq.com
~~~
出现如下提示:
![](https://box.kancloud.cn/55784400747dce514335c71897e9711a_1102x135.png)
根据提示可以知道是没有办法,正确的解析smtp.163.com,这里是因为53端口没有开。
执行如下语句开启53端口
~~~
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p UDP --dport 53 -j ACCEPT
iptables -A OUTPUT -p UDP --sport 53 -j ACCEPT
iptables -A INPUT -p UDP --sport 53 -j ACCEPT
iptables -A OUTPUT -p UDP --dport 53 -j ACCEPT
~~~
然后,继续执行邮件发送:
~~~
echo '测试'|mailx -s "易风课堂" 576617109@qq.com
~~~
出现如下提示:
![](https://box.kancloud.cn/b0025e0b2ddf576d1abd2b60bac7744d_1102x153.png)
出现以上提示,是因为25端口没有开,所以没有办法以进行通信。
执行如下命令,开启25端口:
~~~
#! /bin/bash
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p udp --dport 25 -j ACCEPT
iptables -A OUTPUT -p udp --sport 25 -j ACCEPT
iptables -A INPUT -p udp --sport 25 -j ACCEPT
iptables -A OUTPUT -p udp --dport 25 -j ACCEPT
~~~
开启25端口之后,即可以正常发送邮件了。