OpenSSLコマンドでGmailからメールを送信する

SMTPを学習して浅ーく理解できたのでGmailから自分自身にメールを送信してみた。

Gmailは暗号通信でしかSMTPを利用できないので、telnetではなくopensslコマンドでサーバにアクセスした。


引っかかった箇所:

  • heloとehloは違うらしい。ehloはサポートされている拡張仕様のリストを得られる。
  • auth plainに続くアカウント情報は'\0(アドレス)\0(パスワード)'をbase64エンコードしたもの。 echo -e '\0mail\0pass' | base64とかで実行すると楽
  • mail from:, rcpt to:ではメールアドレスを山括弧<>で囲う。
  • opensslコマンドのオプションで-crlfを指定する。これを指定しないとdataコマンドの終わりのピリオドを認識してくれない。
$ openssl s_client -crlf -connect smtp.gmail.com:465
CONNECTED(00000003)
depth=1 /C=US/O=Google Inc/CN=Google Internet Authority
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
   i:/C=US/O=Google Inc/CN=Google Internet Authority
 1 s:/C=US/O=Google Inc/CN=Google Internet Authority
   i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
xxx
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority
---
No client certificate CA names sent
---
SSL handshake has read 1715 bytes and written 307 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1
    Cipher    : RC4-SHA
    Session-ID: xxx
    Session-ID-ctx: xxx
    Master-Key: 
    Key-Arg   : None
    Start Time: 1311865286
    Timeout   : 300 (sec)
    Verify return code: 20 (unable to get local issuer certificate)
---
220 mx.google.com ESMTP xxx # server
ehlo localhost # client
250-mx.google.com at your service, [xxx.xxx.xxx.xxx] # server
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
auth plain xxx= # client
235 2.7.0 Accepted # server
mail from:<xxx@gmail.com> # client
250 2.1.0 OK xxx # server
rcpt to:<xxx@gmail.com> # client
250 2.1.5 OK xxx # server
data # client
354  Go ahead xxx # server
Date: Thu, 28 Jul 2011 22:31:00 +0900 # client
From:  "john doe" < xxx@gmail.com >
To: xxx@gmail.com
Subject: email by hand

this is my first e-mail written by hand
Thank you.
.
250 2.0.0 OK 1311865401 # server

quit # client
221 2.0.0 closing connection xxx # server
read:errno=0