Help us understand the problem. What is going on with this article?

コマンドラインからSMTP認証の試験を行う

More than 5 years have passed since last update.

SMTPサーバの設定をした後、SMTP認証(AUTH-PLAIN)が通かどうかをtelnetで確認します。
認証用の文字列はユーザ名 (Null)ユーザ名(Null)パスワードをbase64で符号化して作ります。

MacOS Xを使っている場合は、% printf "%s\0... tr -d '\n' | pbcopyでコピーすると文字列を選択してコピーしなくていいので楽です。


  • SMTPサーバ: mx.example.jp:587
  • 認証ユーザ名: user@example.jp
  • 認証パスワード: hoge

% printf "%s\0%s\0%s" user@example.jp user@example.jp hoge | openssl base64 -e | tr -d '\n'; echo
dXNlckBleGFtcGxlLmpwAHVzZXJAZXhhbXBsZS5qcABob2dl ←コピーしておく

% telnet mx.example.jp 587
Trying 192.0.2.25...
Connected to mx.example.jp
Escape character is '^]'.
220 mx.example.jp ESMTP SENDMAIL
EHLO host.example.jp ←自分のホスト名を名乗る
250-mx.example.jp Hello host.example.jp [192.0.2.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE 10485760
250-DSN
250-AUTH LOGIN PLAIN CRAM-MD5 DIGEST-MD5
250-DELIVERBY
250 HELP
AUTH PLAIN dXNlckBleGFtcGxlLmpwAHVzZXJAZXhhbXBsZS5qcABob2dl ←さっきの文字列を貼り付ける
235 2.0.0 OK Authenticated ←200系の応答が返ってきたらOK
quit
221 2.0.0 mx.example.jp closing connection
Connection closed by foreign host.

EHLOで自分のホスト名を名乗り、AUTH PLAINで最初に作った文字列を渡して、200系の応答が返ってきたら認証成功です。

2014/08/04追記

毎回printf,openssl,trで長いワンライナー書くのが面倒なので実際は下記のようなスクリプトを作っておくと良いです。

#!/bin/sh
# smtpauth-passwd
PATH='/usr/local/openssl/bin:/usr/local/bin:/usr/bin:/bin'
MAILADDR="$1"
PASSWORD="$2"

usage()
{
    echo "Usage: $0 mail-address password"
    exit 1
}

if [ -n "$MAILADDR" ]; then
    if [ -n "$PASSWORD" ]; then
        printf "%s\0%s\0%s" $MAILADDR $MAILADDR $PASSWORD | openssl base64 -e | tr -d '\n'
        echo
        exit 0
    else
        usage
    fi
else
    usage
fi

これを~/binとか/usr/local/bin/にsmtpauth-passwdという名前で保存して、実行権限を付けて、コマンドラインから

$ smtpauth-passwd user@example.jp hoge
dXNlckBleGFtcGxlLmpwAHVzZXJAZXhhbXBsZS5qcABob2dl

という感じで使うと便利です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away