Read email in python 3.7 using imaplib with HTML body and attachments in the email












1















I would really appreciate if someone can help me with this issue.



I have implemented the below code to read "unread emails from gmail inbox". I need to print "To", "From", "Subject", "Body" and "save attachments in a specified location"



I have 2 issues here.




  1. If there is any email with attachments, it gives the error Body: [<email.message.Message object at 0x026D1050>, <email.message.Message object at 0x02776B70>]. It will print all the required things and saves attachments but DOESN'T print the body.


This works fine if no attachment is included.




  1. If there is an email body with any styling in it like "bold/italic/underline/colour...etc", it doesn't print as it is.


Example : Python is printed as Python=C2=A0i= and sometimes different styling is seperated by "*".



def get_body(email_message):
for payload in email_message.get_payload():
# print('Body:t', payload.get_payload())
break
return(payload.get_payload())
def read_email(server,uname,pwd):
username = uname
password = pwd
mail = imaplib.IMAP4_SSL(server)
mail.login(username, password)
mail.select("inbox")
try:
result, data = mail.uid('search', None, '(UNSEEN)')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
raw_email = email_data[0][1].decode("UTF-8")
email_message = email.message_from_string(raw_email)
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(location, filename)

if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print('Downloaded file:', filename)
if email_message.is_multipart():
for payload in email_message.get_payload():
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t',email_message['Date'])
print('Body:t', get_body(email_message))
break
else:
print('Nothing'])
except IndexError:
print("No new email")
while True:
read_email("imap.gmail.com", "s@gmail.com", "spassword")
time.sleep(10)


Many thanks










share|improve this question























  • It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

    – stovfl
    Nov 23 '18 at 11:28











  • Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

    – user8301179
    Nov 23 '18 at 16:45













  • " the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

    – stovfl
    Nov 23 '18 at 16:57











  • I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

    – user8301179
    Nov 27 '18 at 10:05


















1















I would really appreciate if someone can help me with this issue.



I have implemented the below code to read "unread emails from gmail inbox". I need to print "To", "From", "Subject", "Body" and "save attachments in a specified location"



I have 2 issues here.




  1. If there is any email with attachments, it gives the error Body: [<email.message.Message object at 0x026D1050>, <email.message.Message object at 0x02776B70>]. It will print all the required things and saves attachments but DOESN'T print the body.


This works fine if no attachment is included.




  1. If there is an email body with any styling in it like "bold/italic/underline/colour...etc", it doesn't print as it is.


Example : Python is printed as Python=C2=A0i= and sometimes different styling is seperated by "*".



def get_body(email_message):
for payload in email_message.get_payload():
# print('Body:t', payload.get_payload())
break
return(payload.get_payload())
def read_email(server,uname,pwd):
username = uname
password = pwd
mail = imaplib.IMAP4_SSL(server)
mail.login(username, password)
mail.select("inbox")
try:
result, data = mail.uid('search', None, '(UNSEEN)')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
raw_email = email_data[0][1].decode("UTF-8")
email_message = email.message_from_string(raw_email)
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(location, filename)

if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print('Downloaded file:', filename)
if email_message.is_multipart():
for payload in email_message.get_payload():
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t',email_message['Date'])
print('Body:t', get_body(email_message))
break
else:
print('Nothing'])
except IndexError:
print("No new email")
while True:
read_email("imap.gmail.com", "s@gmail.com", "spassword")
time.sleep(10)


Many thanks










share|improve this question























  • It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

    – stovfl
    Nov 23 '18 at 11:28











  • Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

    – user8301179
    Nov 23 '18 at 16:45













  • " the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

    – stovfl
    Nov 23 '18 at 16:57











  • I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

    – user8301179
    Nov 27 '18 at 10:05
















1












1








1








I would really appreciate if someone can help me with this issue.



I have implemented the below code to read "unread emails from gmail inbox". I need to print "To", "From", "Subject", "Body" and "save attachments in a specified location"



