特定のドメインへの電子メールを受信したときに plone への HTTP 要求を行うように postfix を構成することで、plone で同様のことを行っています。これは django でも簡単に実行できるはずなので、サーバーを構成し、django でメールを受信するビューを作成するだけで済みます。
それがあなたがそれを行う方法です:
1) ドメインの MX レコードがサーバーを指すように DNS を設定します。
2) postfix 仮想エイリアスを構成します/etc/postfix/virtual
。
example.com anything
django@example.com django-mail-in
3) および/etc/aliases
:
django-mail-in: "|/usr/local/bin/mta2django.py http://127.0.0.1:8000/mail-inbound"
4)は PostScript によって呼び出され、 django ビュー/usr/local/bin/mta2django.py
にメールを送信します。mail-inbound
これmta2django.py
はうまくいくはずです:
#!/usr/bin/python
import sys, urllib
import os
def post_message(url, recipient, message_txt):
""" post an email message to the given url
"""
if not url:
print "Invalid url."
print "usage: mta2django.py url <recipient>"
sys.exit(64)
data = {'mail': message_txt}
if recipient and len(recipient) > 0:
data ['recipient'] = recipient
try:
result = urllib.urlopen(url, urllib.urlencode(data)).read()
except (IOError,EOFError),e:
print "error: could not connect to server",e
sys.exit(73)
try:
exitcode, errormsg = result.split(':')
if exitcode != '0':
print 'Error %s: %s' % (exitcode, errormsg)
sys.exit(int(exitcode))
except ValueError:
print 'Unknown error.'
sys.exit(69)
sys.exit(0)
if __name__ == '__main__':
# This gets called by the MTA when a new message arrives.
# The mail message file gets passed in on the stdin
# Get the raw mail
message_txt = sys.stdin.read()
url = ''
if len(sys.argv)>1:
url = sys.argv[1]
recipient = ''
# If mta2django is executed as external command by the MTA, the
# environment variable ORIGINAL_RECIPIENT contains the entire
# recipient address, before any address rewriting or aliasing
recipient = os.environ.get('ORIGINAL_RECIPIENT')
if len(sys.argv)>2:
recipient = sys.argv[2]
post_message(url, recipient, message_txt)
/mail-inbound
5)メールを受信し、必要なことを実行するdjango ビューを作成します。リクエストには次のものがあります。
mail
- 完全な電子メール メッセージ
recipient
- 元の受信者 (特定のメールアドレスではなく、ドメイン/サブドメイン全体を把握する場合に役立ちます)
email
Pythonモジュールを使用して電子メールを解析できます。
import email
msg = email.message_from_string(request.get('mail'))
私は後置の専門家ではないので、編集/etc/postfix/virtual
で/etc/aliases
十分かどうかはわかりません。詳細については、後置のドキュメントを参照してください。