Python 中 MIME 与 HTML 处理详解
1. MIME 与邮件格式处理
MIME(多用途互联网邮件扩展)在邮件处理中扮演着重要角色。下面是一个将指定源目录下的所有文件打包成适合邮件发送的文件的示例代码:
def pack_mail(source_dir, **headers): ''' Given source_dir, a string that is a path to an existing, readable directory, and arbitrary header name/value pairs passed in as named arguments, packs all the files directly under source_dir (assumed to be plain text files) into a mail message returned as a string. ''' msg = email.Message.Message( ) for name, value in headers.iteritems( ): msg[name] = value msg['Content-type'] = 'multipart/mixed' filenames = os.walk(source_dir).next( )[-1] for filename in filenames: m = email.Message.Message( ) m.ad