Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:25:08+00:00 2026-05-23T02:25:08+00:00

I am pretty new to python and I am trying to parse email from

  • 0

I am pretty new to python and I am trying to parse email from gmail via python’s imaplib and email. It is working pretty well but I am having issues with email attachments.

I would like to parse out all of the plaintext from the email while ignoring any HTML that may be inserted as a secondary content type while also removing and saving all other attachments. I have been trying the following:

...imaplib connection and mailbox selection...

typ, msg_data = c.fetch(num, '(RFC822)')
        email_body = msg_data[0][1]
mail = email.message_from_string(email_body)
        for part in mail.walk():
            if part.get_content_type() == 'text/plain':
                body = body + '\n' + part.get_payload()
            else:
                continue

This was my original attempt to just take the plaintext portions of an email, but when someone sends an email with a text attachment, the contents of the text file shows up for the ‘body’ variable above.

Can someone tell me how I can extract the plaintext portions of an email while ignoring the secondary HTML that is sometimes present, while also saving all other types of file attachments as files? I appologize if this doesn’t make a lot of sense. I will update the question with more clarification if needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:25:09+00:00Added an answer on May 23, 2026 at 2:25 am

    If you just need to keep text attachments out of the body variable with what you have there, it should be as simple as this:

    mail = email.message_from_string(email_body)
        for part in mail.walk():
            c_type = part.get_content_type()
            c_disp = part.get('Content-Disposition')
    
            if c_type == 'text/plain' and c_disp == None:
                body = body + '\n' + part.get_payload()
            else:
                continue
    

    Then if the Content-Disposition indicates that it’s an attachment, you should be able to use part.get_filename() and part.get_payload() to handle the file. I don’t know if any of this can vary, but it’s basically what I’ve used in the past to interface with my mail server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to python and working with firmata I am trying to
I'm pretty new to Python and App Engine, but what I'm trying to do
I'm pretty new to Python, so hopefully the problem I'm having has a simple
I'm trying to use YUI compressor in a maven goal, but I'm pretty new
I am pretty new to Python world and trying to learn it. This is
I'm pretty new to python and ctypes. I'm trying to accomplish a seemingly easy
i'm pretty new to python and i'm having trouble with passing an argument into
I'm pretty new to python and am trying to grab the ropes and decided
I'm pretty new at python and I was wondering if this: def func(self, foo):
I'm pretty new to Python programming and would appreciate some help to a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.