LoginSignup
28
43

More than 5 years have passed since last update.

CentOS7でmailコマンドでgmailにメール送信できるようにする

Last updated at Posted at 2018-01-08

概要

CentOS7で、mailコマンドでgmailにメール送信できるようにする手順です。

メールが送信できない理由

送信できない理由は、以下の3つでした。

  • 送信ポートを587番にしなければならない
  • SMTP認証が必要
  • googleが信頼性の低いアプリからのメールを受け付けない

gmailの送信ポートを確認

gmailのsmtpサーバーの空いているポートを調べてみます。

25番を確認

$ telnet smtp.gmail.com 25
Trying 74.125.204.109...

587番を確認

$ telnet smtp.gmail.com 587
Trying 64.233.189.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP w21sm23015592pfl.50 - gsmtp

確認した結果、587番ポートしか空いていないことが分かりました。

localhost

localhostの開いているポートを確認してみる

25番

$ telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 localhost.localdomain ESMTP Postfix

587番

$ telnet localhost 587
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

localhostは25番しか空いていないので、postfixの送信ポートを変更する必要があります。

postfixの設定変更

postfixで587番ポートで、SMTP認証が使用できるように設定変更をします。

Cyrus SASLライブラリのインストール

まず、PostfixのSMTP認証で利用するCyrus SASLライブラリをインストールします。

$ sudo yum install cyrus-sasl-plain cyrus-sasl-md5

postfixの設定ファイルを編集

$ sudo vi /etc/postfix/main.cf

以下を末尾に追記

# gmail
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain

SMTP認証の設定

以下のファイルにgmailのアカウントを記述します。

$ sudo vi /etc/postfix/sasl_password

中継先SMTPアドレス:ポート 認証ユーザ名:パスワード の書式で記述します。

[smtp.gmail.com]:587    <gmail_address>:<password>

パーミッションの変更

$ sudo chmod 600 /etc/postfix/sasl_password

dbファイルの生成

$ sudo postmap hash:/etc/postfix/sasl_password

postfix再起動

$ sudo systemctl restart postfix

mailコマンドのインストール

$ sudo yum -y install mailx

メールを送信してみる

$ echo hogehoge | mail username@gmail.com

送信できなかったのでログを確認すると、以下のようなものが出力されています。

/var/log/maillog

Please log in via your web browser and?534-5.7.14 then try again.?534-5.7.14

googleの設定変更

googleで安全性の低いアプリを許可しないと、送信することが出来ないようです。
https://myaccount.google.com/lesssecureapps

これで送信できるようになりました。

$ echo hogehoge | mail username@gmail.com

参考

28
43
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
28
43