1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

開発環境のPostfixの設定

Posted at

開発環境でのメールに関する設定。
開発環境から送信したメールが外部に送信されないようにする。

環境はCentOS 7.3。

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

aliasを作成する

所定のアカウントにメールを転送するaliasを作成する。

$ sudo tee /etc/aliases.regexp <<EOT > /dev/null
> /(?!^root\$|^$USER\$)^.*\$/ $USER
> EOT

上記のコマンドを実行した結果。

[te2u@centos73 ~]$ cat /etc/aliases.regexp
/(?!^root$|^te2u$)^.*$/ te2u

rootもしくは自分へのメール以外を自分に転送する。

local転送に変更する

開発環境から送信するすべてのメールをlocal転送にする。

$ sudo tee /etc/postfix/transport_maps <<'EOT' > /dev/null
> /^.*@.*$/ local
> EOT

上記2つのファイルを適用する

現状の設定ファイルのバックアップを取り、上述の2つの設定を適用する変更を行う。

$ sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.org
$ sudoedit /etc/postfix/main.cf

適用前と適用後の差分。

$ diff -u /etc/postfix/main.cf.org /etc/postfix/main.cf
--- /etc/postfix/main.cf.org	2016-12-21 11:58:25.010525872 +0900
+++ /etc/postfix/main.cf	2016-12-21 11:59:22.709358250 +0900
@@ -383,9 +383,10 @@
 # "postfix reload" to eliminate the delay.
 #
 #alias_maps = dbm:/etc/aliases
-alias_maps = hash:/etc/aliases
+#alias_maps = hash:/etc/aliases
 #alias_maps = hash:/etc/aliases, nis:mail.aliases
 #alias_maps = netinfo:/aliases
+alias_maps = hash:/etc/aliases, pcre:/etc/aliases.regexp

 # The alias_database parameter specifies the alias database(s) that
 # are built with "newaliases" or "sendmail -bi".  This is a separate
@@ -677,3 +678,5 @@
 # readme_directory: The location of the Postfix README files.
 #
 readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
+
+transport_maps = pcre:/etc/postfix/transport_maps

変更内容を適用する。

$ sudo systemctl reload postfix.service

確認する

mailコマンドで設定が正しいか確認する。

mailコマンドがないときはmailxをインストールする。

sudo yum install mailx

mailコマンドでメールを送信する。

$ echo 'test' | mail -s 'test' test@example.com

正しく設定されていれば先ほど送信したメールが自分自身宛にきているので、それを確認する。

$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/te2u": 1 message 1 new
>N  1 te2u@centos73.vm      Wed Dec 21 12:02  21/669   "test"
& 1
Message  1:
From owner-test@centos73.vm  Wed Dec 21 12:02:18 2016
Return-Path: <owner-test@centos73.vm>
X-Original-To: te2u@centos73.vm
Delivered-To: te2u@centos73.vm
Delivered-To: test@example.com
Date: Wed, 21 Dec 2016 12:02:18 +0900
To: test@example.com
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: te2u@centos73.vm
Status: R

test

参考

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?