m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

CentOS7.xでGmailをpostfixでリレーしてメール送信する

CentOS6.xじゃない

微妙にコマンド異なるかもしれないので一応

環境

VMware Player
CentOS7.1だったと思う
iosをただ、インストールしただけ

Postfixをインストールしなくてもあった

[root@localhost ~]# which postfix
/usr/sbin/postfix
[root@localhost ~]#

ちなみにserviceで稼働中か確認しようとしたら

[root@localhost ~]# service postfix status
Redirecting to /bin/systemctl status  postfix.service
postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled)
   Active: active (running) since 水 2015-06-10 22:10:30 JST; 3min 28s ago
  Process: 1496 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1470 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1246 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2326 (master)
   CGroup: /system.slice/postfix.service
           tq2326 /usr/libexec/postfix/master -w
           tq2350 pickup -l -t unix -u
           mq2351 qmgr -l -t unix -u

 6月 10 22:10:23 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
 6月 10 22:10:30 localhost.localdomain postfix/master[2326]: daemon started -- version 2.10.1, configuration /etc/postfix
 6月 10 22:10:30 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
[root@localhost ~]#

※なんかserviceコマンドを使用すると「/bin/systemctl」にリダイレクト?している感じ

必要そうなパッケージをインストールして起動

yum -y install cyrus-sasl*
systemctl start saslauthd
systemctl enable saslauthd

telnetもない場合はインストール

yum install -y telnet
465で接続
[root@localhost ~]# telnet smtp.gmail.com 465
Trying 173.194.72.108...
Connected to smtp.gmail.com.
Escape character is '^]'.
ehlo localhost
Connection closed by foreign host.
[root@localhost ~]#

駄目っぽい

587で接続
[root@localhost ~]# telnet smtp.gmail.com 587
Trying 173.194.72.108...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 mx.google.com ESMTP q5sm8697791pde.56 - gsmtp
ehlo localhost
250-mx.google.com at your service, [180.46.248.13]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
quit
221 2.0.0 closing connection q5sm8697791pde.56 - gsmtp
Connection closed by foreign host.
[root@localhost ~]#

OK

postfix設定ファイルを編集

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

※末尾に追記

ファイルを作成
echo [smtp.gmail.com]:587 username@gmail.com:password > /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

※ユーザーとパスワードは自分のものを設定する

再起動
systemctl restart postfix

メール送信

メール送信用のパッケージをインストール
yum -y install mailx
メールを送信する
echo hogehoge | mail username@gmail.com

所感

サービスの登録や起動などのコマンドが変わっているので多少手間取りそうです…
実際、CentOS7に変化するタイミングがうまくつかめない。
今後はなるべくこちらを使用してみる方がいいのかな?