# Linux搭建邮件服务器

此文我们使用Postfix来搭建邮箱服务器，Postifx是一个SMTP服务器。SMTP服务器也被称为MTA（message transfer agent）

## 一、安装postfix

```
sudo apt-get install mailutils
# mailutils是一个命令行邮箱客户端，使用它可以很方便的发送和接收邮件， mailutils里面就包含了sendmail和postfix，当然你也可以只安装postfix，postfix包含sendmail。
```

安装postfix的时候会弹出两个配置，第一个是postfix配置类型，我们默认选择第二种：Internet Site就行，这几种配置分别表示：

* No configuration：表示不要做任何配置；
* Internet Site：表示直接使用本地SMTP服务器发送和接收邮件；
* Internet with smarthos： 表示使用本地SMTP服务器接收邮件，但发送邮件时不直接使用本地SMTP服务器，而是使用第三方smart host来转发邮件；
* Satellite system：表示邮件的发送和接收都是由第三方smarthost来完成。
* Local only：表示邮件只能在本机用户之间发送和接收。

在第二个页面System mail name中填入你的域名，也就是邮箱地址@符号后面的域名，比如，我的邮箱地址是`wangjun@bonnenuit.vip`，所以我填的是`bonnenuit.vip`。当发件人的域名地址没有指定时，Postfix会自动将这个域名添加到发件人的地址中。

Postfix在安装过程中会生成/etc/postfix/main.cf配置文件。安装完成后Postfix会自动运行。我们可以用下面的命令查看Postfix的版本。

```
$ sudo postconf mail_version
mail_version = 3.3.0
```

使用netstat来查看Postfix的监听情况：

```
$ sudo netstat -anptl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      474/systemd-resolve 
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1239/cupsd          
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      6995/master         
tcp        0      0 10.235.111.250:45970    151.101.190.217:443     ESTABLISHED 4806/snapd          
tcp        0      0 10.235.111.250:43748    91.189.92.19:443        ESTABLISHED 4806/snapd          
tcp        0      0 10.235.111.250:45960    151.101.190.217:443     ESTABLISHED 4806/snapd          
tcp6       0      0 ::1:631                 :::*                    LISTEN      1239/cupsd          
tcp6       0      0 :::25                   :::*                    LISTEN      6995/master
```

Postfix的master进程监听TCP 25号端口，在发送测试邮件之前，我们最好是查看25号端口是否被防火墙或主机商屏蔽，nmap可以帮助我们扫描服务器的开放端口，在你的个人电脑上运行下面的命令。

```
# 如果没有安装nmap可以先apt-get install nmap安装一下
$ sudo nmap your-server-ip

Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-15 16:34 +08
Nmap scan report for ubuntu (your server ip)
Host is up (0.00040s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
25/tcp open  smtp

Nmap done: 1 IP address (1 host up) scanned in 1.65 seconds
```

## 二、测试发送邮件

```
echo "test email" | sendmail your-eamil
# 也可以使用mail命令来发送邮件，使用mail发送邮件的时候，main.cf配置文件对其不生效，应该有自己的配置文件，还没找到
# mail -s title your-eamil <<< "mail content"
```

查看你的邮箱收件箱，应该可以看到，估计在垃圾箱。

* 日志文件：/var/log/mail.log
* 配置文件：/etc/postfix/main.cf
* 保存用户邮件：/var/spool/mail和/var/mail/&#x20;

## 三、遇到的问题

**问题1：发件人地址不正确**

在`/var/log/mail.log`日志中显示的发件人信息为：

```
Jul 15 15:28:08 mail postfix/pickup[856]: 2E7B6442DC: uid=1000 from=<wangjun>
Jul 15 15:28:08 mail postfix/cleanup[865]: 2E7B6442DC: message-id=<20190715072808.2E7B6442DC@bonnenuit.vip>
Jul 15 15:28:08 mail postfix/qmgr[857]: 2E7B6442DC: from=<wangjun@iZ94pe2uk6bZ>, size=264, nrcpt=1 (queue active)
```

from的地址不对，期望from的值为<wangjun@bonnenuit.vip>。

**解决方案：**

配置`/etc/postfic/main.cf`，将`myhostname = iZ94pe2uk6b`改为`myhostname = bonnenuit.vip`。

**问题2：连接目标邮箱服务器超时**

日志信息：

```
Jul 15 15:33:02 mail postfix/qmgr[857]: 5542E4431B: from=<wangjun@bonnenuit.vip>, size=264, nrcpt=1 (queue active)
Jul 15 15:33:33 mail postfix/smtp[870]: connect to hotmail-com.olc.protection.outlook.com[104.47.46.33]:25: Connection timed out
Jul 15 15:33:33 mail postfix/smtp[871]: connect to hotmail-com.olc.protection.outlook.com[104.47.9.33]:25: Connection timed out
```

**解决方案：**

找了很多资料，发现是阿里云禁用了SMTP服务。<https://help.aliyun.com/knowledge_detail/40680.html?spm=5176.2000002.0.0.8a2f4dd7AtOIaJ，在此条文中记载：>

> 3.1.3.5. 不得将阿里云所提供的云服务器用作虚拟服务器、或用作磁盘空间出租，或用作非法代理服务器（Proxy）或邮件服务器（根据阿里云《云平台安全规则》，未经报备许可云服务器不得用于连接SMTP服务发送邮件，阿里云默认将禁止云服务器用于SMTP服务）；

MTF！无奈换台本地机器吧，不用云服务器了！

> 参考：
>
> <https://wiki.ubuntu.org.cn/Postfix_%E5%9F%BA%E6%9C%AC%E8%AE%BE%E7%BD%AE%E6%8C%87%E5%8D%97>
>
> <https://www.centos.bz/2017/07/ubuntu-install-postfix-mail-server/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jun-wang.gitbook.io/learnjava/kai-fa/huan-jing-da-jian/linux-da-jian-you-jian-fu-wu-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
