Skip to content

AWS上のpostfixでメールを受信してみる

mechamogera edited this page Feb 21, 2017 · 10 revisions

[2015/06/15追記]この記事の構築手順はCloudFormationで自動化しています。 http://qiita.com/mechamogera/items/f3d5411e55afd4f74db6

一時的な利用程度しか考えていません。 恒久的にpostfixを動かす場合は設定ファイルのセキュリティ面のチェックなどが必要です。

AWS環境

  • EC2インスタンス
  • AMI:Amazon Linux AMI 2012.09
  • SecurityGroup:22番と25番ポート許可
  • レジストラでexample.comドメイン取得(ダミーです)
  • Route53でHostedZone「example.com」作成
  • Aレコード:mail.example.com(valueはec2のIP)
  • MXレコード:mail.example.com(valueは10 mail.example.comに)

手順

  1. postfix導入
$ sudo yum update -y
$ sudo yum install postfix -y
  1. postfix設定
$ sudo cp /etc/postfix/main.cf{,.org}
$ sudo vi /etc/postfix/main.cf
$ sudo diff -u /etc/postfix/main.cf{.org,}
--- /etc/postfix/main.cf.org	2013-01-20 06:44:10.831132999 +0000
+++ /etc/postfix/main.cf	2013-01-20 12:21:11.669057494 +0000
@@ -74,6 +74,7 @@
 #
 #myhostname = host.domain.tld
 #myhostname = virtual.domain.tld
+myhostname = mail.example.com
 
 # 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 = example.com
 
 # SENDING MAIL
 # 
@@ -97,6 +99,7 @@
 #
 #myorigin = $myhostname
 #myorigin = $mydomain
+myorigin = $mydomain
 
 # RECEIVING MAIL
 
@@ -113,10 +116,10 @@
 #inet_interfaces = all
 #inet_interfaces = $myhostname
 #inet_interfaces = $myhostname, localhost
-inet_interfaces = localhost
+inet_interfaces = all
 
 # Enable IPv4, and IPv6 if supported
-inet_protocols = all
+inet_protocols = ipv4
 
 # The proxy_interfaces parameter specifies the network interface
 # addresses that this mail system receives mail on by way of a
@@ -161,7 +164,7 @@
 #
 # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
 #
-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 +420,7 @@
 #
 #home_mailbox = Mailbox
 #home_mailbox = Maildir/
+home_mailbox = Maildir/
  
 # The mail_spool_directory parameter specifies the directory where
 # UNIX-style mailboxes are kept. The default setting depends on the
@@ -568,6 +572,7 @@
 #
 #smtpd_banner = $myhostname ESMTP $mail_name
 #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
+smtpd_banner = $myhostname ESMTP unknown
 
 # PARALLEL DELIVERY TO THE SAME DESTINATION
 #
@@ -674,3 +679,12 @@
 # readme_directory: The location of the Postfix README files.
 #
 readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES
+
+smtpd_sasl_auth_enable = yes
+smtpd_sasl_local_domain = $myhostname
+smtpd_recipient_restrictions =
+    permit_mynetworks
+    permit_sasl_authenticated
+    reject_unauth_destination
+
+message_size_limit = 10485760
  1. システムのアカウントを利用するように設定 (Ubuntuの場合?)
sudo apt-get install sasl2-bin
$ sudo /etc/rc.d/init.d/saslauthd start
$ sudo chkconfig saslauthd on
  1. postfixに切り替え
$ sudo /etc/rc.d/init.d/sendmail stop
$ sudo chkconfig sendmail off
$ sudo alternatives --config mta
# => postfix選択
$ sudo /etc/rc.d/init.d/postfix start
$ sudo chkconfig postfix on
  1. ユーザ作成

ユーザ名とパスワードがいい加減なので気をつけて下さい

$ sudo mkdir -p /etc/skel/Maildir/{new,cur,tmp}
$ sudo chmod -R 700 /etc/skel/Maildir/
$ sudo useradd test
$ sudo passwd test
$ su - test
# => ~/Maildirが作成されていることを確認
  1. メール受信確認
  • gmailからtest@mail.example.comにメールを送信してみる
  • => 受信OK(/home/test/Maildir/newの中にメールに対応するファイルが作成されている)
  1. 特定のアドレス当てのメールを1つのアカウントで受け取るように設定
$ sudo cp /etc/postfix/main.cf{,.bak}
$ sudo vi /etc/postfix/main.cf
$ sudo diff -u  /etc/postfix/main.cf{.bak,}
--- /etc/postfix/main.cf.bak	2013-01-21 08:36:10.046499956 +0000
+++ /etc/postfix/main.cf	2013-01-21 08:37:51.630390403 +0000
@@ -386,7 +386,7 @@
 # "postfix reload" to eliminate the delay.
 #
 #alias_maps = dbm:/etc/aliases
-alias_maps = hash:/etc/aliases
+alias_maps = hash:/etc/aliases,regexp:/etc/postfix/aliases.reg
 #alias_maps = hash:/etc/aliases, nis:mail.aliases
 #alias_maps = netinfo:/aliases
$ sudo vi /etc/postfix/aliases.reg
$ sudo cat /etc/postfix/aliases.reg
/^test-[^@]+(@.*)?$/ test
$ sudo /etc/init.d/postfix reload

メール転送

  • 受信したメールを転送したい時の設定
$ sudo vi /etc/aliases
admin:         [転送先e-mail]
$ sudo newaliases

参照サイト

Clone this wiki locally