LoginSignup
17
14

More than 5 years have passed since last update.

Python の smtpd.DebuggingServer でメール送信をデバッグ

Last updated at Posted at 2014-02-17

メール送信を行うシステムを開発している場合、作業中のネットワーク環境により、利用しているプロバイダの Outbound Port 25 Blocking の制限で25番ポート経由のメール送信がブロックされることがあります。

プロバイダ指定の SMTP サーバを経由して送信すればよいのですが、開発用のメール送信設定に SMTP ユーザ情報を含めなければならず面倒です。

開発時は、実際に送信されなくてもメールの送信内容が確認できればよかったため、デバッグ用の SMTP サーバを探していたところ、Python に smptd.DebuggingServer という標準ライブラリがありました。

以下のワンライナーで、ダミーの SMTP サーバを起動できます。

$ python -m smtpd -n -c DebuggingServer localhost:1025

smtpd モジュールのソースを見てみると SMTPServer#process_message を実装しているだけでしたので、必要に応じて調整したデバッグサーバを作っておくと良いと思います。

http://hg.python.org/cpython/file/2.7/Lib/smtpd.py#l330:

class DebuggingServer(SMTPServer):
    # Do something with the gathered message
    def process_message(self, peer, mailfrom, rcpttos, data):
        ...
        print '---------- MESSAGE FOLLOWS ----------'
        for line in lines:
           ...
        print '------------ END MESSAGE ------------'

dsmtpd というライブラリもすでにありました。

17
14
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
17
14