SyntaxError: invalid syntax


Joined
Aug 16, 2022
Messages
10
Reaction score
0
Trying to search/delete for specific user with one letter name, "a", and always changing domains.
Some addresses from this spam:



Need help please, fixing invalid syntax error.

CODE:
Python:
from imap_tools import MailBox, A
 
with MailBox('imap.mail.yahoo.com').login('[email protected]', 'PWD', 'Bulk') as mailbox:
    DELETE messages that contains '[email protected]*.*' in body from Bulk folder
    mailbox.delete(mailbox.fetch(A(body='[email protected]*.*')))

ERROR:
Code:
λ python DeleteEmailFiles.py
  File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 5
    DELETE messages that contains '[email protected]*.*' in body from Bulk folder
           ^^^^^^^^
SyntaxError: invalid syntax
 
Last edited:
Ad

Advertisements

Joined
Aug 16, 2022
Messages
10
Reaction score
0
My bad, line needed to be commented out!
New error:
Code:
λ 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='[email protected]*.*')))
  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
 
Joined
Aug 31, 2022
Messages
5
Reaction score
0
Could try the following below examples

Python:
mailbox.delete(list(mailbox.fetch(A(body='[email protected]*.*'))))


if this doesnt work above you could try the method below

Python:
mailbox.delete([msg.uid for msg in mailbox.fetch(A(body='[email protected]*.*'))])
 
Joined
Aug 16, 2022
Messages
10
Reaction score
0
Could try the following below examples

Python:
mailbox.delete(list(mailbox.fetch(A(body='[email protected]*.*'))))


if this doesnt work above you could try the method below

Python:
mailbox.delete([msg.uid for msg in mailbox.fetch(A(body='[email protected]*.*'))])

@Narvuk thank you for wanting to help me resolve this!

Unfortunately both your suggestions caused script to freeze.
freeze.png
 
Joined
Aug 31, 2022
Messages
5
Reaction score
0
Have you checked to make sure folders are correct etc.
this is the default code for all messages in an inbox folder

Python:
from imap_tools import MailBox
# DELETE all messages from INBOX
with MailBox('imap.mail.com').login('[email protected]', 'password', 'INBOX') as mailbox:
    mailbox.delete([msg.uid for msg in mailbox.fetch()])

to expand on it could do, and is it definitely in the body or in the from element for the email
is bulk the folder you need ?

Python:
from imap_tools import MailBox, A
# DELETE all messages from INBOX that contain email in from
with MailBox('imap.mail.com').login('[email protected]', 'password', 'INBOX') as mailbox:
    mailbox.delete([msg.uid for msg in mailbox.fetch(A(from_='[email protected]*.*'))])

alternative unsure what your A is importing in but you can do without using just the from

Python:
from imap_tools import MailBox, A
# DELETE all messages from INBOX that contain email in from
with MailBox('imap.mail.com').login('[email protected]', 'password', 'INBOX') as mailbox:
    mailbox.delete([msg.uid for msg in mailbox.fetch(from_='[email protected]*.*')])
 
Last edited:
Joined
Aug 16, 2022
Messages
10
Reaction score
0
Yahoo defines the SPAM folder via any 3rd party access, as BULK.
Yes I need to search the BULK folder where all spam is.

Like I stated earlier, getting hit now with over 120 spam emails per day, from one person; same user with a one letter for a name, "A" and always changing gibberish domains;
 
Ad

Advertisements

Joined
Aug 31, 2022
Messages
5
Reaction score
0
ok i got you, you can try the below see if it works

try this with from_
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.from_])

or this with body
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.body])

or in the html
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.html])
 
Joined
Aug 16, 2022
Messages
10
Reaction score
0
We are getting closer @Narvuk

This does not delete, but flags all as having been read:
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.from_])

This one gets error:
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.body])

Code:
C:\Users\Desktop\Desktop\Python Spam Buster                                                       
λ Python DeleteEmailFiles.py                                                                     
Traceback (most recent call last):                                                               
  File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 12, in <module>   
    mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.body])                     
  File "C:\Users\Desktop\Desktop\Python Spam Buster\DeleteEmailFiles.py", line 12, in <listcomp> 
    mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.body])                     
AttributeError: 'MailMessage' object has no attribute 'body'

This does not delete, but flags all as having been read:
Python:
mailbox.delete([msg.uid for msg in mailbox.fetch() if '[email protected]' in msg.html])
 
Joined
Aug 31, 2022
Messages
5
Reaction score
0
bit of a dirty way of doing it and is not somthing i would normally do but have a check to see if this works

Python:
uids = []
for msg in mailbox.fetch():
    if '[email protected]' in msg.from_:
        print(msg.uid, msg.date, msg.from_, msg.subject)
        uids.append(msg.uid)
mailbox.delete(uids)
print('Completed Process')
 
Ad

Advertisements

Joined
Aug 16, 2022
Messages
10
Reaction score
0
bit of a dirty way of doing it and is not somthing i would normally do but have a check to see if this works

Python:
uids = []
for msg in mailbox.fetch():
    if '[email protected]' in msg.from_:
        print(msg.uid, msg.date, msg.from_, msg.subject)
        uids.append(msg.uid)
mailbox.delete(uids)
print('Completed Process')

Flagged all as having been read, "Completed Process", but did not delete spam.
xxx.png
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top