Chúng tôi đang làm điều gì đó tương tự với bản vẽ bằng cách định cấu hình postfix để thực hiện yêu cầu HTTP để vẽ khi nhận được email đến một miền cụ thể. Điều này cũng nên dễ dàng có thể với django, do đó bạn chỉ cần cấu hình máy chủ của bạn và viết một cái nhìn trong django mà nhận được email.
Đó là cách bạn có thể thực hiện:
1) Thiết lập DNS để bản ghi MX cho tên miền trỏ đến máy chủ của bạn.
2) Cấu hình một postfix bí danh ảo /etc/postfix/virtual
:
example.com anything
[email protected] django-mail-in
3) và /etc/aliases
:
django-mail-in: "|/usr/local/bin/mta2django.py http://127.0.0.1:8000/mail-inbound"
4) Các /usr/local/bin/mta2django.py
được gọi bởi postscript và gửi qua đường bưu điện đến xem mail-inbound
django. mta2django.py
này nên làm việc:
#!/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)
5) Viết một cái nhìn django /mail-inbound
mà nhận được mail và thực hiện những điều bạn cần phải làm gì. Trong yêu cầu bạn có:
mail
- đầy đủ email nhắn
recipient
- người nhận gốc (hữu ích khi bạn không nắm bắt một địa chỉ email cụ thể nhưng lĩnh vực toàn bộ/subdomain)
bạn có thể phân tích các email bằng cách sử dụng python email
mô-đun:
import email
msg = email.message_from_string(request.get('mail'))
Như tôi không có chuyên môn postfix, tôi không chắc chắn nếu chỉnh sửa /etc/postfix/virtual
và /etc/aliases
là đủ. Vui lòng tham khảo tài liệu postfix để biết chi tiết.
Tôi nghĩ bạn sẽ phải tự tạo tập lệnh. Tuy nhiên, điều này có thể hữu ích cho điều đó: http://stackoverflow.com/questions/1225586/checking-email-with-python –