2015年8月15日土曜日

[CentOS6] コマンドラインでSMTPサーバを指定してメール送信(msmtp)


msmtp は、コマンドラインで使用するSMTPクライアントです。

msmtp コマンドを使用すると、コマンドライン引数でSMTPサーバを指定することができます。
msmtp コマンドを実行するサーバ上でpostfixやsendmailなどを動かしたり設定ファイルをいじる必要もありません。

コマンド(プログラム)の実行結果をメール送信するのに便利に使えます。
MSMTPホームページ: http://msmtp.sourceforge.net/

1.msmtp インストール


msmtp は rpmパッケージがないので、ソースをダウンロードしてコンパイルする必要があります。
まず、コンパイルに必要なパッケージをインストールします。
# yum install gcc make
次に msmtp が使用するライブラリをインストールします。
yum の epel レポジトリを使います。
# yum install gnutls-devel
# yum install openssl-devel
# yum --enablerepo=epel install libgsasl-devel
# yum install libidn-devel
sourceforge から msmtp のソースをダウンロードして展開します。
# wget http://sourceforge.net/projects/msmtp/files/latest/download?source=files
# tar xvfh msmtp-1.4.30.tar.bz2
コンパイルしてインストールします。
# cd msmtp-1.4.30
# ./configure
# make
# make install

2.使い方


設定ファイルでSMTPサーバなどを指定する方法もありますが、ここでは、コマンドライン引数で指定する例を紹介します。
使い方の詳細はマニュアルを参照してください
以下の例では、ls コマンドの実行結果をSMTPサーバを指定してメールします。
awkコマンドでメールの題名を定義しています。

◆ 認証なしのSMTPサーバを指定して送信
# ls | awk 'BEGIN{print "Subject: TestMail\n";}{print $0;}' | msmtp --host=<SMTPサーバ> --from=<FROMメールアドレス> <宛先メールアドレス> 

◆ 認証あり(パスワード暗号化なし)のSMTPサーバを指定して送信
# ls | awk 'BEGIN{print "Subject: TestMail\n";}{print $0;}' | msmtp --host=<SMTPサーバ> --auth=plain --user=<SMTPユーザ> --passwordeval="echo <SMTPパスワード>" --from=<FROMメールアドレス> <宛先メールアドレス> 

"--debug" オプションをつけると、以下のようにSMTPサーバとのやり取りを見ることができます。
メール送信がうまくいかないときには、原因を探るのに使えます。
以下は認証ありのSMTPサーバを使用したメール送信の成功例です。
# ls | awk 'BEGIN{print "Subject: TestMail\n";}{print $0;}' | msmtp --host=smtp.example.jp --auth=plain --user=smtpuser --passwordeval="echo smtppass" --from=admin@example.jp --debug ope@example.jp
using account specified on command line
host                  = smtp.example.jp
port                  = 25
timeout               = off
protocol              = smtp
domain                = localhost
auth                  = PLAIN
user                  = smtpuser
password              = *
passwordeval          = echo smtppass
ntlmdomain            = (not set)
tls                   = off
tls_starttls          = on
tls_trust_file        = (not set)
tls_crl_file          = (not set)
tls_fingerprint       = (not set)
tls_key_file          = (not set)
tls_cert_file         = (not set)
tls_certcheck         = on
tls_force_sslv3       = off
tls_min_dh_prime_bits = (not set)
tls_priorities        = (not set)
auto_from             = off
maildomain            = (not set)
from                  = admin@example.jp
dsn_notify            = (not set)
dsn_return            = (not set)
keepbcc               = off
logfile               = (not set)
syslog                = (not set)
aliases               = (not set)
reading recipients from the command line
<-- 220 example.jp ESMTP Sendmail; Thu, 24 Jan 2013 09:33:28 +0900
--> EHLO localhost
<-- 250-example.jp Hello smtp.example.jp [111.222.333.444], pleased to meet you
<-- 250-ENHANCEDSTATUSCODES
<-- 250-PIPELINING
<-- 250-8BITMIME
<-- 250-SIZE 11534336
<-- 250-AUTH PLAIN LOGIN
<-- 250 HELP
--> AUTH PLAIN xxxxxx==
<-- 235 2.0.0 OK Authenticated
--> MAIL FROM:<admin@example.jp>
--> RCPT TO:<ope@example.jp>
--> DATA
<-- 250 2.1.0 <admin@example.jp>... Sender ok
<-- 250 2.1.5 <ope@example.jp>... Recipient ok
<-- 354 Enter mail, end with "." on a line by itself
--> Subject: TestMail
-->
--> anaconda-ks.cfg
--> install.log
--> install.log.syslog
--> msmtp-1.4.30
--> msmtp-1.4.30.tar.bz2
--> .
<-- 250 2.0.0 r0O0XScF018310 Message accepted for delivery
--> QUIT
<-- 221 2.0.0 example.jp closing connection


実際に使用するときは、mail コマンドと組み合わせたほうが使いやすいと思います。