侧翼:MimePart不是iterab

2024-06-07 00:45:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个笨蛋,用侧翼来分析邮件。 https://github.com/mailgun/flanker

我犯了一个不可纠正的错误,我就是搞不懂。我读了很多关于单子的书,但我就是没法用。我希望能得到一些帮助。你知道吗

要运行以下代码,您需要安装侧翼,并将此文件保存为“email”。 http://pastebin.com/ZS4q2kYN

我正在尝试阅读“attachmenttype”,并根据响应做一些事情。但还是不能让它工作。测试代码如下:

#!/usr/bin/python
#Open Email
from flanker import mime
with open ("email", mode="rb") as myfile:
    message_string=myfile.read()
myfile.close()

#Read Email
msg = mime.from_string(message_string)

#read attachment type
attachmenttype = msg.parts[1]
print attachmenttype

#This errors for me:  TypeError: argument of type 'MimePart' is not iterable
if attachmenttype:
    if '(text/html)' in attachmenttype:
        print "woohoo"

以下是我得到的答复: myerror

提前谢谢。你知道吗


Tags: fromcommessagereadstringemailtypemsg
1条回答
网友
1楼 · 发布于 2024-06-07 00:45:00

attachmenttype可以打印一个字符串,但它不是一个字符串,而是一个包含一些属性的结构。但是,既然你能打印出来,你就完成了一半。只需使用str将其转换为字符串并进行比较。你知道吗

像这样修复代码。我无法测试它,但我不知道它怎么会不起作用:

if attachmenttype:
    if '(text/html)' in str(attachmenttype):
        print("woohoo")

相关问题 更多 >