


シェルスクリプトなどでメール送信する場合、mailコマンドではsmtpサーバの設定などが出来なくて困ることがあります。
私はいつもmuttコマンドを利用していますが今回は難儀したのでそのメモ。
環境
・CentOS Linux release 8.2.2004 (Core)
・kernel 4.18.0-193.28.1.el8_2.x86_64
- muttのインストール
- 設定ファイルの編集
- テストメール送信
- デバック
- muttのSTARTTLSを無効
- テストメール
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | [root@centos8 ~]# dnf info mutt Available Packages Name         : mutt Epoch        : 5 Version      : 1.10.1 Release      : 2.el8 Architecture : x86_64 Size         : 1.5 M Source       : mutt-1.10.1-2.el8.src.rpm Repository   : AppStream Summary      : A text mode mail user agent URL          : http://www.mutt.org License      : GPLv2+ and Public Domain Description  : Mutt is a small but very powerful text-based MIME mail client.  Mutt              : is highly configurable, and is well suited to the mail power user with              : advanced features like key bindings, keyboard macros, mail threading,              : regular expression searches and a powerful pattern matching language              : for selecting groups of messages. [root@centos8 ~]# dnf -y install mutt Dependencies resolved. ================================================================================================  Package             Architecture  Version                               Repository        Size ================================================================================================ Installing:  mutt                x86_64        5:1.10.1-2.el8                        AppStream        1.5 M Installing dependencies:  mailcap             noarch        2.1.48-3.el8                          BaseOS            39 k  tokyocabinet        x86_64        1.4.48-10.el8                         AppStream        486 k  urlview             x86_64        0.9-23.20131022git08767a.el8          AppStream         34 k Transaction Summary ================================================================================================ Install  4 Packages Total download size: 2.1 M Installed size: 7.6 M Downloading Packages: (1/4): urlview-0.9-23.20131022git08767a.el8.x86_64.rpm          371 kB/s |  34 kB     00:00 (2/4): mailcap-2.1.48-3.el8.noarch.rpm                          397 kB/s |  39 kB     00:00 (3/4): tokyocabinet-1.4.48-10.el8.x86_64.rpm                    1.6 MB/s | 486 kB     00:00 (4/4): mutt-1.10.1-2.el8.x86_64.rpm                             938 kB/s | 1.5 MB     00:01 ------------------------------------------------------------------------------------------------ Total                                                           648 kB/s | 2.1 MB     00:03 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction   Preparing        :                                                                        1/1   Installing       : mailcap-2.1.48-3.el8.noarch                                            1/4   Installing       : urlview-0.9-23.20131022git08767a.el8.x86_64                            2/4   Installing       : tokyocabinet-1.4.48-10.el8.x86_64                                      3/4   Running scriptlet: tokyocabinet-1.4.48-10.el8.x86_64                                      3/4   Installing       : mutt-5:1.10.1-2.el8.x86_64                                             4/4   Running scriptlet: mutt-5:1.10.1-2.el8.x86_64                                             4/4   Verifying        : mutt-5:1.10.1-2.el8.x86_64                                             1/4   Verifying        : tokyocabinet-1.4.48-10.el8.x86_64                                      2/4   Verifying        : urlview-0.9-23.20131022git08767a.el8.x86_64                            3/4   Verifying        : mailcap-2.1.48-3.el8.noarch                                            4/4 Installed:   mailcap-2.1.48-3.el8.noarch               mutt-5:1.10.1-2.el8.x86_64   tokyocabinet-1.4.48-10.el8.x86_64         urlview-0.9-23.20131022git08767a.el8.x86_64 Complete! | 
設定ファイルは/etc/Muttrcと/etc/Muttrc.localがあります。
編集は/etc/Muttrc.localか或いは新規ファイルを作成して送信時に指定します。
今回はアラートメール専用の設定ファイルを作成してみます。
| 1 2 3 4 5 6 | [root@centos8 ~]# vi /etc/Muttrc.Alert [root@centos8 ~]# cat /etc/Muttrc.Alert # Local configuration for Mutt. set realname="Alert Mail" set from="Alert@example.co.jp" set smtp_url="smtp://192.168.1.1:25" | 
| 1 | [root@centos8 ~]# echo Alert | mutt -F /etc/Muttrc.Alert -s "Alert message" alert-mail@example.co.jp | 
何度テストしてもメールが届きません。
muttのコマンドを確認するとデバックレベルが指定できて~/.muttdebug0に記録されるとありました。
-d level
If mutt was compiled with +DEBUG log debugging output to ~/.muttdebug0. Level can range from 1-5 and effects verbosity. A value of 2 is recommended.
レベル2でデバックするとメールサーバがSTARTTLSを開始していました。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@centos8 ~]# echo Alert | mutt -d 2 -F /etc/Muttrc.Alert -s "Alert message" alert-mail@example.co.jp [root@centos8 ~]# cat .muttdebug0 [2020-12-02 11:35:50] Mutt/1.10.1 (2018-07-13) debugging at level 2 [2020-12-02 11:35:50] getdnsdomainname(): example.co.jp [2020-12-02 11:35:50] Reading configuration file '/etc/Muttrc'. [2020-12-02 11:35:50] Reading configuration file '/etc/Muttrc.local'. [2020-12-02 11:35:50] Reading configuration file '/etc/Muttrc.Alert'. [2020-12-02 11:35:50] Connected to 192.168.1.1:25 on fd=6 [2020-12-02 11:35:50] 6< 220 mail.example.co.jp ESMTP [2020-12-02 11:35:50] 6> EHLO centos8.example.co.jp [2020-12-02 11:35:50] 6< 250-mail.example.co.jp [2020-12-02 11:35:50] 6< 250-PIPELINING [2020-12-02 11:35:50] 6< 250-SIZE 20480000 [2020-12-02 11:35:50] 6< 250-ETRN [2020-12-02 11:35:50] 6< 250-STARTTLS [2020-12-02 11:35:50] 6< 250-AUTH PLAIN LOGIN [2020-12-02 11:35:50] 6< 250-AUTH=PLAIN LOGIN [2020-12-02 11:35:50] 6< 250-ENHANCEDSTATUSCODES [2020-12-02 11:35:50] 6< 250-8BITMIME [2020-12-02 11:35:50] 6< 250 DSN [2020-12-02 11:35:50] 6> STARTTLS [2020-12-02 11:35:50] 6< 220 2.0.0 Ready to start TLS | 
Muttrcのドキュメントを見るとデフォルトでSTARTTLSが有効になっていました。
ssl_starttls
Type: quadoption
Default: yesIf set (the default), mutt will attempt to use STARTTLS on servers advertising the capability. When unset, mutt will not attempt to use STARTTLS regardless of the server’s capabilities.
取り合えず今回はTLS無しで(^^;
| 1 2 3 4 5 6 7 | [root@centos8 ~]# vi /etc/Muttrc.Alert [root@centos8 ~]# cat /etc/Muttrc.Alert # Local configuration for Mutt. set realname="Alert Mail" set from="Alert@example.co.jp" set smtp_url="smtp://192.168.1.1:25" set ssl_starttls="no" | 
今度は試しにレベル3で
		
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@centos8 ~]# echo Alert | mutt -d 3 -F /etc/Muttrc.Alert -s "Alert message" alert-mail@example.co.jp Debugging at level 3. [root@centos8 ~]# cat .muttdebug0 [2020-12-02 11:52:46] Mutt/1.10.1 (2018-07-13) debugging at level 3 [2020-12-02 11:52:46] getdnsdomainname(): example.co.jp [2020-12-02 11:52:46] Reading configuration file '/etc/Muttrc'. [2020-12-02 11:52:46] Reading configuration file '/etc/Muttrc.local'. [2020-12-02 11:52:46] Reading configuration file '/etc/Muttrc.Alert'. [2020-12-02 11:52:46] send.c:1273: mutt_mktemp returns "/tmp/mutt-centos8-0-1668-869608087262211111". [2020-12-02 11:52:46] sendlib.c:2790: mutt_mktemp returns "/tmp/mutt-centos8-0-1668-1094883095509022222". [2020-12-02 11:52:46] send.c:987: mutt_mktemp returns "/tmp/mutt-centos8-0-1668-9664642171354633333". [2020-12-02 11:52:46] Connected to 192.168.1.1:25 on fd=6 [2020-12-02 11:52:46] 6< 220 mail.example.co.jp ESMTP [2020-12-02 11:52:46] 6> HELO centos8.example.co.jp [2020-12-02 11:52:46] 6< 250 mail.example.co.jp [2020-12-02 11:52:46] 6> MAIL FROM:<Alert@example.co.jp> [2020-12-02 11:52:46] 6< 250 2.1.0 Ok [2020-12-02 11:52:46] 6> RCPT TO:<alert-mail@example.co.jp> [2020-12-02 11:52:46] 6< 250 2.1.5 Ok [2020-12-02 11:52:46] 6> DATA [2020-12-02 11:52:46] 6< 354 End data with <CR><LF>.<CR><LF> [2020-12-02 11:52:46] 6> . [2020-12-02 11:52:47] 6< 250 2.0.0 Ok: queued as EAE945A115 [2020-12-02 11:52:47] 6> QUIT [2020-12-02 11:52:47] mutt_free_body: unlinking /tmp/mutt-centos8-0-1668-869608087262296177. | 
無事にメールが届きました。
メールサーバがSTARTTLS有効かどうか確認しておいた方がいいですね。