Django でメールを送信と確認

メールを送信する。Python 標準モジュールやライブラリにもありそうだけど、Django 組み込みであるのでそれを利用する。
日本語1.4::メールの送信 — Django 1.4 documentation

つかう

ドキュメントのままです

from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)
# もうちょっとつかいまわしやすくする
message = message
subject = subject
# to 部分はリストかタプルで指定
email_from  = "atas@hoge.com"
email_to = ("noreply@hoge.com",)
send_mail(subject, message, email_from, email_to)

メールサーバーはとりあえず postfix をつかった

sudo apt-get install postfix
sudo /etc/init.d/postfix start

とどきます

ダミーサーバーをたてる

いちいち全部自分宛にやるのもうっとおしいのでなんかないかな、と思ったら python の smtpd モジュールで DebuggingServer なるものがあるらしくて、そのままつかった

# sudo しないと動かなかった。権限の問題?
sudo python -m smtpd -n -c DebuggingServer localhost:25

なんかダミーサーバーがうごいていないと思ったら、postfix が port 25 で動いてたので止めたらよかった

sudo /etc/init.d/postfix stop

内容はこんな感じ

---------- MESSAGE FOLLOWS ----------
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: some message
From: noreply@hoge.com
To: atas@hoge.com
Date: Wed, 17 Apr 2001 06:37:39 -0000
Message-ID: <20130417063739.10658.62373@dev>
X-Peer: 127.0.0.1

高円寺5.5万円徒歩10分東向き部屋一人暮らし情報ください
------------ END MESSAGE ------------

ちなみに -m はモジュール、-c はコマンドってのはわかったけど -n ってなんだろう。

まあ

はい