I have 2 issues here.




  1. If there is any email with attachments, it gives the error Body: [<email.message.Message object at 0x026D1050>, <email.message.Message object at 0x02776B70>]. It will print all the required things and saves attachments but DOESN'T print the body.


This works fine if no attachment is included.




  1. If there is an email body with any styling in it like "bold/italic/underline/colour...etc", it doesn't print as it is.


Example : Python is printed as Python=C2=A0i= and sometimes different styling is seperated by "*".



def get_body(email_message):
for payload in email_message.get_payload():
# print('Body:t', payload.get_payload())
break
return(payload.get_payload())
def read_email(server,uname,pwd):
username = uname
password = pwd
mail = imaplib.IMAP4_SSL(server)
mail.login(username, password)
mail.select("inbox")
try:
result, data = mail.uid('search', None, '(UNSEEN)')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
raw_email = email_data[0][1].decode("UTF-8")
email_message = email.message_from_string(raw_email)
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(location, filename)

if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print('Downloaded file:', filename)
if email_message.is_multipart():
for payload in email_message.get_payload():
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t',email_message['Date'])
print('Body:t', get_body(email_message))
break
else:
print('Nothing'])
except IndexError:
print("No new email")
while True:
read_email("imap.gmail.com", "s@gmail.com", "spassword")
time.sleep(10)


Many thanks










share|improve this question














I would really appreciate if someone can help me with this issue.



I have implemented the below code to read "unread emails from gmail inbox". I need to print "To", "From", "Subject", "Body" and "save attachments in a specified location"



I have 2 issues here.




  1. If there is any email with attachments, it gives the error Body: [<email.message.Message object at 0x026D1050>, <email.message.Message object at 0x02776B70>]. It will print all the required things and saves attachments but DOESN'T print the body.


This works fine if no attachment is included.




  1. If there is an email body with any styling in it like "bold/italic/underline/colour...etc", it doesn't print as it is.


Example : Python is printed as Python=C2=A0i= and sometimes different styling is seperated by "*".



def get_body(email_message):
for payload in email_message.get_payload():
# print('Body:t', payload.get_payload())
break
return(payload.get_payload())
def read_email(server,uname,pwd):
username = uname
password = pwd
mail = imaplib.IMAP4_SSL(server)
mail.login(username, password)
mail.select("inbox")
try:
result, data = mail.uid('search', None, '(UNSEEN)')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
raw_email = email_data[0][1].decode("UTF-8")
email_message = email.message_from_string(raw_email)
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(location, filename)

if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print('Downloaded file:', filename)
if email_message.is_multipart():
for payload in email_message.get_payload():
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t',email_message['Date'])
print('Body:t', get_body(email_message))
break
else:
print('Nothing'])
except IndexError:
print("No new email")
while True:
read_email("imap.gmail.com", "s@gmail.com", "spassword")
time.sleep(10)


Many thanks







python email imaplib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 9:54







user8301179




















  • It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

    – stovfl
    Nov 23 '18 at 11:28











  • Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

    – user8301179
    Nov 23 '18 at 16:45













  • " the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

    – stovfl
    Nov 23 '18 at 16:57











  • I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

    – user8301179
    Nov 27 '18 at 10:05





















  • It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

    – stovfl
    Nov 23 '18 at 11:28











  • Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

    – user8301179
    Nov 23 '18 at 16:45













  • " the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

    – stovfl
    Nov 23 '18 at 16:57











  • I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

    – user8301179
    Nov 27 '18 at 10:05



















It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

– stovfl
Nov 23 '18 at 11:28





It's bad pratice, to enclose more as needed with a try ... except block. Try the pattern in this Answer python-imaplib-html-body-parsing

– stovfl
Nov 23 '18 at 11:28













Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

– user8301179
Nov 23 '18 at 16:45







Thanks for the reply. I have made it simple now. But have only 1 issue now. It doesn't print the body as it is if it's not text/plain. I have used type() to see what type is the body. It's like plain/text / list/ str.. Different types.. How do we handle this

