回复电子邮件至Commo

2024-04-25 05:43:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想设置我的邮件服务器,这样如果有人向user@example.com发送电子邮件,它将被定向到user的收件箱。这不是邮箱。而是一个聊天平台。所以通讯应该是实时的。工作流程是

  1. 有人发邮件到user@example.com。你知道吗
  2. user获取包含该消息的聊天窗口。你知道吗

我可以通过编写一个程序,每分钟轮询邮件服务器并检查新邮件来解决这个问题。如果找到了,就发一条聊天信息。但那不是实时的。 另一个选择是向邮件服务器添加某种插件。我还没有安装任何邮件服务器。我将只设置邮件服务器,帮助我做到这一点。你知道吗

我用的是Python,PHP。因此,任何使用这两种语言的解决方案都是受欢迎的。如果所有这些都失败了,我想我必须用C写插件


Tags: 程序服务器com插件消息example电子邮件邮件
2条回答

你可以这样做:

1)设置DNS,使域的MX记录指向您的服务器。你知道吗

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

@example.com django-mail-in

3)和/etc/别名:

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公司应该工作:

#!/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-原始收件人(当您捕获的不是特定的电子邮件地址,而是整个域/子域时很有用)

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

import email

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

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

拉姆森可以工作,是用Python写的。它位于您的SMTP服务器前面,过滤掉您在它的路由文件中定义的电子邮件。主要的理由似乎是开发人员易于使用,并且它被设计成集成到其他软件中。你知道吗

http://lamsonproject.org/

相关问题 更多 >