我在解析 MIME 消息的特定正文部分时遇到问题。
我有一个电子邮件客户端 Web 界面。我想允许用户下载电子邮件的附件。过去,每次我想下载附件时,我都会使用参数 RFC822 调用 IMAP 服务器以获取整个消息,我可以使用 Python 轻松解析。
但是,这效率不高,我需要一种方法来获取所需的附件。我正在使用特定正文部分的 BODY[1]、BODY[2] 等索引调用 IMAP 服务器的替代方法。
当我进行此 IMAP 调用时,我获得了正确的正文部分(当我调用 BODYSTRUCTURE 时,我正在寻找的部分中的字节数加起来,所以我肯定获得了正确的部分)。
但是,我无法将这个身体部分解析为可用的东西,或者为此保存它。
一个具体的例子:我打电话获取邮件的 BODY[1] 并获取回来
('4 (UID 26776 BODY[2] {5318}', '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMg\r\nIyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09\r\nPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wAARCABRAQIDASIA\r\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\r\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\r\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\r\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\r\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\r\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg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bDpFpptjMttF85QgueWbj1rlNYu7ebSriwDl3mKZZOQu056967zqOawtX8LW17DM9\r\noqwXLL8pyQmfcD+lVB2kmiZpSi01c5HToZLO0aZLlLOyhY75pXwgOORj+I9OKx/EXijTddntrW2e\r\nRFtlZEmkTCyliM8clenGf0pn/CBeJ9W1U2t6iQQRHPmlv3IB7oB1P6+pr0Pw34G0rw4Fkij+0Xg6\r\n3EoBYf7o6L+HPvXYpQoy5+bmkccoSrx9ny2ibGkRtFo9lHIpV0gRWB7EKKuUUVxN3dztSsrBRRRS\r\nGFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQA\r\nneloooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKA\r\nCiiigD//2Q==\r\n').
此特定响应对应于 JPEG 图像附件。
我尝试提取代表身体部分的字符串(所以,我说的是从'/9j'开始并以'2Q==\r\n'结束的字符串)并将其保存为.jpg文件,但是它不是一个有效的文件。
然后我认为,由于该字符串中有多个 \r\n 实例,因此该字符串可能会使用换行符/回车符进行拆分,因此我拆分了字符串并将其从 \r\n 中剥离,然后加入了子字符串并尝试将其保存到文件中。仍然不是有效的 JPEG 文件。
我能做些什么来尝试解析这个响应?
谢谢你。