Postfix お手軽設定＆起動＆利用

# はじめに
Postfix をお手軽に起動して SMTP サーバを起動する手順です。

セキュアサーバの設定などは置いといて、自分のローカル環境などで SMTP サーバを立てたいなどお手軽に設定する手順なので注意してください。

環境は Red Hat Linux 7 です

```bash
cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.7 (Maipo)
```


# インストール

Postfix とそれに必要なライブラリをインストールする。

```bash
yum -y install postfix
yum -y install cyrus-sasl
```

インストールできたかの確認

```
rpm -q postfix
postfix-2.10.1-9.el7.x86_64
```

以下のライブラリもインストールする

```bash
yum -y install cyrus-sasl-plain
yum -y install cyrus-sasl-md5
```

おそらくこれらのライブラリがないとメール送信するときに以下のようなエラーがでると思います。

```bash
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: connect from ip-172-31-15-34.ap-northeast-1.compute.internal[172.31.15.34]
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: warning: SASL authentication failure: Internal Error -4 in server.c near line 1757
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: warning: SASL authentication failure: Internal Error -4 in server.c near line 1757
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: warning: SASL authentication failure: Internal Error -4 in server.c near line 1757
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: warning: xsasl_cyrus_server_get_mechanism_list: no mechanism available
Aug  5 01:06:52 ip-172-31-15-34 postfix/smtpd[16097]: fatal: no SASL authentication mechanisms
Aug  5 01:06:53 ip-172-31-15-34 postfix/master[16089]: warning: process /usr/libexec/postfix/smtpd pid 16097 exit status 1
Aug  5 01:06:53 ip-172-31-15-34 postfix/master[16089]: warning: /usr/libexec/postfix/smtpd: bad command startup -- throttling
Aug  5 01:08:36 ip-172-31-15-34 postfix/smtpd[16124]: connect from ip-172-31-15-34.ap-northeast-1.compute.internal[172.31.15.34]
Aug  5 01:08:36 ip-172-31-15-34 postfix/smtpd[16124]: disconnect from ip-172-31-15-34.ap-northeast-1.compute.internal[172.31.15.34]
```

# 設定



```bash
# diff -u /etc/postfix/main.cf.orig /etc/postfix/main.cf
--- /etc/postfix/main.cf.orig   2020-08-05 00:24:32.358806466 -0400
+++ /etc/postfix/main.cf        2020-08-05 00:33:54.455135924 -0400
@@ -74,6 +74,7 @@
 #
 #myhostname = host.domain.tld
 #myhostname = virtual.domain.tld
+myhostname = ＜FQDNなホスト名＞

 # The mydomain parameter specifies the local internet domain name.
 # The default is to use $myhostname minus the first component.
@@ -81,6 +82,7 @@
 # parameters.
 #
 #mydomain = domain.tld
+mydomain = ＜ドメイン名＞

 # SENDING MAIL
 #
@@ -97,6 +99,7 @@
 #
 #myorigin = $myhostname
 #myorigin = $mydomain
+myorigin = $mydomain

 # RECEIVING MAIL

@@ -114,6 +117,7 @@
 #inet_interfaces = $myhostname
 #inet_interfaces = $myhostname, localhost
 #inet_interfaces = localhost
+inet_interfaces = all # とりあえず全サーバから送信受付け

 # Enable IPv4, and IPv6 if supported
 inet_protocols = all
@@ -161,7 +165,8 @@
 #
 # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
 #
-mydestination = $myhostname, localhost.$mydomain, localhost
+#mydestination = $myhostname, localhost.$mydomain, localhost
+mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
 #      mail.$mydomain, www.$mydomain, ftp.$mydomain
@@ -417,6 +422,7 @@
 #
 #home_mailbox = Mailbox
 #home_mailbox = Maildir/
+home_mailbox = Maildir/  # メールボックスは各ユーザ(ホームディレクトリ)の Maildir を利用

 # The mail_spool_directory parameter specifies the directory where
 # UNIX-style mailboxes are kept. The default setting depends on the
@@ -571,6 +577,7 @@
 #
 #smtpd_banner = $myhostname ESMTP $mail_name
 #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
+smtpd_banner = $myhostname ESMTP $mail_name # 送信するときのメールバナー

 # PARALLEL DELIVERY TO THE SAME DESTINATION
 #
@@ -677,3 +684,11 @@
 # readme_directory: The location of the Postfix README files.
 #
 readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
+
+
+# SASL 認証できるように追加
+smtpd_sasl_auth_enable = yes
+smtpd_sasl_local_domain = $myhostname
+smtpd_recipient_restrictions =
+    permit_mynetworks
+    permit_sasl_authenticated
+    reject_unauth_destination
```

# デーモン起動

SASL 認証用のデーモンと共に Postfix を起動

```bash
systemctl start saslauthd
systemctl enable saslauthd
systemctl restart postfix
systemctl enable postfix
```

# メール送信確認

telnet コマンドを利用してメール送信確認

```bash
yum -y install telnet

telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 ＜ホスト名＞ ESMTP Postfix
helo ＜ホスト名＞
250 ＜ホスト名＞
mail from: <＜メールアドレス＞>
250 2.1.0 Ok
rcpt to: <＜メールアドレス＞>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
test
.
250 2.0.0 Ok: queued as 2CDE3C00DBF
quit
221 2.0.0 Bye
Connection closed by foreign host.
```


送信したメールの確認

```bash
# cat  ~＜ユーザ名＞/Maildir/new/1596674264.Vca02I80de9aM88429.ip-172-31-8-85.ap-northeast-1.compute.internal
Return-Path: <＜メールアドレス＞>
X-Original-To: ＜メールアドレス＞
Delivered-To: ＜メールアドレス＞
Received: from ＜ホスト名＞ (localhost [IPv6:::1])
        by ＜ホスト名＞ (Postfix) with SMTP id 2CDE3C00DBF
        for <＜メールアドレス＞>; Thu,  6 Aug 2020 00:37:26 +0000 (UTC)
Message-Id: <20200806003735.2CDE3C00DBF@＜ホスト名＞>
Date: Thu,  6 Aug 2020 00:37:26 +0000 (UTC)
From: ＜メールアドレス＞

test
```