Memo

メモ > サーバ > 各論: プログラミング > Sisimai(バウンスメール解析)を使う

■Sisimai(バウンスメール解析)を使う
Sisimai | bounceHammerの後継となるバウンスメール解析ライブラリ http://libsisimai.org/ja/ Sisimaiを使ったバウンスメールの管理 http://techlife.cookpad.com/entry/2017/05/15/000000 Sisimaiでバウンスメールを解析してみる http://qiita.com/taku1201/items/0afae3dd90507a688e3e ■インストール(失敗)
>gem install sisimai ERROR: Could not find a valid gem 'sisimai' (>= 0), here is why: Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/latest_specs.4.8.gz)
インストールできないので、以下を参考に設定を変更 http://book.scss.jp/code/c2/07.html
>gem source -a http://rubygems.org/ https://rubygems.org is recommended for security over http://rubygems.org/ Do you want to add this insecure source? [yn] y http://rubygems.org/ added to sources
■インストール
>gem install sisimai Fetching: oj-2.18.5.gem (100%) Temporarily enhancing PATH to include DevKit... Building native extensions. This could take a while... Successfully installed oj-2.18.5 Fetching: sisimai-4.21.0.gem (100%) Successfully installed sisimai-4.21.0 Parsing documentation for oj-2.18.5 Installing ri documentation for oj-2.18.5 Parsing documentation for sisimai-4.21.0 Installing ri documentation for sisimai-4.21.0 Done installing documentation for oj, sisimai after 7 seconds 2 gems installed
■解析例
#!/ruby/bin/ruby require 'find' require 'sisimai' puts "Content-type: text/html\n\n" puts '<!DOCTYPE html>' puts '<html>' puts '<head>' puts '<meta charset="utf-8">' puts '<title>Sisimai</title>' puts '</head>' puts '<body>' puts '<h1>Sisimaiのテスト</h1>' # バウンス理由 http://libsisimai.org/ja/reason/ # ハードバウンスとは https://sendgrid.kke.co.jp/blog/?p=7837 reasons = { 'hostunknown' => '宛先ホスト名が存在しない(ハードバウンス)', 'userunknown' => '宛先メールアドレスは存在しない(ハードバウンス)', 'filtered' => 'DATAコマンド以降で拒否された', 'hasmoved' => '宛先メールアドレスは移動した(ハードバウンス)', 'rejected' => 'エンベロープFromで拒否された', 'mailboxfull' => 'メールボックスが一杯', 'suspend' => '宛先アカウントは一時的に停止中', 'vacation' => '宛先は現在不在である自動応答メッセージ', 'contenterror' => '不正な形式のヘッダまたはメール', 'exceedlimit' => 'メールサイズが最大値を超過(5.2.3)', 'feedback' => '元メールへの苦情によるバウンス(FBL形式の)', 'mesgtoobig' => 'メールが大き過ぎる(5.3.4)', 'securityerror' => 'ウィルスの検出または認証失敗', 'spamdetected' => 'メールはスパムとして判定された', 'blocked' => 'IPアドレスやホスト名による拒否', 'expired' => '配送時間切れ', 'mailererror' => '宛先ホストでのメールプログラムのエラー', 'networkerror' => 'DNS等ネットワーク関係のエラー', 'norelaying' => 'リレーの拒否', 'notaccept' => '宛先ホストはメールを受けとらない', 'systemerror' => '宛先サーバでのOSレベルのエラー', 'systemfull' => '宛先サーバのディスクが一杯', 'toomanyconn' => '接続制限数を超過した', 'delivered' => '正常に配信された', 'syntaxerror' => 'SMTPの文法エラー', 'onhold' => 'エラー理由の特定は保留', 'undefined' => 'バウンスした理由は特定出来ず', } Find.find('set-of-emails/maildir/err/') {|path| if File.ftype(path) == 'file' then puts '<h2>' + File.basename(path) + '</h2>' bounces = Sisimai.make(path) if bounces.is_a? Array then puts '<pre>' bounces.each do |bounce| if reasons.has_key?(bounce.reason) then reason = ' … ' + reasons[bounce.reason] else reason = '' end puts 'from: ' + bounce.addresser.address # shironeko@example.org # From puts 'to: ' + bounce.recipient.address # kijitora@example.jp # To puts 'host: ' + bounce.recipient.host # example.jp puts 'status: ' + bounce.deliverystatus # 5.1.1 puts 'code: ' + bounce.replycode # 550 puts 'reason: ' + bounce.reason + reason # userunknown end puts '</pre>' else puts '<p>There is no bounce message in the mailbox or Sisimai could not parse</p>' end end } puts '</body>' puts '</html>' exit
■活用例 SisimaiはRubyとPerlに対応している もしメインシステムがRubyかPerlなら、上のような仕組みを普通に組み込めばいい メインシステムがRubyでもPerlでもないなら、 ・Ruby+Sisimaiでバウンスメールを解析し、問題のあったメールアドレスとその詳細(バウンスの理由など)をデータベースに記録する ・別途PHPでそのデータベースを参照し、メール送信先リストを更新する のような仕組みが必要になりそう Rubyで直接メール送信先リストを書き換えると、機能追加や仕様変更があったときの対応が大変そう

Advertisement