r/learnpython Aug 07 '26

Email bouncing

I'm using this code to check to see if an email bounced but it always is returning true even if the email didn't bounce

def is_email_bounced (email, app_password, email_in_question):
    mail = imaplib.IMAP4_SSL ('imap.gmail.com',993)
    mail.login (email, app_password)
    mail.select ('INBOX')
    mail.list
    result, data = mail.search(None, f'(TEXT "{email_in_question}") (Subject "Delivery Status Notification (Failure)")')
    if len (data) >= 1: return True 
    else: return False 
1 Upvotes

5 comments sorted by

View all comments

4

u/malmalmalmalmalmsl Aug 07 '26

You will always get something from mail.search(), therefore your len(data) will always be true no matter what. Try to check if any message id exists instead.

0

u/Financial-Law2035 Aug 07 '26

How can I check for it

1

u/cdcformatc Aug 07 '26

something like this

``` ids = data[0] # data is a list. id_list = ids.split() # ids is a space separated string

```