James
James

Reputation: 490

Encode() Argument Error When Fetching Emails

I am trying to fetch emails using imap_tools. I want to limit the fetch to new emails that contain the word "order" in the subject but do not contain any of the words "stock", "return" and "delivery" in the subject.

I thought this line would do it but I get the error below.

ubox = MailBox(yahooSmtpServer).login(email, password, 'INBOX')

msgs = ubox.fetch(AND(new=True, subject='order'), NOT(OR(subject=['stock', 'return', 'delivery'])))

encode() argument 'encoding' must be str, not NOT

Can anyone see where I have gone worng?

Upvotes: 0

Views: 130

Answers (1)

enzo
enzo

Reputation: 11496

When you pass the second parameter to fetch, you're passing the charset of the strings that appear in the search criteria. From docs:

BaseMailBox.fetch - first searches email nums by criteria in current folder, then fetch and yields MailMessage:

  • criteria = ‘ALL’, message search criteria, query builder
  • charset = ‘US-ASCII’, indicates charset of the strings that appear in the search criteria. See rfc2978

You should put all the conditions in the first parameter:

msgs = ubox.fetch(AND(NOT(OR(subject=['stock', 'return', 'delivery'])), new=True, subject='order'))

Upvotes: 3

Related Questions