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

  • SEARCH
  • Home
  • 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 8979363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:49:55+00:00 2026-06-15T19:49:55+00:00

I have an email client in Django. Currently supporting GMail accounts using imaplib. My

  • 0

I have an email client in Django. Currently supporting GMail accounts using imaplib.

My problem is: I want to obtain the attachment names without having to download the full email. Currently, in order to obtain the attachment names, or the email body, I need to download the whole email using the fetch function with the parameter (RFC822).

I know I can obtain specific fields only using HEADER.FIELDS, for the subject, from, cc for example. But is there a way to obtain the attachment names or the email body without downloading the whole email?

What I mean specifically is: let’s say I have a 30Mb email that has one line of text in the body and two 15Mb attachments. I want to obtain the attachment names and that line of text without downloading the full 30Mb body.

Thank you

  • 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-06-15T19:49:56+00:00Added an answer on June 15, 2026 at 7:49 pm

    Assuming you’re asking what I think you’re asking, here’s what to do:

    First, fetch the BODYSTRUCTURE. Assuming gmail’s IMAP server supports this, you’ll get back something like this:

    (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "QUOTED-PRINTABLE" 56 1 NIL NIL NIL NIL)
     ("TEXT" "HTML" ("CHARSET" "UTF-8") (NAME "") NIL NIL "BASE64" 12345 NIL 
      ("attachment" ("FILENAME" "")) NIL NIL) 
     ("IMG" "JPEG" (NAME "funny picture") NIL NIL "BASE64" 56789 NIL
      ("attachment" ("FILENAME" "image.jpg")) NIL NIL))
     "MIXED" ("BOUNDARY" "----_=_NextPart_001_1234ABCD.56789EF0") NIL NIL NIL)
    

    And then fetch the (BODY ENVELOPE) is the structure has one.

    If you look at RFC3501 7.4.2, it explains how to deal with these.

    Once you’ve determined that the (BODY[1]) and (BODY[2]) are the plain-text and HTML versions of the main content, and (BODY[3]) is the first real attachment, you download the plain-text body by fetching (BODY[1]), and you’ve got the name of the attachment from the structure.

    Sorry there’s no code here. I don’t think either imaplib or any of the stdlib MIME- and mail-related modules will do the hard part for you (interpreting the structure), but I haven’t actually checked, so I’d look there first, and, if not, go to PyPI to see if anyone else has already written the code.

    Well, actually, first I’d just fetch BODYSTRUCTURE, (BODY ENVELOPE) and (BODY[3]) for a specific message to make sure gmail has complete support before writing a whole mess of code…

    PS, if worst comes to worst, if your use case is as simple and rigid as you described, you can just always fetch BODYSTRUCTURE and (BODY[1]), fall back to RFC822 if that fails, and get the attachment names by running a hacky regexp on the structure instead of a real parse. I wouldn’t write this for anything but a one-shot script or a quick&dirty prototype to learn about gmail, but for those cases, I probably would.

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

Sidebar

Related Questions

I have email addresses like user1@gmail.com , user2@ymail.com user3@hotmail.com ... etc. I want a
Am using django forms on my app. I have a model Client like this:
I am trying to send an email using the Python email client. I have
Using javascript to open email. Email client comes up but no attachment. Can anyone
I have some tables roughly like so: Client: id name Employee id name Email
I have code for sending email to clients. System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); MailMessage
I have an email attachment form that is working fine when the <form action=processingtheformfile.php>
I have multiple email addresses linked to my gmail account. I am signed up
I currently have a client website on the Grid server on MediaTemple that will
I have to build an email sending sending program and now i want to

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.