5

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

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

谢谢!

4

3 回答 3

5

我们通过配置 postfix 在接收到特定域的电子邮件时执行 HTTP 请求来进行 plone 类似的操作。使用 django 也应该很容易做到这一点,因此您只需配置您的服务器并在 django 中编写一个接收电子邮件的视图。

你可以这样做:

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

2)配置后缀虚拟别名/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-inbounddjango 视图。这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)

5) 编写一个 django 视图/mail-inbound来接收邮件并做你需要它做的事情。在您的请求中:

  • mail- 完整的电子邮件信息
  • recipient- 原始收件人(当您没有捕获特定电子邮件地址而是整个域/子域时很有用)

email您可以使用 python模块解析电子邮件:

import email

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

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

于 2013-01-02T20:21:41.790 回答
1

使用邮枪。

您需要为 MailGun 提供一个用于 POST 的 URL,然后您就可以解析电子邮件。

http://documentation.mailgun.net/quickstart.html#receiving-and-parsing-email

于 2013-01-03T07:57:54.717 回答
0

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

如果你需要比使用poplib.

于 2013-01-02T20:25:10.537 回答