– user8301179
Nov 23 '18 at 16:45















" the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

– stovfl
Nov 23 '18 at 16:57





" the body as it is if it's not text/plain": Edit your Question and show the first 100 chars.

– stovfl
Nov 23 '18 at 16:57













I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

– user8301179
Nov 27 '18 at 10:05







I have somehow managed to get the body now but I can only get the text. All the images are just displayed with URLs/styling formats which is not readable. How can this be solved?

– user8301179
Nov 27 '18 at 10:05














1 Answer
1






active

oldest

votes


















0














I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.



import email
import imaplib
import os
import html2text
import time
detach_dir = 'locationWhereYouWantToSaveYourAttachments'


def get_body(email_message):
for payload in email_message.get_payload():
break
return payload.get_payload()

def two_way_email(server,uname,pwd):
username = uname
password = pwd
mail = imaplib.IMAP4_SSL(server)
mail.login(username, password)
mail.select("inbox")
try:
result, data = mail.uid('search', None, '(UNSEEN)')
inbox_item_list = data[0].split()
most_recent = inbox_item_list[-1]
result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
raw_email = email_data[0][1].decode("UTF-8")
email_message = email.message_from_string(raw_email)

for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue

filename = part.get_filename()
att_path = os.path.join(detach_dir, filename)

if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print('Downloaded file:', filename)
if email_message.is_multipart():
for payload in email_message.get_payload():
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t',email_message['Date'])
for part in email_message.walk():
if (part.get_content_type() == 'text/plain') and (part.get('Content-Disposition') is None):
print('Body:t',part.get_payload())
break
else:
print('To:tt', email_message['To'])
print('From:t', email_message['From'])
print('Subject:', email_message['Subject'])
print('Date:t', email_message['Date'])
print('Thread-Index:t', email_message['Thread-Index'])
text = f"{email_message.get_payload(decode=True)}"
html = text.replace("b'", "")
h = html2text.HTML2Text()
h.ignore_links = True
output = (h.handle(f'''{html}''').replace("\r\n", ""))
output = output.replace("'", "")
print(output)

