Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse url from gmail using imaplib

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.

like image 455
zerode Avatar asked Jul 31 '26 05:07

zerode


1 Answers

'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
like image 119
Ponkadoodle Avatar answered Aug 01 '26 19:08

Ponkadoodle