この記事は約39分40秒で読むことができます。

docker postfix ssmtp install gmail

root login


[oracle@centos7 ~]$ docker exec --interactive --tty --user root --workdir / orcl_12cr2 bash

postfix intall


bash-4.2# yum install -y postfix
Loaded plugins: ovl
epel/x86_64/metalink                                                                                                           | 9.6 kB  00:00:00     
epel                                                                                                                           | 3.2 kB  00:00:00     
ol7_UEKR4                                                                                                                      | 1.2 kB  00:00:00     
ol7_latest                                                                                                                     | 1.4 kB  00:00:00     
osquery-s3-centos7-repo                                                                                                        |  951 B  00:00:00     
(1/2): epel/x86_64/updateinfo                                                                                                  | 942 kB  00:00:02     
(2/2): epel/x86_64/primary                                                                                                     | 3.6 MB  00:00:04     
epel                                                                                                                                      12776/12776
Resolving Dependencies
--> Running transaction check
---> Package postfix.x86_64 2:2.10.1-7.el7 will be installed
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================
 Package                             Arch                          Version                                    Repository                         Size
======================================================================================================================================================
Installing:
 postfix                             x86_64                        2:2.10.1-7.el7                             ol7_latest                        2.4 M
Installing for dependencies:
 mariadb-libs                        x86_64                        1:5.5.60-1.el7_5                           ol7_latest                        758 k

Transaction Summary
======================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 3.2 M
Installed size: 17 M
Downloading packages:
(1/2): mariadb-libs-5.5.60-1.el7_5.x86_64.rpm                                                                                  | 758 kB  00:00:05     
(2/2): postfix-2.10.1-7.el7.x86_64.rpm                                                                                         | 2.4 MB  00:00:07     
------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                 457 kB/s | 3.2 MB  00:00:07     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                               1/2 
  Installing : 2:postfix-2.10.1-7.el7.x86_64                                                                                                      2/2 
failed to link /usr/share/man/man1/mailq.1.gz -> /etc/alternatives/mta-mailqman: No such file or directory
failed to link /usr/share/man/man1/newaliases.1.gz -> /etc/alternatives/mta-newaliasesman: No such file or directory
failed to link /usr/share/man/man8/sendmail.8.gz -> /etc/alternatives/mta-sendmailman: No such file or directory
failed to link /usr/share/man/man5/aliases.5.gz -> /etc/alternatives/mta-aliasesman: No such file or directory
  Verifying  : 2:postfix-2.10.1-7.el7.x86_64                                                                                                      1/2 
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                               2/2 

Installed:
  postfix.x86_64 2:2.10.1-7.el7                                                                                                                       

Dependency Installed:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                                                                                                                

Complete!
bash-4.2# 

mailx cyrus-sasl-plain install


bash-4.2# yum install -y mailx cyrus-sasl-plain
Loaded plugins: ovl
Package mailx-12.5-19.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package cyrus-sasl-plain.x86_64 0:2.1.26-23.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================
 Package                                 Arch                          Version                                Repository                         Size
======================================================================================================================================================
Installing:
 cyrus-sasl-plain                        x86_64                        2.1.26-23.el7                          ol7_latest                         38 k

Transaction Summary
======================================================================================================================================================
Install  1 Package

Total download size: 38 k
Installed size: 39 k
Downloading packages:
cyrus-sasl-plain-2.1.26-23.el7.x86_64.rpm                                                                                      |  38 kB  00:00:07     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : cyrus-sasl-plain-2.1.26-23.el7.x86_64                                                                                              1/1 
  Verifying  : cyrus-sasl-plain-2.1.26-23.el7.x86_64                                                                                              1/1 

Installed:
  cyrus-sasl-plain.x86_64 0:2.1.26-23.el7                                                                                                             

Complete!

/etc/postfix/main.cfの編集


bash-4.2# vim /etc/postfix/main.cf


# The relayhost parameter specifies the default host to send mail to
# when no entry is matched in the optional transport(5) table. When
# no relayhost is given, mail is routed directly to the destination.
#
# On an intranet, specify the organizational domain name. If your
# internal DNS uses no MX records, specify the name of the intranet
# gateway host instead.
#
# In the case of SMTP, specify a domain, host, host:port, [host]:port,
# [address] or [address]:port; the form [host] turns off MX lookups.
#
# If you're connected via UUCP, see also the default_transport parameter.
#
#relayhost = $mydomain
#relayhost = [gateway.my.domain]
#relayhost = [mailserver.isp.tld]
#relayhost = uucphost
#relayhost = [an.ip.add.ress]
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

