mailコマンド

サーバ管理していると監視やcronでメールを飛ばしたりすることが多いと思いますが、いざコマンドでメールを飛ばすときやスクリプトのデバッグの時になると「どうやんだけかな・・・」と毎回調べることが多いので改めてよく使いそうな操作を調べてみました。

どうやらディストリビューションによってメールのFrom指定等のオプションが違っていたりするらしいです。
今回はCentOS 6.7 で確認してます。

 

Contents

 

前提

MTAはローカルのpostfixを使いますので、起動していることを確認します。

[root@localhost ~]# /etc/init.d/postfix status
master (pid  12157) is running...

 

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

最小構成だと入っていないのでインストールします。

[root@localhost ~]# yum install mailx

 

コマンドでメール送信

テストメールとかでよく使いますね。


対話で送信

メール本文は対話モードで入力してメールを送ります。本文改行後に「.」ドットか[Ctrl]+[D]キーでメール本文を終了して送信になります。

[root@localhost ~]# mail -s "subject" -r from@example.com to@example.com
body 1
body 2
body 3
.
EOT

非対話で送信

body部分を"|"パイプで渡してあげることでそのままメール送信できます。

[root@localhost ~]#  echo "body" | mail -s "subject" -r from@example.com to@example.com

echoコマンドの-eオプションで"\n"を改行として扱うことで、複数行の本文も送信できます。

[root@localhost ~]#  echo -e "body 1\nbody 2\nbody 3" | mail -s "subject" -r from@example.com to@example.com

 

スクリプトでメール送信

スクリプトでメール送信はよく使うので関数にしてしまって、あとは引数を渡すだけでメール送信できるようにする。
基本だけわかっていれば、あとは好きなように加工して使えると思います。

#!/bin/sh

SendMail() {
  from=$1
  to=$2
  subject=$3
  body=$4
  echo "$body" | mail -s "$subject" -r $from $to
}

FROM=from@example.com
TO=to@example.com
SUBJECT="subject"
BODY="body"

SendMail "$FROM" "$TO" "$SUBJECT" "$BODY"

 

キューに溜まったメール

ネットワークの障害等で大量のアラートメールが送信できずにキューに溜まってしまうなんてことがあるかもしれません。
ネットワークが復旧した時にすでに不要となったアラートメールが一斉に飛んでしまうとかなり迷惑なので、そういうときのためにキューに溜まったメールの確認と削除。

キューにあるメールの確認

[root@localhost ~]# postqueue -p
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
AD8272096B      426 Sun Jun  5 01:45:06  from@xxxxx.com
(delivery temporarily suspended: connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

151D020936      426 Sun Jun  5 01:45:06  from@xxxxx.com
(delivery temporarily suspended: connect to xxxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

24C1E20926      426 Sun Jun  5 01:45:05  from@xxxxx.com
(delivery temporarily suspended: connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

8176F20909      426 Sun Jun  5 01:45:03  from@xxxxx.com
(delivery temporarily suspended: connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

6535620918      426 Sun Jun  5 01:45:04  from@xxxxx.com
(delivery temporarily suspended: connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)

キューのメールを削除

Queue IDを指定して削除

[root@localhost ~]# postsuper -d AD8272096B
postsuper: AD8272096B: removed
postsuper: Deleted: 1 message

[root@localhost ~]# postqueue -p
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
151D020936      426 Sun Jun  5 01:45:06  from@xxxxx.xxx
              (connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

24C1E20926      426 Sun Jun  5 01:45:05  from@xxxxx.com
              (connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

8176F20909      426 Sun Jun  5 01:45:03  from@xxxxx.com
              (connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

6535620918      426 Sun Jun  5 01:45:04  from@xxxxx.com
              (connect to xxxxx.com[xxx.xxx.xxx.xxx]:25: Connection timed out)
                                         to@xxxxx.com

キューにあるメールをすべて削除

[root@localhost ~]# postsuper -d ALL
postsuper: Deleted: 4 messages

[root@localhost ~]# postqueue -p
Mail queue is empty

※今回この記事のために存在しないと思ってテストメールを飛ばしたドメインが実は存在していて、送信されてしまったという・・・ご注意を・・・

 

参考

[mailコマンド]Linuxからメールを送る
UNIXコマンド辞典>>mail
Postfixのメールキューを確認、削除する方法