我将您的问题理解为“我必须检查电子邮件的所有附件,但如果附件也是电子邮件,我想忽略它。” 无论哪种方式,这个答案都应该引导你走上正确的道路。
我想你想要的是mimetypes.guess_type()
. 使用此方法也比仅检查扩展列表要好得多。
def check(self, msg):
import mimetypes
for part in msg.walk():
if part.get_filename() is not None:
filenames = [n for n in part.getaltnames() if n]
for filename in filenames:
type, enc = mimetypes.guess_type(filename)
if type.startswith('message'):
print "This is an email and I want to ignore it."
else:
print "I want to keep looking at this file."
请注意,如果这仍然查看附加的电子邮件,请将其更改为:
def check(self, msg):
import mimetypes
for part in msg.walk():
filename = part.get_filename()
if filename is not None:
type, enc = mimetypes.guess_type(filename)
if type.startswith('message'):
print "This is an email and I want to ignore it."
else:
part_filenames = [n for n in part.getaltnames() if n]
for part_filename in part_filenames:
print "I want to keep looking at this file."
MIME 类型文档