ドメインを取得したはいいけど、これでどうやったらメールを送受信できるようになるの?
という人のために、取得したドメインでメールを送受信するための手順をまとめました。
今回は、「wegirls.techというドメインを取得して、info@wegirls.techというメールアドレスでメールを送受信できるようにする」ということが目標です。
※ちなみに、info@wegirls.tech というメールアドレスは有効ではないので、お問い合わせなどはTwitterかコメントでお願いいたします。
手順はこちら。準備が整っている人は、6番あたりまで飛ばしても大丈夫かと思います。
- ドメインを取得する
- メールサーバーとなるサーバーを用意する
- ドメインのDNSを変更する
- サーバーにユーザーを追加する
- サーバーにpostfixをインストールする
- postfixのmain.cfを編集する
- local_recipientsに、メールを受信するユーザー情報を記載
- postfixを再起動する
使用したサーバー、ソフトウェアなどは、「使い慣れていたから」が主な理由です。ほかにも選択肢は色々あるので、一例としてご覧ください。
ドメインを取得する
ドメイン取得サービスを使って、ドメインを取得します。
今回は、「wegirls.tech」というドメインを取得したと仮定して進めますので、適宜置き換えてください。
ドメインの取得とDNS設定が行えれば何でも大丈夫です(DNS設定は別サービスでも大丈夫)。
わたしはお名前.comを使いました。
→サイトはこちら:お名前.com
ほかにもドメイン取得サービスは色々あり、例えばこんなのがオススメです。
- .moeドメイン
・・・アニメ系におすすめ、「.moe(=萌え)」ドメインが取得できる。
- 年間920円(税抜)からの格安ドメイン取得サービス─ムームードメイン─
・・・利用者が多いので、困った時もググって解決できる。
- バリューサーバー【まるっとプラン】
・・・徹底的に安くおさえたい人向け。ドメイン+サーバーで月額133円~!
メールサーバーとなるサーバーを用意する
今回は、Digital Oceanで、CentOS6.8(64bit)のサーバーを用意しました。
このサーバーのIPを「123.456.789.012」と仮定して進めますので、適宜置き換えてください。
Digital Oceanは海外のサービスですが、ほかにもレンタルサーバーのサービスは色々あります。
ドメインのDNSを変更する
取得したドメインのDNSを変更します。
AレコードとMXレコードの2つを指定します。
まず、Aレコードを指定
下記のように、Aレコードを指定します。
- ホスト名:mail.wegirls.tech
- TYPE:A
- VALUE:123.456.789.012
青色部分は、ご自身のドメインやサーバーIPに置き換えてくださいね!
次に、MXレコードを指定
次に、MXレコードを指定します。
- ホスト名:wegirls.tech
- TYPE:MX
- VALUE:mail.wegirls.tech
- Priority(優先):10
これでDNS設定は完了です。
場合によっては、DNS設定の変更が有効になるまで72時間くらいかかるので、余裕をもってやっておきましょう。
お名前.comでは、TTLを300などにしたらすぐ変更されていました。
サーバーにユーザーを追加する
メールを受け取りたいユーザーを、サーバーに追加します。
今回は、info@wegirls.techというメールアドレスを利用したいので、infoというユーザーを追加します。
[root@centos ~]# useradd info
★わたしはこの手順をすっ飛ばしてしまい、Unknown User・・・というエラーになってしまいました。
下記のようなエラーが出る方は、この手順のところを確認してみてください。
This is the mail system at host wegirls.tech. I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below. For further assistance, please send mail to postmaster. If you do so, please include this problem report. You can delete your own text from the attached returned message. The mail system <info@wegirls.tech>: unknown user: "info" Final-Recipient: rfc822; info@wegirls.tech Original-Recipient: rfc822;info@wegirls.tech Action: failed Status: 5.1.1 Diagnostic-Code: X-Postfix; unknown user: "info"
サーバーにpostfixをインストールする
下記のようなコマンドを実行してみて、コマンドが使えなければpostfixをインストールします。
[root@centos ~]# postconf mail_version
[root@centos ~]# yum -y install postfix
※(予備知識)postfixとは?
Postfix(ポストフィックス)はフリーソフトウェア・オープンソースソフトウェアのメール転送エージェント(MTA)である。 Sendmailとの操作上の互換性を確保しつつ、管理・設定が容易で、高速・安全であることを指向して開発されている。
postfixのmain.cf
を編集する
編集するmain.cfの場所はこちら。
/etc/postfix/main.cf
ちゃんとバックアップをとってから、
[root@centos ~]# cp /etc/postfix/main.cf /etc/postfix/main.cf.bk
下記のコマンドで、main.cfを開き、入力モードでファイルの中身を変更していきます。
[root@centos ~]# vim /etc/postfix/main.cf
myhostnameを変更
# INTERNET HOST AND DOMAIN NAMES # # The myhostname parameter specifies the internet hostname of this # mail system. The default is to use the fully-qualified domain name # from gethostname(). $myhostname is used as a default value for many # other configuration parameters. # #myhostname = host.domain.tld #myhostname = virtual.domain.tld #myhostname = host.domain.tld #myhostname = virtual.domain.tld
ここのしたに、
myhostname = wegirls.tech
こう追記します。他のmyhostnameが有効になっていたら、#をつけてコメントアウトしておきましょう。
mydomainを変更
# The mydomain parameter specifies the local internet domain name. # The default is to use $myhostname minus the first component. # $mydomain is used as a default value for many other configuration # parameters. # #mydomain = domain.tld #mydomain = domain.tld
ここのしたに、
mydomain = wegirls.tech
こう追記します。他のmydomainが有効になっていたら、#をつけてコメントアウトしておきましょう。
inet_interfacesを変更する
そのままだとローカルのメールしか受信しないようになっているので、外部から受信できるように変更します。
# RECEIVING MAIL # The inet_interfaces parameter specifies the network interface # addresses that this mail system receives mail on. By default, # the software claims all active interfaces on the machine. The # parameter also controls delivery of mail to user@[ip.address]. # # See also the proxy_interfaces parameter, for network addresses that # are forwarded to us via a proxy or network address translator. # # Note: you need to stop/start Postfix when this parameter changes. # #inet_interfaces = all #inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost inet_interfaces = localhost
となっているところを、一番下の部分、inet_interfacesをlocalhostからallに変更します。
送信元の変更
# SENDING MAIL # # The myorigin parameter specifies the domain that locally-posted # mail appears to come from. The default is to append $myhostname, # which is fine for small sites. If you run a domain with multiple # machines, you should (1) change this to $mydomain and (2) set up # a domain-wide alias database that aliases each user to # user@that.users.mailhost. # # For the sake of consistency between sender and recipient addresses, # myorigin also specifies the default domain name that is appended # to recipient addresses that have no @domain part. # #myorigin = $myhostname #myorigin = $mydomain #myorigin = $mydomain
これを、
myorigin = $mydomain
となるように#をはずして有効にしておきます。
local_recipient_mapsの変更
#local_recipient_maps = unix:passwd.byname $alias_maps #local_recipient_maps = proxy:unix:passwd.byname $alias_maps #local_recipient_maps =
ここのしたに、
local_recipient_maps = $alias_maps btree:/etc/postfix/local_recipients
こう追記します。
ここまで全て変更したら、コマンドモードで:wp!
で保存してファイルを閉じます。
local_recipientsに、メールを受信するユーザー情報を記載
info@wegirls.techのアドレスだけメールを受信できるようにします(手当たり次第にスパムがたくさん送られることを防ぐ)。
下記のコマンドでファイルを開きます。
[root@centos ~]# vim /etc/postfix/local_recipients
下記を追記(もしくは新規作成)して、コマンドモードで:wp!
で保存してファイルを閉じます。
info@wegirls.tech OK
postfixを再起動する
その前に、postmapコマンドでDBを一発作成します。
[root@centos ~]# postmap btree:/etc/postfix/local_recipients
postfixを再起動して、変更した設定を読み込ませます。
[root@centos ~]# service postfix restart
下記のように再起動が完了したら、メールを送受信できるようになっています!
Shutting down postfix: [ OK ] Starting postfix: [ OK ]
やったね!
もっと詳しくpostfixやメールサーバーについて知りたい人は・・・

- 作者: Kyle D. Dent,菅野良二
- 出版社/メーカー: オライリージャパン
- 発売日: 2004/08/15
- メディア: 大型本
- 購入: 3人 クリック: 38回
- この商品を含むブログ (16件) を見る

Postfix 辞典 (DESKTOP REFERENCE)
- 作者: とみたまさひろ
- 出版社/メーカー: 翔泳社
- 発売日: 2006/06/21
- メディア: 単行本
- クリック: 14回
- この商品を含むブログ (7件) を見る
【参考サイト】
小悪魔女子大生のサーバエンジニア日記 » Blog Archive » postfixで外部からメールを受信できるようにする
メールの設定に関する基本的な考え方がとてもわかりやすいです。
Postfixをインストールしてメール送信してみる | 本日も乙
試しにメールを送信してみたいときに使える、コマンド例もあります。
メールサーバー構築(Postfix+Dovecot) - CentOSで自宅サーバー構築
今回は紹介していませんが、SMTPの設定まわりで参考にさせていただきました。