AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 866325
Accepted
Thomas Ward
Thomas Ward
Asked: 2017-08-02 09:07:53 +0800 CST2017-08-02 09:07:53 +0800 CST 2017-08-02 09:07:53 +0800 CST

后缀中继:仅根据特定条件将消息发送到 Internet,如果不满足条件,则中继到另一个中继

  • 772

这听起来有点令人费解和复杂,但我们目前在我的一个客户环境中为电子邮件设置了一个非常糟糕的设置。

从最里面的系统到外面,我们有:

Exchange Server -> Sendmail Server -> McAfee Email Gateway -> OUTSIDE

这有点邪恶,因为外部目的地不在办公室电子邮件(从系统内部的某个人到外部)不起作用,它们似乎被 McAfee 电子邮件网关捕获并且不会在外部中继。

我正在尝试做的是将 Postfix 服务器设置为中继和 SMTP 服务器,并且取决于接收到的内容:

  1. 直接发送电子邮件(使用 SMTP,仅适用于外出回复)
  2. 将邮件中继到 Sendmail 服务器,以完成正常情况下的其余中继职责。

这看起来有点像以下:

Exchange Server -> Postfix Relay --- Out of Office messages only ---> OUTSIDE
                         |
                   All other mail
                         |
                         ---> Sendmail Server/Relay ---> McAfee Email Gateway ---> OUTSIDE

我对如何为选择性中继选项配置 Postfix 有点犹豫。有没有人可以就如何实现这一目标提供一些见解?

postfix
  • 1 1 个回答
  • 742 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas Ward
    2017-08-03T17:55:07+08:002017-08-03T17:55:07+08:00

    我想出了一个非常hackish的方法。

    本质上,我在 Postfix 服务器和实际处理真正中继路由的 Exchange 服务器之间放置了一个用 Python 编写的自定义 SMTP“服务器” 。它是用 Python 编写的,并在端口 25 上以超级用户身份运行(因为端口绑定限制)。

    然后,该 Python 脚本像往常一样处理消息,运行来自字符串解析器的电子邮件,然后读取主题行以确定将其发送到哪里,然后将原始消息原封不动地发送到本地 Postfix SMTP 服务器(直接向外发送),或发送到网络上的其他中继。到目前为止,它似乎正在工作。

    这是我在 Python 方面使用的代码:

    #!/usr/bin/env python3
    
    # Libraries
    import smtplib
    import smtpd
    import asyncore
    import email
    import sys
    from datetime import datetime
    
    print('Starting custom mail handling server...')
    
    # CONFIGURATION VARIABLES - We are expecting to relay, so... let's relay.
    SMTP_RELAY = 'SMTP_RELAY_HOSTNAME_OR_IP'
    SMTP_DIRECT = 'localhost'
    
    #############
    #############
    # SMTP SERVER
    #############
    #############
    
    
    # noinspection PyMissingTypeHints,PyBroadException
    class AutoReplyHandlerSMTP(smtpd.SMTPServer):
    
        def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
            print('MESSAGE RECEIVED - [%s]' % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            print('Receiving message from:', peer)
            print('Message addressed from:', mailfrom)
            print('Message addressed to  :', rcpttos)
            print('Message length        :', len(data))
            print(data)
    
            # Flush the output buffered (handy with the nohup launch)
            sys.stdout.flush()
    
            # Analyze and extract data headers
            msg = email.message_from_string(data)
            subject = ''
            try:
                subject = msg['Subject']
                subject = subject.lower()
            except:
                print("Subject error:", sys.exc_info()[0])
            print('Message subject       :', msg['Subject'])
    
            # Determine whether we are directly sending outbound, or if we're relaying to another server.
            if "automatic reply" in subject:
                print("Automatic reply received, sending directly.")
                # Local postfix SMTPd is on tcp/6625.
                conn = smtplib.SMTP(host=SMTP_DIRECT, port=6625, timeout=60)
                conn.sendmail(mailfrom, rcpttos, msg.as_string())
                conn.quit()
            else:
                print("Standard message detected, sending to relay.")
                # Other relay server is on tcp/25 (standard smtp)
                conn = smtplib.SMTP(host=SMTP_RELAY, timeout=60)
                conn.sendmail(mailfrom, rcpttos, msg.as_string())
    
            # Flush the output buffered (handy with the nohup launch)
            print("\n\n")
            sys.stdout.flush()
            return
    
    
    # Listen to port 25 ( 0.0.0.0 can be replaced by the ip of your server but that will work with 0.0.0.0 )
    server = AutoReplyHandlerSMTP(('0.0.0.0', 25), None)
    
    # Wait for incoming emails
    asyncore.loop()
    

    我将 Postfix 配置为在不同端口上侦听 SMTP。实际上,这就是最终/etc/postfix/master.cf在我的 Ubuntu 服务器上完成的工作,最终是这两行以及我如何在 Postfix 中配置 SMTPd - 请注意,您可以将任何高编号端口用于 Postfix SMTPd 的任何其他端口,但我选择了一些简单的东西:

    #smtp      inet  n       -       y       -       -       smtpd
    6625      inet  n       -       y       -       -       smtpd
    

    然后 Python 脚本将数据转发到 port 6625, Postfix 的 SMTPd 在那里运行;完成后,我们专门让 Python 确定哪个 SMTP 服务器或中继是“下一跳”。这有点打破了标准的“接收者”标头,但它应该可以很好地处理消息。

    我的解决方案没有遇到任何问题,而且它似乎正在工作。额外的测试是必要的,但这个“解决方案”似乎解决了如何路由消息(直接 SMTP 输出,或通过其余中继)的路由问题,而不会真正弄乱 Postfix 配置。

    • 0

相关问题

  • Postfix 在特定端口上接受邮件

  • 让 Postfix 以两种方式处理垃圾邮件

  • Postfix 或 exim:自动/程序化和转发电子邮件设置

  • 后缀电子邮件地址

  • 什么是最好的开源电子邮件解决方案包

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve