I tried to fetch a message content in the following way
result, data = m.uid('fetch', num, "( FLAGS BODY.PEEK[HEADER.FIELDS (SUBJECT FROM DATE)] BODYSTRUCTURE)")
It worked well when I was connecting to a private mail server "mail.example.com"
But it returns exception when I used "imap.gmail.com"
error: UID command error: BAD ['Could not parse command']
I think gmail doesnot support detailed search like HEADER.FIELDS....
So I tried the following option for gmail server and it worked really well
result, data = m.uid('fetch', num, "(FLAGS BODY.PEEK[HEADER] BODYSTRUCTURE)")
Related
I am trying to fetch email from Yahoo mail.
msg = imap.fetch(str(fromMail[-1]), "(RFC822)")
When the above code it throws the following error but the the same is working for Gmail. Any clue what need to be changed for Yahoo mail.
imaplib.IMAP4.error: FETCH command error: BAD [b'[CLIENTBUG] FETCH Command arguments invalid']
Test code:
fetch_criteria = AND(seen=False), AND(NOT(uid=['61','88']))
for message in mailbox.fetch(fetch_criteria, mark_seen=False, charset='utf8', limit=10):
print(message.uid)
Server response:
SEARCH command error: BAD [b'Could not parse command']
Please help me correctly create the conditions to exclude emails with UID 66 and 88
from imap_tools import AND, NOT
mailbox.fetch(AND(NOT(uid=['39', '40'])))
mailbox.fetch(NOT(AND(uid=['39', '40'])))
Both works for me at [yandex, google, outlook, zimbra].
May be your server can not do this.
Error example at mail.ru: imap_tools.errors.MailboxSearchError: Response status "OK" expected, but "NO" received. Data: [b'[CANNOT] Unsupported search criterion: (UID 5,6)']
Lib author.
I'm trying to import an email with a particular search string from the server. I'm using the following code.
library(rPython)
python.exec("import imaplib
import email")
python.exec('
m=imaplib.IMAP4("imap.myserver.com","143")
m.login(r"myid","mypass")
m.select()')
python.exec('r,data=m.search(None,\'HEADER From "*cds*"\')')
a = python.get("data")
but it seems to not like the escaped apostrophes. I get the message
Error in python.exec("r,data=m.search(None,'HEADER From \"*cds*\"')") :
SEARCH command error: BAD ['unable to handle request: Invalid search parameters: "HEADER FROM \\"*CDS*\\""']
Can anyone help me figure out how to get this right? Thanks
I'm using imaplib and attempting to parse messages from a gmail acct. My code has worked for months, and now all of the sudden it's failing miserably. I have no idea what to attribute this to.
The following works around 1/3 of the time. By 'works', I mean succeeds in printing something other than 'no new messages' when I receive an email. Would anyone have any suggestions of more robust ways to attempt this? Or maybe suggestions of now to configure gmail accounts to get this to work more reliably?
I'm also generally interested if the way I've coded this seems like good practice.
Thanks for any and all help...
def check_email(interval):
while True:
server.select('INBOX')
status, ids = server.search(None, 'UnSeen')
if not ids or ids[0] is '':
print 'no new messages'
else:
print 'found a message; attempting to parse...'
latest_id = ids[0]
status, msg_data = server.fetch(latest_id, '(UID BODY[TEXT])')
raw_data = msg_data[0][1]
char_array = list(raw_data)
print 'message result: ', char_array
time.sleep(interval)
EDIT1: I am now getting the following error:
"imaplib.error: FETCH command error: BAD ['Could not parse command']"
Does anyone know what I can attribute this to? It's apparently a result of the line
status, msg_data = server.fetch(latest_id, '(UID BODY[TEXT])')
EDIT2: I found out that I can log into the gmail account, click on the 'more' tab, then click on 'mark all as read' and all of the sudden the code works as expected. Is there a way to mark all messages as read remotely with imaplib??
I am using this with Gmail's SMTP server, and I would like to search via IMAP for emails either sent to or received from an address.
This is what I have:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user', 'pass')
mail.list()
mail.select("[Gmail]/All Mail")
status, email_ids = mail.search(None, 'TO "tech163#fusionswift.com" OR FROM "tech163#fusionswift.com"')
The last line of the error is: imaplib.error: SEARCH command error: BAD ['Could not parse command']
Not sure how I'm supposed to do that kind of OR statement within python's imaplib. If someone can quickly explain what's wrong or point me in the right direction, it'd be greatly appreciated.
The error you are receiving is generated from the server because it can't parse the search query correctly. In order to generate a valid query follow the RFC 3501, in page 49 it is explained in detail the structure.
For example your search string to be correct should be:
'(OR (TO "tech163#fusionswift.com") (FROM "tech163#fusionswift.com"))'
Try to use IMAP query builder from https://github.com/ikvk/imap_tools
from imap_tools import A, AND, OR, NOT
# AND
A(text='hello', new=True) # '(TEXT "hello" NEW)'
# OR
OR(text='hello', date=datetime.date(2000, 3, 15)) # '(OR TEXT "hello" ON 15-Mar-2000)'
# NOT
NOT(text='hello', new=True) # 'NOT (TEXT "hello" NEW)'
# complex
A(OR(from_='from#ya.ru', text='"the text"'), NOT(OR(A(answered=False), A(new=True))), to='to#ya.ru')
Of course you can use all library tools