LoginSignup
1
2

postfixで自分宛に送ったメールを受け取ってみた

Last updated at Posted at 2023-07-18

postfix試したい

postfixじゃなくてもいいけど、
とにかくメールサーバを理解したい
けど練習なのにドメインとかサーバとかいきなり用意できない
とりあえず触ってみたいだけなのに…
そんなあなたはdockerでubuntuのイメージを用意し
そこで自分宛のメールを送り自分で受け取ることから始めようじゃないか

前提

Dockerインストール済みであること

Ubuntuコンテナを作る

docker run -it —name my_ubuntu_contaier ubuntu bash

このコマンドで

ubuntuの最近のイメージがpullされる(ubuntuがローカルにないとき)

→ それを起動する

→ 起動したコンテナのbashを起動する

ということまでいっぺんにやってくれる

成功すると、

# <- bashのプロンプト(コマンドを受け付けているところ)が表示される。

これでDocker container内のUbuntuとbashを通じて対話しているのと同じ状態になる

コンテナ内で環境を設定する

apt-get update && apt-get upgrade -y

これでUbuntuのシステムを更新することができる。

apt-getはdebian系のLinux(ubuntuもその一つ)で使うコマンド。

Postfixのインストール

apt-get install postfix -y

実行するとpostfixの設定に自動で移る。

Please select the mail server configuration type that best meets your needs.

 No configuration:
  Should be chosen to leave the current configuration unchanged.
 Internet site:
  Mail is sent and received directly using SMTP.
 Internet with smarthost:
  Mail is received directly using SMTP or by running a utility such
  as fetchmail. Outgoing mail is sent using a smarthost.
 Satellite system:
  All mail is sent to another machine, called a 'smarthost', for
  delivery.
 Local only:
  The only delivered mail is the mail for local users. There is no
  network.

  1. No configuration  3. Internet with smarthost  5. Local only
  2. Internet Site     4. Satellite system
General mail configuration type: 2

The 'mail name' is the domain name used to 'qualify' _ALL_ mail addresses
without a domain name. This includes mail to and from <root>: please do not
make your machine send out mail from root@example.org unless root@example.org
has told you to.

This name will also be used by other programs. It should be the single, fully
qualified domain name (FQDN).

Thus, if a mail address on the local host is foo@example.org, the correct value
for this option would be example.org.

System mail name: my_ubuntu_container

General mail configurationには、2を選んでおく

System mail nameにはコンテナの名前、ここではmy_ubuntu_containerを設定しておく(あくまでも例)

Postfixの設定ファイル

次にPostfixの設定ファイルである /etc/postfix/main.cfを開いてみる

私の環境ではvimがなかったのでインストール

apt-get install -y vim

// 設定ファイルを編集する
vim /etc/postfix/main.cf

/etc/postfix/main.cfの設定事項

myhostname → メール送信時、メールヘッダ(エンベロープ)の一部になる

mydestination → これらのドメイン名とホスト名から来たメールを受け取る、というリスト

mynetworks → 信頼するネットワークのリスト

inet_interfaces → Postfixのリッスンするネットワークインターフェース

これらを確認する。

myhostname = my_container_id
mydestionation = $myhostname, my_ubuntu_container, my_container_id, localhost.localdomain, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = loopback-only

この設定を確認することで

Docker内部でとりあえずローカルメールの送信はできるようになっていることがわかる

(結局編集しなかったのでcatで確認するだけでも良かった)

echo "This is a test." | mail -s "Test mail" root

とするとメールをローカルで送ることができるはずなのだが

私の環境ではmail commandが使えなかったので

apt-get install -y mailutils

インストールが無事完了した場合

メールを自分に送ることができる

しかしそれを受け取るにはpostfixのサービスを当然起動しなければならない

service postfix start

これを実行してから以下のようにコマンドを打つ。

root@my_container_id:/# echo "This is a test." | mail -s "Test mail" root
root@mycontainer_id:/# mail
"/var/mail/root": 1 message 1 new
>N   1 root               Tue Jul 18 10:16  14/411   Test mail
?

このようにmailを送ることができるはず。

ちなみに、postfixをあえてstartせず先にメールを送っておき、

mailで確認しようとすると、やはりNo mail for rootと表示される

しかしpostfixが起動していない時に送ったメールはきちんと保存される

それは、postqueue -pで確かめられる

root@3b4d88febc64:/# postqueue -p
postqueue: warning: Mail system is down -- accessing queue directly
-Queue ID-  --Size-- ----Arrival Time---- -Sender/Recipient-------
D643E320A65     206 Wed Jul 19 01:19:18  root@3b4d88febc64
                                         root

58BBC3206D8     206 Wed Jul 19 01:18:50  root@3b4d88febc64
                                         root

このキューに入っているメールはpostfixを改めて起動するときちんと届く。

-s はsubjectのsだったので “Test mail”とした、その題名がきちんと設定されているのがわかる。

メールを開きたい時はmailと打つ。そうするとmail boxに入ることができる。

mail boxの中では、メールの番号を選ぶだけで、メールを開くことができる。

?と出ているのはプロンプトである。1 をおす。

To: root@my_container_id
User-Agent: mail (GNU Mailutils 3.14)
Date: Tue, 18 Jul 2023 10:16:03 +0000
Message-Id: <20230718101603.D2D8F321394@sender_domain_name>
From: root <root@my_contaier_id>

This is a test.

?

このようなメールの内容が確認できる。

ちなみにmessage-idは全てのメールが一意のIDを持つという原則に基づいて、

メールシステム全体でそれを保証するために割り振られている。

<2023…@sender_domain_name>としたが、

今回の場合送り主のドメイン名がコンテナのIDと等しくなっているので

<2023...@my_container_id>とも言える。

そしてこれらの値はあくまでメールの送受信の際に使われ、

セキュリティを担保しているわけではない。

認証もやっていない。そういった役割はSPD, DKIM, DMARCなどのメカニズムが該当する(らしい)。

今回やったことのまとめ

  • ローカルにたてたSMTPサーバの動作を、メールを受け取ることで確認した

次回(あれば)

  • DovecotでPOP/IMAPサーバを作る
  • 自分のメールクライアントからメールボックスへアクセスする
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2