python: extract and download an image from the body of the email -
i trying read content of email. if email contains image in body part need extract , save in local here code getting body content
def get_body(id): res, mail_data = connection.fetch(id, '(rfc822)') raw_email=mail_data[0][1] email_message_instance = email.message_from_string(raw_email) whole_body = '' part in email_message_instance.walk(): if part.get_content_type() == "text/plain": # ignore attachments/html body = part.get_payload(decode=true) whole_body += body + ' ' else: continue return whole_body
found solution.
def get_body(id): """get body of email""" res, mail_data = connection.fetch(id, '(rfc822)') raw_email=mail_data[0][1] email_message_instance = email.message_from_string(raw_email) whole_body = '' part in email_message_instance.walk(): if part.get_content_type() == "text/plain": # ignore attachments/html body = part.get_payload(decode=true) print "body" + body whole_body += body + ' ' if part.get_content_maintype() != 'multipart' , part.get('content-disposition') not none: print "image content" image = part.get_filename().split('.') image_name = image[0] + id + "." + image[1] open(e: + '/' + image_name, 'wb').write(part.get_payload(decode=true)) else: continue return whole_body
Comments
Post a Comment