后缀中继:只能按照一定的标准向Internet发送消息,如果不符合标准,则转发到另一个中继

这听起来有点复杂和复杂,但我们目前在我的一个客户的环境中有一个非常邪恶的设置电子邮件。

从最内层的系统到外面我们有:

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

由于外部目的地离开办公室的电子邮件(从系统内部的人员到外部)不起作用,他们似乎被捕获在McAfee Email Gateway中,并且不在外部中继。

我想要做的是build立一个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 

我有点挂断了如何configurationPostfix的select性中继选项。 有没有人可以提供一些见解,如何实现这一点?

我想出了一个非常黑客的办法。

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

然后,Python脚本会像通常那样处理邮件,通过stringparsing器中的电子邮件运行,然后阅读主题行以确定将邮件发送到哪里,然后将原始邮件原封不动地发送到本地Postfix SMTP服务器(直接发送出去),或者到networking上的其他中继。 到目前为止,它似乎正在工作。

这是我在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() 

我configurationPostfix在不同的端口上侦听SMTP。 实际上,这是最终做了什么,在我的Ubuntu服务器上的/etc/postfix/master.cf ,这是最终的两条线,以及我如何在Postfix中configurationSMTPd – 请注意,您可以使用任何高编号的端口对于任何其他端口的SMTPD,但我select了一些简单的:

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

Python脚本然后将数据转发到端口6625 ,并在那里运行Postfix的SMTPd; 在完成之后,我们特别让Python确定哪个SMTP服务器或中继是“下一跳”。 这有点打破标准的“收到”标题,但它应该与消息正常工作。

我没有遇到任何问题与我的解决scheme,它似乎工作。 额外的testing是必要的,但这个“解决scheme”似乎解决了路由问题,如何路由邮件(直接SMTP或通过其余的继电器),而不是真的搞乱Postfixconfiguration。