Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I have a python code that I want to delete an email with specific subject.But I get an error.What should i do?

Error:
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "__carClicker__.py", line 753, in <module>
    read_TXT_FILE_from_gmail()
  File "__carClicker__.py", line 214, in read_TXT_FILE_from_gmail
    mail.store(mail_ids , '+FLAGS', '(\\Trash)')
  File "/usr/lib/python3.8/imaplib.py", line 842, in store
    typ, dat = self._simple_command('STORE', message_set, command, flags)
  File "/usr/lib/python3.8/imaplib.py", line 1205, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python3.8/imaplib.py", line 963, in _command
    data = data + b' ' + arg
TypeError: can't concat list to bytes

Here is the code:

What I have tried:

Python
global FROM_EMAIL , FROM_PWD
    mail = imaplib.IMAP4_SSL(SMTP_SERVER)
    mail.login(FROM_EMAIL , FROM_PWD)
    mail.select('inbox')
    
    data = mail.search(None, 'ALL')
    mail_ids = data[1]
    id_list = mail_ids[0].split()   
    latest_email_id = int(id_list[-1])
    check_last_N_emails = 11
    for e in range(latest_email_id , latest_email_id - check_last_N_emails , -1):
        data = mail.fetch(str(e), '(RFC822)' )
        for response_part in data:
            arr = response_part[0]
            if isinstance(arr, tuple):
                msg = email.message_from_string(str(arr[1],'utf-8'))
                email_subject = msg['subject']
                email_from = msg['from']

        for part in msg.walk():
            filename__ = part.get_filename()
            if filename__:
                open(PATH_NAME + str(filename__) , "wb").write(part.get_payload(decode=True))
               
        if(not "20 errors occured" in email_subject):
            now = datetime.datetime.now()
            
            if(email_subject == "update" or email_subject == "Update"):
                mail.store(mail_ids , '+FLAGS', '(\\Trash)')


This is a function.I use it to do other things too.
I think the mistake here is the last command.

Thanks for your help.
Posted
Updated 4-Nov-22 20:10pm
Comments
Richard MacCutchan 27-Oct-22 11:42am    
The error is telling you that one of the required libraries is not at the correct version.
Sandeep Mewara 27-Oct-22 16:23pm    
You seems to be passing list in store command instead of individual emails.
Try this instead of last line:
for num in mail_ids.split():   mail.store(num, '+FLAGS', '\\Deleted')

1 solution

[Moving the comment to answer with a reference]

You seems to be passing list in store command instead of individual emails. You need to try this instead of last line in your original code:
Python
for num in mail_ids.split():   
    mail.store(num, '+FLAGS', '\\Deleted')
Refer: How to Delete Emails in Python - Python Code[^]

BTW, as Richard pointed out, you also have version issues with dependent libraries which you can correct off - though they seem to be raising warning right now and are not the reason for the error you get.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900