except IndexError:
print("No new email")
while True:
two_way_email("outlook.office365.com", "yourOffice365EmailAddressHere", "yourpassword")
two_way_email("imap.gmail.com", "yourGmailAddressHere", "yourPassword")
time.sleep(10)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444318%2fread-email-in-python-3-7-using-imaplib-with-html-body-and-attachments-in-the-ema%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.



    import email
    import imaplib
    import os
    import html2text
    import time
    detach_dir = 'locationWhereYouWantToSaveYourAttachments'


    def get_body(email_message):
    for payload in email_message.get_payload():
    break
    return payload.get_payload()

    def two_way_email(server,uname,pwd):
    username = uname
    password = pwd
    mail = imaplib.IMAP4_SSL(server)
    mail.login(username, password)
    mail.select("inbox")
    try:
    result, data = mail.uid('search', None, '(UNSEEN)')
    inbox_item_list = data[0].split()
    most_recent = inbox_item_list[-1]
    result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
    raw_email = email_data[0][1].decode("UTF-8")
    email_message = email.message_from_string(raw_email)

    for part in email_message.walk():
    if part.get_content_maintype() == 'multipart':
    continue
    if part.get('Content-Disposition') is None:
    continue

    filename = part.get_filename()
    att_path = os.path.join(detach_dir, filename)

    if not os.path.isfile(att_path):
    fp = open(att_path, 'wb')
    fp.write(part.get_payload(decode=True))
    fp.close()
    print('Downloaded file:', filename)
    if email_message.is_multipart():
    for payload in email_message.get_payload():
    print('To:tt', email_message['To'])
    print('From:t', email_message['From'])
    print('Subject:', email_message['Subject'])
    print('Date:t',email_message['Date'])
    for part in email_message.walk():
    if (part.get_content_type() == 'text/plain') and (part.get('Content-Disposition') is None):
    print('Body:t',part.get_payload())
    break
    else:
    print('To:tt', email_message['To'])
    print('From:t', email_message['From'])
    print('Subject:', email_message['Subject'])
    print('Date:t', email_message['Date'])
    print('Thread-Index:t', email_message['Thread-Index'])
    text = f"{email_message.get_payload(decode=True)}"
    html = text.replace("b'", "")
    h = html2text.HTML2Text()
    h.ignore_links = True
    output = (h.handle(f'''{html}''').replace("\r\n", ""))
    output = output.replace("'", "")
    print(output)

    except IndexError:
    print("No new email")
    while True:
    two_way_email("outlook.office365.com", "yourOffice365EmailAddressHere", "yourpassword")
    two_way_email("imap.gmail.com", "yourGmailAddressHere", "yourPassword")
    time.sleep(10)





    share|improve this answer




























      0














      I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.



      import email
      import imaplib
      import os
      import html2text
      import time
      detach_dir = 'locationWhereYouWantToSaveYourAttachments'


      def get_body(email_message):
      for payload in email_message.get_payload():
      break
      return payload.get_payload()

      def two_way_email(server,uname,pwd):
      username = uname
      password = pwd
      mail = imaplib.IMAP4_SSL(server)
      mail.login(username, password)
      mail.select("inbox")
      try:
      result, data = mail.uid('search', None, '(UNSEEN)')
      inbox_item_list = data[0].split()
      most_recent = inbox_item_list[-1]
      result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
      raw_email = email_data[0][1].decode("UTF-8")
      email_message = email.message_from_string(raw_email)

      for part in email_message.walk():
      if part.get_content_maintype() == 'multipart':
      continue
      if part.get('Content-Disposition') is None:
      continue

      filename = part.get_filename()
      att_path = os.path.join(detach_dir, filename)

      if not os.path.isfile(att_path):
      fp = open(att_path, 'wb')
      fp.write(part.get_payload(decode=True))
      fp.close()
      print('Downloaded file:', filename)
      if email_message.is_multipart():
      for payload in email_message.get_payload():
      print('To:tt', email_message['To'])
      print('From:t', email_message['From'])
      print('Subject:', email_message['Subject'])
      print('Date:t',email_message['Date'])
      for part in email_message.walk():
      if (part.get_content_type() == 'text/plain') and (part.get('Content-Disposition') is None):
      print('Body:t',part.get_payload())
      break
      else:
      print('To:tt', email_message['To'])
      print('From:t', email_message['From'])
      print('Subject:', email_message['Subject'])
      print('Date:t', email_message['Date'])
      print('Thread-Index:t', email_message['Thread-Index'])
      text = f"{email_message.get_payload(decode=True)}"
      html = text.replace("b'", "")
      h = html2text.HTML2Text()
      h.ignore_links = True
      output = (h.handle(f'''{html}''').replace("\r\n", ""))
      output = output.replace("'", "")
      print(output)

      except IndexError:
      print("No new email")
      while True:
      two_way_email("outlook.office365.com", "yourOffice365EmailAddressHere", "yourpassword")
      two_way_email("imap.gmail.com", "yourGmailAddressHere", "yourPassword")
      time.sleep(10)





      share|improve this answer


























        0












        0








        0







        I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.



        import email
        import imaplib
        import os
        import html2text
        import time
        detach_dir = 'locationWhereYouWantToSaveYourAttachments'


        def get_body(email_message):
        for payload in email_message.get_payload():
        break
        return payload.get_payload()

        def two_way_email(server,uname,pwd):
        username = uname
        password = pwd
        mail = imaplib.IMAP4_SSL(server)
        mail.login(username, password)
        mail.select("inbox")
        try:
        result, data = mail.uid('search', None, '(UNSEEN)')
        inbox_item_list = data[0].split()
        most_recent = inbox_item_list[-1]
        result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
        raw_email = email_data[0][1].decode("UTF-8")
        email_message = email.message_from_string(raw_email)

        for part in email_message.walk():
        if part.get_content_maintype() == 'multipart':
        continue
        if part.get('Content-Disposition') is None:
        continue

        filename = part.get_filename()
        att_path = os.path.join(detach_dir, filename)

        if not os.path.isfile(att_path):
        fp = open(att_path, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        print('Downloaded file:', filename)
        if email_message.is_multipart():
        for payload in email_message.get_payload():
        print('To:tt', email_message['To'])
        print('From:t', email_message['From'])
        print('Subject:', email_message['Subject'])
        print('Date:t',email_message['Date'])
        for part in email_message.walk():
        if (part.get_content_type() == 'text/plain') and (part.get('Content-Disposition') is None):
        print('Body:t',part.get_payload())
        break
        else:
        print('To:tt', email_message['To'])
        print('From:t', email_message['From'])
        print('Subject:', email_message['Subject'])
        print('Date:t', email_message['Date'])
        print('Thread-Index:t', email_message['Thread-Index'])
        text = f"{email_message.get_payload(decode=True)}"
        html = text.replace("b'", "")
        h = html2text.HTML2Text()
        h.ignore_links = True
        output = (h.handle(f'''{html}''').replace("\r\n", ""))
        output = output.replace("'", "")
        print(output)

        except IndexError:
        print("No new email")
        while True:
        two_way_email("outlook.office365.com", "yourOffice365EmailAddressHere", "yourpassword")
        two_way_email("imap.gmail.com", "yourGmailAddressHere", "yourPassword")
        time.sleep(10)





        share|improve this answer













        I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.



        import email
        import imaplib
        import os
        import html2text
        import time
        detach_dir = 'locationWhereYouWantToSaveYourAttachments'


        def get_body(email_message):
        for payload in email_message.get_payload():
        break
        return payload.get_payload()

        def two_way_email(server,uname,pwd):
        username = uname
        password = pwd
        mail = imaplib.IMAP4_SSL(server)
        mail.login(username, password)
        mail.select("inbox")
        try:
        result, data = mail.uid('search', None, '(UNSEEN)')
        inbox_item_list = data[0].split()
        most_recent = inbox_item_list[-1]
        result2, email_data = mail.uid('fetch', most_recent, '(RFC822)')
        raw_email = email_data[0][1].decode("UTF-8")
        email_message = email.message_from_string(raw_email)

        for part in email_message.walk():
        if part.get_content_maintype() == 'multipart':
        continue
        if part.get('Content-Disposition') is None:
        continue

        filename = part.get_filename()
        att_path = os.path.join(detach_dir, filename)

        if not os.path.isfile(att_path):
        fp = open(att_path, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        print('Downloaded file:', filename)
        if email_message.is_multipart():
        for payload in email_message.get_payload():
        print('To:tt', email_message['To'])
        print('From:t', email_message['From'])
        print('Subject:', email_message['Subject'])
        print('Date:t',email_message['Date'])
        for part in email_message.walk():
        if (part.get_content_type() == 'text/plain') and (part.get('Content-Disposition') is None):
        print('Body:t',part.get_payload())
        break
        else:
        print('To:tt', email_message['To'])
        print('From:t', email_message['From'])
        print('Subject:', email_message['Subject'])
        print('Date:t', email_message['Date'])
        print('Thread-Index:t', email_message['Thread-Index'])
        text = f"{email_message.get_payload(decode=True)}"
        html = text.replace("b'", "")
        h = html2text.HTML2Text()
        h.ignore_links = True
        output = (h.handle(f'''{html}''').replace("\r\n", ""))
        output = output.replace("'", "")
        print(output)

        except IndexError:
        print("No new email")
        while True:
        two_way_email("outlook.office365.com", "yourOffice365EmailAddressHere", "yourpassword")
        two_way_email("imap.gmail.com", "yourGmailAddressHere", "yourPassword")
        time.sleep(10)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 15:05







        user8301179







































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444318%2fread-email-in-python-3-7-using-imaplib-with-html-body-and-attachments-in-the-ema%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga