LoginSignup
2
3

More than 5 years have passed since last update.

VagrantのCentOS7からGmail経由でメール送信するために設定したこと

Last updated at Posted at 2019-02-13

背景

VagrantにCentOS7のLAMP環境を構築しました。
そして、あるCMSをインストールしていて、CMSからGmailのメールアドレスを使用して外へメールを送信したいとなりました。

Gmail経由でメールを送信するための設定を調べたり試行錯誤したりしたので、次回はすんなり行くように作業内容を残したいと思います。
もし、内容に変なところがありましたらご指摘のほどよろしくお願いいたします。

作業

以下作業は全てsu状態で行なった。

postfixをインストール

メール転送エージェント(MTA)はpostfixを使用したかったためインストール。

yum -y install postfix

MTA を Sendmail から Postfix に切り替え

Sendmailが使われていたのでPostfixに切り替えた。
/usr/sbin/sendmail.postfix の番号を選びEnterをします。

alternatives --config mta

postfix 起動

postfix を起動時に立ち上げるようにする。Sendmailは使わなくなりました。

systemctl stop sendmail
systemctl start postfix
systemctl disable sendmail
systemctl enable postfix

メール保存先のディレクトリ作成

mkdir -p /etc/skel/Maildir/{new,cur,tmp}
chmod -R 700 /etc/skel/Maildir/

/etc/postfix/main.cf 編集

cp /etc/postfix/main.cf /etc/postfix/main.cf.org
vim /etc/postfix/main.cf

以下のように編集

- #myhostname = host.domain.tld
+ myhostname = localhost.jp

- #mydomain = domain.tld
+ mydomain = localhost.jp

- #myorigin = $mydomain
+ myorigin = $mydomain

- #inet_interfaces = all
+ inet_interfaces = all

- mydestination = $myhostname, localhost.$mydomain, localhost
+ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

- #mynetworks_style = host
+ mynetworks = localhost, 192.168.33.10/24

- #home_mailbox = Maildir/
+ home_mailbox = Maildir/

ファイルの末尾に以下を追加

relayhost = [smtp.gmail.com]:587
#sasl setting
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
#tls setting
smtp_use_tls = yes
smtp_tls_CApath = /etc/pki/tls/certs/thawte_Premium_Server_CA.pem

/etc/postfix/sasl_passwd 作成

vim /etc/postfix/sasl_passwd

以下の内容を追記し保存

[smtp.gmail.com]:587 あなたのメールアドレス:パスワード

以下のコマンドで使えるようにする

chown root:root /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

smtp.gmail.com との連携には証明書が必要

wget https://www.thawte.com/roots/thawte_Premium_Server_CA.pem
mv thawte_Premium_Server_CA.pem /etc/pki/tls/certs/

cyrus-sasl-plain インストール

yum install cyrus-sasl-plain

安全性の低いアプリのアクセスを許可する

Gmail側の設定が必要です。
安全性の低いアプリのアクセスを許可を有効にしてあげてください。

テスト送信

echo test | mail メールアドレス

おわり

以上の設定で完了だと思います。
CMSからGmailのメールアドレスを使用して外へメールを送信できました。

参考

試行錯誤していた中で参考にさせていただいたサイトのリンクをご紹介いたします。

2
3
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
2
3