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 4115668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:36:43+00:00 2026-05-20T22:36:43+00:00

How can I distinguish sender-generated carriage returns from word wrap auto-generated carriage returns in

  • 0

How can I distinguish sender-generated carriage returns from word wrap auto-generated carriage returns in an email body? I’m using Python imaplib to access Gmail and download message bodies like so:

user='whoever@gmail.com'
pwd='password'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)
m.select("INBOX")
resp, items = m.search(None, "ALL")
items = items[0].split()
messages = []
for emailid in items:
    resp, data = m.fetch(emailid, "(RFC822)")
    email_body = data[0][1]
    mail = email.message_from_string(email_body)
    for part in mail.walk():
        if part.get_content_type() == 'text/plain':
            body = part.get_payload(decode=1)
            messages.append(body)

I’m focusing on the case of messages received from another Gmail user. The message body text has a number of carriage returns (‘\r\n’) in it. These fall into two classes: 1) those inserted by the sender of the email, the “true” returns, 2) those created by Gmail word wrapping at ~78 characters, the “false” returns. I want to remove the second class of carriage returns only. I’m sure I could come up with a programmatic approximation that searches for the ‘\r\n’ at a window around every 78th character but that wouldn’t be bulletproof and isn’t what I want. Interestingly, I notice that when the message displays in Gmail in the web browser, there are not returns for the second class of carriage returns. Gmail somehow knows to remove/not display these specifically. How? Is there some special encoding I’m missing?

  • 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-20T22:36:44+00:00Added an answer on May 20, 2026 at 10:36 pm

    Gmail sends messages in both the MIME multipart format, in both a text/plain version (what you are grabbing) and a text/html version. The latter version is what contains fancy formatting like bold, italic, links, etc., and is what Gmail displays. While the text/html version is also line-broken at 78 characters (a part of the e-mail standard — the underlying text must never have a line exceeding 78 characters), the “real” line breaks that you are looking for are embedded therein as HTML <br> tags. You can see this yourself if you send yourself a message and then, using the little down-arrow next to the Reply button, click “Show original”.

    You cannot distinguish between “fake” and “real” line-breaks in the text/plain version of the message, at least not reliably (as you obviously know). You can, however, pull the text/html version instead, knowing then that the “real” line-breaks are the <br> tags, however you then have to deal with the additional HTML (as well as first correctly processing the “Content-Transfer-Encoding” used therein).

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

Sidebar

Related Questions

I'm looking at things that can distinguish a blog from a normal website. These
In Windows you can distinguish the 'move' drag operation from a 'copy' drag operation
Can I distinguish signal, between delivered directly to a process and delivered via debugger.
I can not distinguish the difference between aggregating and holding. What does it mean
There seems to be a bug in the Java varargs implementation. Java can't distinguish
I'm trying to find a CSS selector that can help me distinguish between the
How can I get C# to distinguish between ambiguous class types without having to
How can I get the distinguished name from Active Directory of the currently logged
Can anyone tell me how to hide the header in DevExpress gridcontrol..?? I'm using
Scenario: I am parsing values from an XML file using C# and have the

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.