【備忘録】開発環境の作り方 - メール環境

# postfix インストール

```
# yum install postfix
# systemctl restart postfix
    mail_version = 3.5.25
# postconf | grep mail_version
# systemctl enable postfix

# vi /etc/postfix/main.cf
(変更点は下記参照)
# diff /etc/postfix/main.cf- /etc/postfix/main.cf
95a96
> myhostname = vicuna.mail-server.test
102a104
> mydomain = mail-server.test
118c120
< #myorigin = $mydomain
---
> myorigin = $mydomain
135c137
< inet_interfaces = localhost
---
> inet_interfaces = all
183,184c185,186
< mydestination = $myhostname, localhost.$mydomain, localhost
< #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
---
> #mydestination = $myhostname, localhost.$mydomain, localhost
> mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
227c229
< #local_recipient_maps = unix:passwd.byname $alias_maps
---
> local_recipient_maps = unix:passwd.byname $alias_maps
438c440
< #home_mailbox = Maildir/
---
> home_mailbox = Maildir/

# 全てのメールを任意の宛先に統一するための設定（設定自体は header_checks に記載する）
567c569
< #header_checks = regexp:/etc/postfix/header_checks
---
> header_checks = regexp:/etc/postfix/header_checks
(ZZ)

# systemctl restart postfix
```
# 全てのメールを任意の送信先に統一する
```console
# vi /etc/postfix/header_checks
# 末尾に以下を追記
/^To:.*/ REDIRECT tonoaki_sato@mail-server.test
(ZZ)
# systemctl restart postfix
```
# telnet インストールとローカル内配信テスト
```
# yum install telnet
# telnet localhost 25
        Trying ::1...
        telnet: connect to address ::1: Connection refused
        Trying 127.0.0.1...
        Connected to localhost.
        Escape character is '^]'.
        220 stampede.mail-server.test ESMTP Postfix
    >   helo localhost
        250 stampede.mail-server.test
    >   mail from: tonoaki_sato@mail-server.test
        250 2.1.0 Ok
    >   rcpt to: tonoaki_sato@mail-server.test
        250 2.1.5 Ok
    >   data
        354 End data with <CR><LF>.<CR><LF>
    >   helo postfix
    >   .
        250 2.0.0 Ok: queued as 8B32F2005C75
    >   quit
        221 2.0.0 Bye
        Connection closed by foreign host.
# ls /home/tonoaki_sato/Maildir/
cur  new  tmp
```
# Dovecot インストール
```
# systemctl start dovecot
# systemctl enable dovecot
# vi /etc/dovecot/dovecot.conf
    protocols = imap pop3 lmtp
    listen = *
    (ZZ)
# vi /etc/dovecot/conf.d/10-mail.conf
    mail_location = maildir:~/Maildir
    (ZZ)
# vi /etc/dovecot/conf.d/10-auth.conf
    disable_plaintext_auth = no
    auth_mechanisms = plain login
    (ZZ)
# vi /etc/dovecot/conf.d/10-ssl.conf
    ssl = no
    (ZZ)
# systemctl restart dovecot
```
# ローカル配信メールの受信テスト
```
# telnet localhost 110
        Trying ::1...
        telnet: connect to address ::1: Connection refused
        Trying 127.0.0.1...
        Connected to localhost.
        Escape character is '^]'.
        +OK Dovecot ready.
    >   USER tonoaki_sato
        +OK
    >   PASS 1234567890
        +OK Logged in.
    >   LIST
        +OK 2 messages:
        1 498
        2 355
        .
    >   RETR 1
        +OK 498 octets
        Return-Path: <tonoaki_sato@mail-server.test>
        X-Original-To: tonoaki_sato@mail-server.test
        Delivered-To: tonoaki_sato@mail-server.test
        Received: from localhost (localhost [127.0.0.1])
                by stampede.mail-server.test (Postfix) with SMTP id 8B32F2005C75
                for <tonoaki_sato@mail-server.test>; Sat, 10 Sep 2022 00:47:25 +0900 (JST)
        Message-Id: <20220909154740.8B32F2005C75@stampede.mail-server.test>
        Date: Sat, 10 Sep 2022 00:47:25 +0900 (JST)
        From: tonoaki_sato@mail-server.test

        helo postfix
        .
    >   quit
        +OK Logging out.
        Connection closed by foreign host.
```
# リモートテスト
```
# firewall-cmd --permanent --add-service=smtp --zone=public
# firewall-cmd --permanent --add-service=pop3 --zone=public
# firewall-cmd --reload
# firewall-cmd --list-all

[ホストOSのコマンドプロンプト]
telnet 192.168.56.110 25
        220 stampede.mail-server.test ESMTP Postfix
    >   helo localhost
        250 stampede.mail-server.test
    >   mail from: tonoaki_sato@mail-server.test
        250 2.1.0 Ok
    >   rcpt to: tonoaki_sato@mail-server.test
        250 2.1.5 Ok
    >   data
        354 End data with <CR><LF>.<CR><LF>
    >   helo remote postfix
    >   .
        250 2.0.0 Ok: queued as 0E2EC2005C60
    >   quit
        221 2.0.0 Bye
telnet 192.168.56.110 110
        +OK Dovecot ready.
    >   USER tonoaki_sato
        +OK
    >   PASS **********
        +OK Logged in.
    >   LIST
        +OK 2 messages:
        1 498
        2 355
        .
    >   RETR 1
        +OK 498 octets
        Return-Path: <tonoaki_sato@mail-server.test>
        X-Original-To: tonoaki_sato@mail-server.test
        Delivered-To: tonoaki_sato@mail-server.test
        Received: from localhost (localhost [127.0.0.1])
                by stampede.mail-server.test (Postfix) with SMTP id 8B32F2005C75
                for <tonoaki_sato@mail-server.test>; Sat, 10 Sep 2022 00:47:25 +0900 (JST)
        Message-Id: <20220909154740.8B32F2005C75@stampede.mail-server.test>
        Date: Sat, 10 Sep 2022 00:47:25 +0900 (JST)
        From: tonoaki_sato@mail-server.test

        helo postfix
        .
    >   RETR 2
        +OK 355 octets
        Return-Path: <tonoaki_sato@mail-server.test>
        X-Original-To: tonoaki_sato@mail-server.test
        Delivered-To: tonoaki_sato@mail-server.test
        Received: from localhost (unknown [192.168.56.1])
                by stampede.mail-server.test (Postfix) with SMTP id 0E2EC2005C60
                for <tonoaki_sato@mail-server.test>; Sat, 10 Sep 2022 01:23:57 +0900 (JST)

        helo remote postfix
        .
    >   quit
        +OK Logging out.
```
# メーラー設定
ゲストOSに適当なメーラーをインストールして設定する
nPOP 使用

# 参考
https://qiita.com/y-araki-qiita/items/864dab044786f008e3c9