Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python或Django处理收到的电子邮件?_Python_Django - Fatal编程技术网

用Python或Django处理收到的电子邮件?

用Python或Django处理收到的电子邮件?,python,django,Python,Django,我了解通过Django发送电子邮件的工作原理,但我希望用户能够回复电子邮件。如果他们发送(和我接收)的电子邮件包含与特定字符串匹配的消息,我将调用函数 我做了一些谷歌搜索,但似乎没有什么好的解决方案,除了自己制作脚本。请纠正我,如果有什么东西可以做到这一点;否则,我可以使用什么来开始编写我自己的脚本来实现这一点 谢谢 我们正在对plone进行类似的操作,将postfix配置为在接收到特定域的电子邮件时向plone发出HTTP请求。这在django中也很容易实现,因此您只需配置服务器并在djang

我了解通过Django发送电子邮件的工作原理,但我希望用户能够回复电子邮件。如果他们发送(和我接收)的电子邮件包含与特定字符串匹配的消息,我将调用函数

我做了一些谷歌搜索,但似乎没有什么好的解决方案,除了自己制作脚本。请纠正我,如果有什么东西可以做到这一点;否则,我可以使用什么来开始编写我自己的脚本来实现这一点


谢谢

我们正在对plone进行类似的操作,将postfix配置为在接收到特定域的电子邮件时向plone发出HTTP请求。这在django中也很容易实现,因此您只需配置服务器并在django中编写一个接收电子邮件的视图

这就是你可以做到的:

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

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

example.com anything
django@example.com django-mail-in
3) 和
/etc/alias

django-mail-in: "|/usr/local/bin/mta2django.py http://127.0.0.1:8000/mail-inbound"
4) postscript调用
/usr/local/bin/mta2django.py
,并将邮件发送到
邮件入站
django视图。这个
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)

由于我不是后缀专家,我不确定编辑
/etc/postfix/virtual
/etc/alias
是否足够。有关详细信息,请参阅postfix文档。

Django不提供任何电子邮件接收支持

如果您需要比使用
poplib
查看电子邮件更高级的东西,或者比使用postfix更具pythonic的东西,那么这可能是一个不错的选择。

使用Mailgun

您需要为MailGun提供一个它将发布到的URL,并且您可以解析电子邮件


我想你得自己写剧本了。但是,这可能有助于:
import email

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