root mailをgmailに転送設定する


bash-4.2# vim /etc/aliases
# Person who should get root's mail
#root:          marc
root:           メールアドレス@gmail.com

/etc/postfix/sasl_passwdの編集


bash-4.2# vim /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 メールアドレス@gmail.com:password

/etc/postfix/sasl_passwdの権限設定


bash-4.2# cd /etc/postfix/
bash-4.2# pwd
/etc/postfix
bash-4.2# ls -lt
total 152
-rw-r--r--. 1 root root    62 Dec 30 00:38 sasl_passwd
-rw-r--r--. 1 root root 27462 Dec 30 00:33 main.cf
-rw-r--r--. 1 root root 20876 Aug 24 06:16 access
-rw-r--r--. 1 root root 11883 Aug 24 06:16 canonical
-rw-r--r--. 1 root root 10106 Aug 24 06:16 generic
-rw-r--r--. 1 root root 21545 Aug 24 06:16 header_checks
-rw-r--r--. 1 root root  6105 Aug 24 06:16 master.cf
-rw-r--r--. 1 root root  6816 Aug 24 06:16 relocated
-rw-r--r--. 1 root root 12549 Aug 24 06:16 transport
-rw-r--r--. 1 root root 12696 Aug 24 06:16 virtual
bash-4.2# chmod 600 /etc/postfix/sasl_passwd
bash-4.2# ls -lt
total 152
-rw-------. 1 root root    62 Dec 30 00:38 sasl_passwd
-rw-r--r--. 1 root root 27462 Dec 30 00:33 main.cf
-rw-r--r--. 1 root root 20876 Aug 24 06:16 access
-rw-r--r--. 1 root root 11883 Aug 24 06:16 canonical
-rw-r--r--. 1 root root 10106 Aug 24 06:16 generic
-rw-r--r--. 1 root root 21545 Aug 24 06:16 header_checks
-rw-r--r--. 1 root root  6105 Aug 24 06:16 master.cf
-rw-r--r--. 1 root root  6816 Aug 24 06:16 relocated
-rw-r--r--. 1 root root 12549 Aug 24 06:16 transport
-rw-r--r--. 1 root root 12696 Aug 24 06:16 virtual


bash-4.2# ls -lt
total 152
-rw-------. 1 root root    62 Dec 30 00:38 sasl_passwd
-rw-r--r--. 1 root root 27462 Dec 30 00:33 main.cf
-rw-r--r--. 1 root root 20876 Aug 24 06:16 access
-rw-r--r--. 1 root root 11883 Aug 24 06:16 canonical
-rw-r--r--. 1 root root 10106 Aug 24 06:16 generic
-rw-r--r--. 1 root root 21545 Aug 24 06:16 header_checks
-rw-r--r--. 1 root root  6105 Aug 24 06:16 master.cf
-rw-r--r--. 1 root root  6816 Aug 24 06:16 relocated
-rw-r--r--. 1 root root 12549 Aug 24 06:16 transport
-rw-r--r--. 1 root root 12696 Aug 24 06:16 virtual
bash-4.2# postmap /etc/postfix/sasl_passwd
postmap: fatal: parameter inet_interfaces: no local interface found for ::1
bash-4.2# vim /etc/hosts
bash-4.2# postmap /etc/postfix/sasl_passwd
bash-4.2# ls -lt
total 160
-rw-------. 1 root root 12288 Dec 30 00:44 sasl_passwd.db
-rw-------. 1 root root    62 Dec 30 00:38 sasl_passwd
-rw-r--r--. 1 root root 27462 Dec 30 00:33 main.cf
-rw-r--r--. 1 root root 20876 Aug 24 06:16 access
-rw-r--r--. 1 root root 11883 Aug 24 06:16 canonical
-rw-r--r--. 1 root root 10106 Aug 24 06:16 generic
-rw-r--r--. 1 root root 21545 Aug 24 06:16 header_checks
-rw-r--r--. 1 root root  6105 Aug 24 06:16 master.cf
-rw-r--r--. 1 root root  6816 Aug 24 06:16 relocated
-rw-r--r--. 1 root root 12549 Aug 24 06:16 transport
-rw-r--r--. 1 root root 12696 Aug 24 06:16 virtual
bash-4.2# 

