SyntaxError: invalid syntax (help)

Trying to search/delete for specific user with one letter name, “a”, and always changing domains.
Some addresses from this spam:

a@kragar.imentss.com
a@octobertres.com
a@cngruel.com

Need help please, fixing invalid syntax error.

CODE:

from imap_tools import MailBox, A
 
with MailBox('imap.mail.yahoo.com').login('name@yahoo.com', 'PWD', 'Bulk') as mailbox:
    DELETE messages that contains 'a@*.*' in body from Bulk folder
    mailbox.delete(mailbox.fetch(A(body='a@*.*')))

ERROR:

λ python DeleteEmailFiles.py
  File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 5
    DELETE messages that contains 'a@*.*' in body from Bulk folder
           ^^^^^^^^
SyntaxError: invalid syntax
1 Like

My guess is that line is supposed to be a comment.
Comment it out and try again to run the code

2 Likes

Correct, sorry didn’t notice that.

New error:

λ python DeleteEmailFiles.py
Traceback (most recent call last):
  File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 6, in <module>
    mailbox.delete(mailbox.fetch(A(body='a@*.*')))
  File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\site-packages\imap_tools\mailbox.py", line 176, in delete
    uid_str = clean_uids(uid_list)
  File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\site-packages\imap_tools\utils.py", line 30, in clean_uids
    raise TypeError('uid "{}" is not string'.format(str(uid)))
TypeError: uid "<imap_tools.message.MailMessage object at 0x000001A96BC7BD90>" is not string

either the fetch or the delete is expecting a string, and got a fully formed object fed into it instead.

1 Like

@m_hutley thank you for responding, but since I’m a newbie to Python, I have no idea how to use your advice to fix code.

From https://pypi.org/project/imap-tools/0.1.8/ :

# DELETE all messages from current dir to folder1, *in bulk
mailbox.delete([msg.uid for msg in mailbox.fetch()])

Try changing your mailbox.delete statement like this example (using your fetch)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.