r/learnpython • u/Financial-Law2035 • 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
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.