LoginSignup
1
0

More than 5 years have passed since last update.

SMTPのやり取りを検証したかった

Posted at

メールの調査のためEHLOとHELOを切り替えたりした場合のレスポンスなどを確認したかった。
その際、メールサーバ立ててパケットキャプチャを実施してメールの監視なども行っていたが、rubyスクリプトで監視出来るようにスクリプトを用意したので備忘録としてメモ

プログラムはこんな感じsmtpライブラリを使って
smtp.set_debug_outputをする事で送受信の内容が見えた。
プログラム中の TO = "なんとかかんとーか@gmail.com" は要書き換え。

smtp.rb
#!/usr/bin/env ruby

require 'net/smtp'

# dig -t MX  gmail.com から取得
SMTP_SERVER = 'gmail-smtp-in.l.google.com'
TO = 'なんとかかんとーか@gmail.com'

smtp = Net::SMTP.new(SMTP_SERVER, 25)
smtp.set_debug_output $stderr

# SMTPのEHLOの代わりにHELOコマンドを使って送信したい
# smtp.esmtp = false

smtp.start do
  message = <<-END_MAIL
From: "メール送り主" <#{TO}>
To: #{TO}
Subject: メール配信テスト
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <#{Time.now.to_i}@gmail.com>

メール配信テスト
  END_MAIL

  response = smtp.send_message(message, TO, TO)
  p response
end

上記プログラムを実行すると以下のようなメッセージになる。
送信の時にEHLOコマンドを実行して、サポートされている機能などが返ってきていることが確認出来る。

Connection opened: gmail-smtp-in.l.google.com:25
-> "220 mx.google.com ESMTP 6-v6si8033943pgl.517 - gsmtp\r\n"
<- "EHLO localhost\r\n"
-> "250-mx.google.com at your service, [210.128.134.30]\r\n"
-> "250-SIZE 157286400\r\n"
-> "250-8BITMIME\r\n"
-> "250-STARTTLS\r\n"
-> "250-ENHANCEDSTATUSCODES\r\n"
-> "250-PIPELINING\r\n"
-> "250-CHUNKING\r\n"
-> "250 SMTPUTF8\r\n"
<- "MAIL FROM:<なんとかかんとーか@gmail.com>\r\n"
-> "250 2.1.0 OK 6-v6si8033943pgl.517 - gsmtp\r\n"
<- "RCPT TO:<なんとかかんとーか@gmail.com>\r\n"
-> "250 2.1.5 OK 6-v6si8033943pgl.517 - gsmtp\r\n"
<- "DATA\r\n"
-> "354  Go ahead 6-v6si8033943pgl.517 - gsmtp\r\n"
writing message from String
wrote 222 bytes
-> "250 2.0.0 OK 1535351007 6-v6si8033943pgl.517 - gsmtp\r\n"
#<Net::SMTP::Response:0x00007fac5905e4b8 @status="250", @string="250 2.0.0 OK 1535351007 6-v6si8033943pgl.517 - gsmtp\n">
<- "QUIT\r\n"
-> "221 2.0.0 closing connection 6-v6si8033943pgl.517 - gsmtp\r\n"

参考: https://docs.ruby-lang.org/ja/latest/class/Net=3a=3aSMTP.html

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0