/etc/hostsを修正する

変更前


127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      609a69bc0b21

変更後


127.0.0.1       localhost
#::1    localhost ip6-localhost ip6-loopback
#fe00::0        ip6-localnet
#ff00::0        ip6-mcastprefix
#ff02::1        ip6-allnodes
#ff02::2        ip6-allrouters
172.17.0.2      609a69bc0b21

postfixの起動


bash-4.2# postfix restart
postfix/postfix-script: error: unknown command: 'restart'
postfix/postfix-script: fatal: usage: postfix start (or stop, reload, abort, flush, check, status, set-permissions, upgrade-configuration)
bash-4.2# postfix start
postfix/postfix-script: starting the Postfix mail system
bash-4.2# postfix status
postfix/postfix-script: the Postfix mail system is running: PID: 381
bash-4.2# 

メールのテスト送信


bash-4.2# echo "hoge" | mail -s "test mail from docker" ${自身のGmailメールアドレス}

postfixだとできなかった!
ssmtpで挑戦!

ssmtpのインストール


bash-4.2# yum install -y ssmtp
Loaded plugins: ovl
Resolving Dependencies
--> Running transaction check
---> Package ssmtp.x86_64 0:2.64-14.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================
 Package                           Arch                               Version                                  Repository                        Size
======================================================================================================================================================
Installing:
 ssmtp                             x86_64                             2.64-14.el7                              epel                              50 k

Transaction Summary
======================================================================================================================================================
Install  1 Package

Total download size: 50 k
Installed size: 74 k
Downloading packages:
ssmtp-2.64-14.el7.x86_64.rpm                                                                                                   |  50 kB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ssmtp-2.64-14.el7.x86_64                                                                                                           1/1 
failed to link /usr/share/man/man1/mailq.1.gz -> /etc/alternatives/mta-mailqman: No such file or directory
failed to link /usr/share/man/man1/newaliases.1.gz -> /etc/alternatives/mta-newaliasesman: No such file or directory
failed to link /usr/share/man/man5/aliases.5.gz -> /etc/alternatives/mta-aliasesman: No such file or directory
failed to link /usr/share/man/man8/sendmail.8.gz -> /etc/alternatives/mta-sendmailman: No such file or directory
warning: %post(ssmtp-2.64-14.el7.x86_64) scriptlet failed, exit status 2
Non-fatal POSTIN scriptlet failure in rpm package ssmtp-2.64-14.el7.x86_64
  Verifying  : ssmtp-2.64-14.el7.x86_64                                                                                                           1/1 

Installed:
  ssmtp.x86_64 0:2.64-14.el7                                                                                                                          

Complete!

/etc/ssmtp/ssmtp.confの編集


bash-4.2# sudo vim /etc/ssmtp/ssmtp.conf

編集内容


root=${自身のGmailメールアドレス}
mailhub=smtp.gmail.com:587
RewriteDomain=gmail.com
hostname=gmail.com
AuthUser=${自身のGmailメールアドレス}
AuthPass=${自分のGmailログインパスワード}
AuthMethod=LOGIN
UseTLS=YES
FromLineOverride=YES

root=${自身のGmailメールアドレス}


# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=${自身のGmailメールアドレス}

mailhub=smtp.gmail.com:587


# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and your mailhub is so named.
#mailhub=mail
mailhub=smtp.gmail.com:587

RewriteDomain=gmail.com


# Where will the mail seem to come from?
#RewriteDomain=
RewriteDomain=gmail.com

Hostname=gmail.com


# The full hostname
#Hostname=
Hostname=gmail.com
AuthUser=${自身のGmailメールアドレス}
AuthPass=${自分のGmailログインパスワード}
AuthMethod=LOGIN

UseTLS=YES


# Use SSL/TLS to send secure messages to server.
#UseTLS=YES
UseTLS=YES

FromLineOverride=YES


# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
#FromLineOverride=YES
FromLineOverride=YES

以下のURLから「安全性の低いアプリ」を許可する
https://myaccount.google.com/lesssecureapps
2段階認証の場合は許可できない。

メールのテスト送信


bash-4.2# echo "hoge" | mail -s "test mail from docker" ${自身のGmailメールアドレス}

できた!!

Leave a Reply

Your email address will not be published. Required fields are marked *