Question

I 'am using imaplib(python) to login into gmail inbox and searching for appropriate messages.
But when I'am printing those message, links inside the message body seems to be broken.
With '3D' appended randomly.

No correct solution

OTHER TIPS

'3D' is the hexadecimal encoding of '='. So the problem is that you aren't properly decoding the email, which can be done using python's email module and message.get_payload(decode=True).

Here's a short snippet:

import imaplib, email
imap_server = "imap.aol.com" #maybe this would be imap.gmail.com for gmail?
conn = imaplib.IMAP4_SSL(imap_server, 993)
conn.login(username, password)
conn.select()
resp, data = conn.uid('FETCH', '1:*' , '(RFC822)')
raw = data[0][1].strip()
message = email.message_from_string(raw)

decoded = message.get_payload(decode=True) #this will be the decoded body of the email message

Try Imbox

Because imaplib is a very excessive low level library and returns results which are hard to work with

Installation

pip install imbox

Usage

from imbox import Imbox

with Imbox('imap.gmail.com',
        username='username',
        password='password',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:

    all_inbox_messages = imbox.messages()
    for uid, message in all_inbox_messages:
        message.sent_from
        message.sent_to
        message.subject
        message.headers
        message.message_id
        message.date
        message.body.plain
        message.body.html
        message.attachments
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top