我需要接收与后缀中的特定规则集匹配的传入电子邮件,将其发送到外部python进程,根据后缀postmap表将电子邮件重写为多个传递收件人,然后重新注入后缀链。所包含的python除了满足一项基本要求外,它跟踪新重新注入的电子邮件的queueid。
使用/ usr / sbin / sendmail重新注入的典型方法不会为正确的过程返回可用的queueid。这会导致新创建电子邮件的可见性丧失(必须手动解析日志以生成第三方的送达确认报告)。
由于重新注入过程是在python中进行的,因此理想情况下,我想使用smtplib.Sendmail进行此操作,但它也不会返回queueid。
我尝试过的工作类型是将netcat用作python中的子进程(netcat_msg =“ helo myserver \ nmail from:” +发件人+“ \ nrcpt到:” +收件人+“ \ ndata \ n” + msg.as_string()+“ \ n \ n。\ nquit \ n”),但我觉得这是一种破解,并且我收到有关smtp中命令序列的警告(这是预期的,因为我在发出该命令之前不等待响应下一个命令)。
有没有办法在过程完成后公开从远程SMTP服务器返回的queueid?有建议的方法吗?
我的目标是将这些queueid记录到文件/ api端点/无论如何,以便我们确定到a@domain.tld的传入电子邮件已发送到bob @ example.com,chris @ example.com并跟踪返回状态目标服务器。
(对不起,我的原始python)
#!/bin/python
#v 2.7.15
import email
import sys
from email.mime.text import MIMEText
import argparse
from subprocess import Popen
from subprocess import PIPE
#ignore this, it was just for debugging
def dump(obj):
for attr in dir(obj):
if hasattr( obj, attr ):
print( "obj.%s = %s" % (attr, getattr(obj, attr)))
def process_message(data, sender, recipient):
msg = email.message_from_string(data)
newaddress = '"{recipient}" <{recipient}>'.format(recipient=recipient)
oldaddress = ''
if msg.has_key('To'):
oldaddress = msg['To']
msg.replace_header('To', newaddress)
else:
msg.add_header('To', newaddress)
oldaddress = newaddress
if msg.has_key('X-Original-To'):
msg.replace_header('X-Original-To', oldaddress)
else:
msg.add_header('X-Original-To', oldaddress)
#print(msg.as_string())
try:
# replace this with a re-inject that can return the queueid
p = Popen(["/usr/sbin/sendmail", "-G", "-i", "-f " + sender, "--", recipient ], stdin=PIPE)
p.communicate(msg.as_string())
# end replacement
# log original queueid, returned queueid and destination email here
except Exception as ex:
exit(69)
def main():
parser = argparse.ArgumentParser(description='To field replacement for Email MIME.')
parser.add_argument('--from', dest="sender", help="From email address", required=True)
parser.add_argument('--recipient', dest="recipients", help="Recipient address to replace in To field (N+1)", nargs='+', required=True)
args = parser.parse_args()
#dump(args)
data = sys.stdin.readlines()
data = ''.join(data)
for recipient in args.recipients:
#print(recipient)
process_message(data, args.sender, recipient)
exit(0)
main()