简体   繁体   English

如何使用python的smptd DebuggingServer输出解码的base64编码的电子邮件

[英]how to output decoded base64 encoded emails with python's smptd DebuggingServer

I found this awesome one liner to create smtp servers on the fly with python: 我发现了这个很棒的衬板,可以使用python快速创建smtp服务器:

python -m smtpd -n -c DebuggingServer localhost:1025

Trouble is, my application sends the mail contents base64 encoded, so the output really looks like garbage: 麻烦的是,我的应用程序发送了以base64编码的邮件内容,因此输出确实看起来像垃圾:

---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/alternative; boundary="===============7226846835346479139=="'
b'MIME-Version: 1.0'
b'Subject: redacted subject'
b'From: ACME Online 2'
b'To: john.doe@acme.com'
b'Cc: '
b'X-Peer: 10.255.13.37'
b''
b'--===============7226846835346479139=='
b'Content-Type: text/html; charset="utf-8"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: base64'
b''
b'PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv'
b'L0VOIj4KPGh0bWw+CjxoZWFkPgogICAgPG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBj'
b'b250ZW50PSJ0ZXh0L2h0bWw7Y2hhcnNldD1pc28tODg1OS0xIj4KICAgIDx0aXRsZT5XZWVrbHkg'

and so on. 等等。

if I atob this, I get: 如果我不喜欢这个,我会得到:

atob('PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv')
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/"

How can I get clean base64 output decoded logs ? 如何获得干净的base64输出解码日志? Ps: I don't want to process logs afterwards, I want a clean output directly. 附言:我不想以后再处理日志,我希望直接输出干净的日志。 It's running inside a linux docker container. 它在linux docker容器中运行。

thanks to @zwer answer, I got to implement a server myself: 感谢@zwer的回答,我自己实现了一个服务器:

import asyncore
import email
from smtpd import DebuggingServer

class ActualDebuggingServer(DebuggingServer):
    def _print_message_content(self, peer, data):
        msg = email.message_from_string(data)
        for (header,value) in msg.items():
            print(header, ': ', value)
        for part in msg.walk():
            print('---------- a part: ----------')
            maybe_decoded_payload = part.get_payload(decode=True)
            if (maybe_decoded_payload is not None):
                print(bytes.decode(maybe_decoded_payload, encoding="utf-8"))


if __name__ == '__main__':
    ActualDebuggingServer(
        ('0.0.0.0', 1025),
        ('0.0.0.0', 1025)
    )
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass

it's really rough, it assumes parts are encoded in UTF-8. 这确实很粗糙,它假定部分是以UTF-8编码的。 Also, i'm not sure what the second argument is for. 另外,我不确定第二个参数是什么。 Use with caution. 请谨慎使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM