PostfixでメールリレーしてMailHogで受信する開発用Dockerコンテナの構築

PostfixでメールリレーしてMailHogで受信する開発用Dockerコンテナの構築

2021-05-195 min read

目次

  1. 概要
  2. dockerイメージ作成
  3. コンテナの起動
  4. telnetで送信テスト
  5. phpで送信テスト

概要

Postfixのリレーを介して送信されたメールをMailHog(開発用SMTPサーバ)でキャッチするDocker開発環境を構築した際のメモです。

環境

  • Docker
    • CentOS:8.3

Dockerイメージ作成

Dockerイメージ作成に必要なファイルの用意を行います。

リポジトリの構成はこのようにします。

├── app
│   ├── Dockerfile
│   └── conf
│       ├── entrypoint.sh
│       └── main.cf
└── docker-compose.yml

app/Dockerfile

FROM centos:8.3.2011

RUN dnf -y install postfix

# おまけ: 送信テストのためにphpとtelnetをインストール
RUN dnf -y install telnet php php-mbstring

COPY conf/entrypoint.sh /entrypoint.sh
COPY conf/main.cf /etc/postfix/main.cf

RUN chmod 700 /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
CMD ["/sbin/init"]

conf/main.cf

# relayhost = [host.docker.internal]:1025
compatibility_level = 2
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
mydomain = example.com
myorigin = $mydomain
inet_interfaces = localhost
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
mynetworks = localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
home_mailbox = Maildir/
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix/samples
readme_directory = /usr/share/doc/postfix/README_FILES
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_security_level = may
smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtp_tls_security_level = may
meta_directory = /etc/postfix
shlib_directory = /usr/lib64/postfix

conf/entrypoint.sh

#!/bin/bash

set -e

# リレーホストの設定
echo "relayhost = $MAIL_SERVICE" >> /etc/postfix/main.cf

# Postfixを起動
/usr/sbin/postfix -c /etc/postfix start

exec "$@"

docker-compose.yml

version: '3'
services:
  app:
    container_name: "app"
    build: ./app
    tty: true
    environment: 
      - MAIL_SERVICE=[mail]:1025
    networks:
      - my-network1
  mail:
    image: mailhog/mailhog:latest
    ports:
      - "8025:8025"
      - "1025:1025"
    networks:
      - my-network1

networks:
  my-network1:

この設定でアプリケーションを動かすコンテナと開発用のメール受信コンテナ(MailHog)を作成できました。

コンテナの起動

次のコマンドでコンテナを起動します。

docker-compose up -d --build

MailHogは http://localhost:8025 からアクセスできます。

コンテナにシェルで入ります。

docker exec -it app bash

telnetで送信テスト

telnetでローカルホストのSMTPサーバに繋ぎに行きます。

telnet localhost 25

次のようにメッセージを入れることでメール送信を確認することができます。

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 90f3e3d03e27.example.com ESMTP Postfix
HELO foo@example.com
250 90f3e3d03e27.example.com
MAIL FROM: <foo@example.com>
250 2.1.0 Ok
RCPT TO: <bar@example.com>
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Subject: test 

Hello World!
.
250 2.0.0 Ok: queued as B6DD13210E4
quit
221 2.0.0 Bye
Connection closed by foreign host.

phpで送信テスト

次のようにPHPファイルを作成し実行することでメールの送信を確認することができると思います。

<?php
$to = "to@example.com";
$subject = "TEST";
$message = "Hello World!";
$headers = "From: from@example.com";
mb_send_mail($to, $subject, $message, $headers);
Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項