流程收到的电子邮件与Python或Django? [英] Process received email with Python or Django?

查看:147
本文介绍了流程收到的电子邮件与Python或Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何通过Django发送电子邮件,但我希望用户能够回复电子邮件。如果他们发送的电子邮件(和我收到)包含一个匹配某个字符串的消息,我将调用一个函数。

I understand how sending email works through Django, but I want users to be able to respond to the email. If the email they send (and I receive) contains a message that matches a certain string, I will call a function.

我已经做了几个谷歌搜索,但似乎除了自己做脚本以外,不是很好的解决方案。请纠正我,如果有东西可以做到这一点;否则,我可以用什么来开始编写自己的脚本来做到这一点?

I have done a few google searches but there seems to be no good solution other than to just make the script myself. Please correct me if there is something out there that could do this; otherwise, what could I use to get started on how to write my own script to do this?

谢谢!

推荐答案

我们正在做一些类似于plone的事情,通过配置postfix在接收到特定域的电子邮件时执行HTTP请求。这也应该很容易使用django,所以你只需要配置你的服务器,并在django中编写一个视图,接收电子邮件。

We are doing something similar with plone by configuring postfix to do a HTTP request to plone when receiving an email to a specific domain. This should also easily be possible with django, so that you just have to configure your server and write a view in django which receives the email.

这是怎么做的:

1)设置您的DNS,使域名的MX记录指向您的服务器。

1) Set up your DNS so that a MX record for the domain points to your server.

2)配置postfix虚拟别名 / etc / postfix / virtual

2) Configure a postfix virtual alias /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) /usr/local/bin/mta2django.py 由postscript调用,并将邮件发送到 mail-inbound django视图。这个 mta2django.py 应该可以工作:

4) The /usr/local/bin/mta2django.py is called by postscript and sends the mail to the mail-inbound django view. This mta2django.py should work:

#!/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)写一个django视图 / mail-inbound 收到邮件,做你需要做的事情。在您的请求中,您有:

5) Write a django view /mail-inbound which receives the mail and does the things you need it to do. In the request you have:


  • 邮件 - 完整的电子邮件

  • 收件人 - 原始收件人(当您没有捕获特定电子邮件地址但整个域/子域名时有用)

  • mail - the full email message
  • recipient - the original recipient (useful when you do not catch a specific email address but the whole domain / subdomain)

您可以使用python 电子邮件解析邮件 module:

You can parse the email using the python email module:

import email

msg = email.message_from_string(request.get('mail'))

由于我没有postfix专家,我不知道是否编辑 / etc / postfix / virtual / etc / aliases 就足够了。有关详细信息,请参阅后缀文档。

As I'm no postfix expert, I'm not sure if editing /etc/postfix/virtual and /etc/aliases is sufficient. Please consult the postfix documentation for details.

这篇关于流程收到的电子邮件与Python或Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