postfixによるsmtpメール送信の練習メモです
目次
1. 参考url
2. 全体構成
構築の中心は、以下のSMTP serverで、
AWS内の SMTP server(ec2) から、SMTP(port25)で、Gmailのアドレスへメール送信します。
OP25B(Outbound Port 25 Blocking)により、外向けのSMTP(port25)は通常ブロックされますので、
AWSに対しては、以下のurlから制限解除申請を行っています。
https://aws-portal.amazon.com/gp/aws/html-forms-controller/contactus/ec2-email-limit-rdns-request:tile
また、私の場合、自宅でJ:COMをプロバイダとして利用していますが、
AWSのような制限解除申請はないようですので、
AWS内のSMTP client(ec2)へ、一旦、ssh接続した上で、SMTPコマンドを実行しています。
┌Google─────┐
│┌──────┐│
││Gmail ││
│└──────┘│
└───↑────┘
│ssh【Port25】
┌AWS ─↓────┐
│┏━━━━━━┓│
│┃SMTP server ┃│
│┗━━━━━━┛│
│ ↑smtp(Port25)
│┌──┴───┐│
││SMTP client ││
│└──────┘│
└───↑────┘
│ssh
┌J:COM │────┐
│┌──┴───┐│
││自宅PC ││
│└──────┘│
└────────┘
3. OS関連の確認や、ホスト名、タイムゾーン変更
$ ssh -i ~/.ssh/end0tknr_202008.pem ec2-user@$IPアドレス内緒
amazon linux2 の version 確認
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
$ uname --all
Linux ip-172-31-15-199.ap-northeast-1.compute.internal
4.14.209-160.339.amzn2.x86_64 #1 SMP Wed Dec 16 22:44:04 UTC 2020
x86_64 x86_64 x86_64 GNU/Linux
ホスト名や timezone 変更
$ sudo hostnamectl set-hostname mailp.end0tknr.com
$ sudo vi /etc/sysconfig/clock
#ZONE="UTC"
ZONE="Asia/Tokyo"
UTC=true
$ sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
$ sudo reboot
default installedなpostfixや、postfix関連userの確認
いずれも、amazon linux2ではdefault installされています
$ yum list installed |grep postfix
postfix.x86_64 2:2.10.1-6.amzn2.0.3 installed
[ec2-user@mailp ~]$
# ps -ef | grep -i post
root 2951 1 0 00:40 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 2952 2951 0 00:40 ? 00:00:00 pickup -l -t unix -u
postfix 2953 2951 0 00:40 ? 00:00:00 qmgr -l -t unix -u
# netstat -ln | grep 25
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
# systemctl stop postfix
# systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
$ id postfix
uid=89(postfix) gid=89(postfix) groups=89(postfix),12(mail)
$ cat /etc/group | grep post
mail:x:12:postfix
postdrop:x:90:
postfix:x:89:
AWSのRoute53で、MXレコードを次のように登録しています。
$ nslookup -type=mx end0tknr.com
Non-authoritative answer:
end0tknr.com mail exchanger = 10 mailp.end0tknr.com.
/etc/postfix/main.cf の編集
$ sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.20210101
$ sudo vi /etc/postfix/main.cf
## 以下の mailp の「p」はpackageの意味です
old) # myhostname =
new) myhostname = mailp.end0tknr.com
old) # mydomain =
new) mydomain = end0tknr.com
## inet_interfacesは、リモートからの SMTP 接続OKとする為、allに
old) #inet_interfaces = all
old) inet_interfaces = localhost
new) inet_interfaces = all
new) #inet_interfaces = localhost
## inet_interfacesは、リモートからの SMTP 接続OKとする為、
## 0.0.0.0/0 を追加していますが、本来は良くない気がします。
old) #mynetworks = 127.0.0.0/8 172.31.0.167/32
new) mynetworks = 127.0.0.0/8 0.0.0.0/0
new) home_mailbox = Maildir/
## 自host宛のmailとして扱うドメイン?一覧
old) mydestination = $myhostname, localhost.$mydomain, localhost
new) mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## SMTP送信自体に直接の関係はありませんが、メールボックスはMaildir形式
old) # home_mailbox = Maildir/
new) home_mailbox = Maildir/
postconf コマンドによる postfix設定内容の確認
/sbin/postconf コマンドを引数なし、または 引数ありで実行すると、
default値を含めた設定内容を表示できます。
$ /sbin/postconf
2bounce_notice_recipient = postmaster
access_map_defer_code = 450
access_map_reject_code = 554
<略:対象に出力されます>
virtual_transport = virtual
virtual_uid_maps =
$ /sbin/postconf mydestination
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
$ sudo systemctl restart postfix
6. 動作確認
Gmail等からメール送信し、SMTP serverへの配信確認
先程までで、postfixの設定は完了していますので、
試しに ec2-user@endotknr.com 宛にGmail等から、メール送信して下さい。
メールの受信確認
その後、SMTP server へssh接続し、
/home/ec2-user/Maildir/new を見ると、受信したメールのファイルを確認できます。
$ ls -lh /home/ec2-user/Maildir/new
total 4.0K
-rw------- 1 ec2-user 2.4K Jan 14 17:01 1610611306.Vca01I3c00062M491241.mailp.end0tknr.com
/var/log/maillog の確認
配信されたログも確認できますので、以下のようにご確認をお願いします。
$ sudo less /var/log/maillog
SMTP client & server から、Gmailへの配信確認
mailコマンド install
$ sudo yum install mailx
$ which mail
/usr/bin/mail
mailコマンドでメール送信
SMTP clientへ、ssh後、SMTP clientから送信
$ echo "TEST MAIL MESSAGE BODY" | \
mail -v \
-s "TEST MAIL SUBJECT" \
-S smtp=smtp://mailp.end0tknr.com:25 \
-r test-mail@hogehoge.com \
ないしょ@gmail.com
SMTP server 自身(localhost)から送信
echo "TEST MAIL MESSAGE BODY" | \
mail -v \
-s "TEST MAIL SUBJECT" \
-S smtp=smtp://localhost:25 \
-r test-mail@hogehoge.com \
ないしょ@gmail.com