I am using imaplib to read gmail messages in my python command window. The only problem is if that the emails come with with newlines and return carriages. Also, the text does not seem to be formatted correct. Instead of Amount: $36.49, it returns =2436.49. How can I go about cleaning up this text? Thanks!
Sample email content:
r\nItem name: Scanner\r\nItem=23: 130585100869\r\nPurchase Date: Oct 7, 2011\r\nUnit Price: =2436.49 USD\r\nQty: 1\r\nAmount: =2436.49USD\r\nSubtotal: =2436.49 USD\r\nShipping and handling: =240.00 USD\r\nInsurance - not offered
Code:
import imaplib
import libgmail
import re
import email
from BeautifulSoup import BeautifulSoup
USER = 'email@gmail.com'
PASSWORD = 'password'
#connecting to the gmail imap server
imap_server = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap_server.login(USER, PASSWORD)
imap_server.select('Inbox')
typ, response = imap_server.search(None, '(SUBJECT "payment received")')
Data = []
for i in response[0].split():
results, data = imap_server.fetch(i, "(RFC822)")
Data.append(data)
break
for i in Data:
print i
The
\r\nissueThe
\r\nproblem is caused by you not printing strings, but internal representations thereof. Try this to understand what I mean:The
ithat you print above is a list of strings, so first representation kicks in. Try this:I identified this by inspection of the object — you should read the documentation of the libraries you are using to understand what exactly this object is composed of to understand why specifically this field represents the message. Or how to convert the
Dataobject to something more… palatable.The encoding issue
Try: